├── LICENSE
├── README.md
└── aps-length
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | aps-length
2 | ==========
3 |
4 | (c) James Kermode 2014
5 |
6 | Usage: aps-length [options] tex-files
7 |
8 | Count number of equivalent words in an APS manuscript formatted in
9 | LaTeX, following guidelines described at http://journals.aps.org/authors/length-guide
10 |
11 | Requires PyMuPDF to be installed (`python3 -m pip install PyMuPDF`) for pdf figures.
12 | Note that PyMuPDF is more accurate than gs and identify for pdf figures.
13 |
14 | Requires _detex_ (http://www.ctan.org/pkg/detex) and either
15 | _ghostscript_ (http://www.ghostscript.com/) or ImageMagick _identify_
16 | (http://www.imagemagick.org) to be available on your path.
17 |
18 | Use --figs=gs if you have .eps or .pdf figures, and --figs=identify if you have
19 | .png figures.
20 |
21 | Options:
22 | -h, --help show this help message and exit
23 | -v key value, --var=key value
24 | Define TeX variables e.g. to specify location of
25 | figure files.
26 | -e env1,env2,..., --env=env1,env2,...
27 | Comma-separated list of LaTeX environments to ignore.
28 | -m (detex | wordcount), --method=(detex | wordcount)
29 | Tool to use to count words in main text. Default is
30 | wordcount, detex is also supported
31 | (but tends to underestimate word count).
32 | -f (identify | gs), --figs=(identify | gs)
33 | Tool to use to extract bounding box from figure.
34 | Default is gs, ImageMagick identify also supported.
35 | gs works with eps and pdf images, while
36 | identify is a better choice for png images.
37 | --scale-figs=SCALE_FIGS
38 | Scale estimate of figure word counts by factor,
39 | default 1.1 (10%)
40 | -j PRL, --journal=PRL
41 | Journal abbreviation (e.g. PRL, PRB-RC)
42 | -l LATEX, --latex=LATEX
43 | Latex executable. Default is "pdflatex".
44 |
--------------------------------------------------------------------------------
/aps-length:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 |
3 | """
4 | Count length of an APS manuscript formatted in LaTeX, following
5 | guidelines described at http://journals.aps.org/authors/length-guide
6 |
7 | Copyright (c) James Kermode 2014
8 |
9 | This program is free software; you can redistribute it and/or modify
10 | it under the terms of the GNU General Public License as published by
11 | the Free Software Foundation; either version 2 of the License, or
12 | (at your option) any later version.
13 |
14 | This program is distributed in the hope that it will be useful,
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 | GNU General Public License for more details.
18 |
19 | You should have received a copy of the GNU General Public License along
20 | with this program; if not, write to the Free Software Foundation, Inc.,
21 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 | """
23 |
24 | import glob
25 | import optparse
26 | import os
27 | import sys
28 | import subprocess
29 | import re
30 | import fitz
31 |
32 | word_limit = {'PRL': 3750,
33 | 'PRA-RC': 4500,
34 | 'PRB-RC': 3500,
35 | 'PRC-RC': 4500,
36 | 'PRD-RC': 4500,
37 | 'PRE-RC': 4500,
38 | 'PRApplied': 3500,
39 | 'PRST-PER': 3500}
40 | # word limits from: https://journals.aps.org/authors/length-guide
41 | # updated 2017-10-03
42 |
43 | wordcount_tex_contents = r"""\ProvidesFile{wordcount.tex}[2000/09/27 v1.5 Michael Downes]
44 | % Copyright 2000 Michael John Downes
45 | % This file has no restrictions on its use, distribution, or sale.
46 | %
47 | % If you run LaTeX on wordcount.tex it will prompt you for the name of a
48 | % document to be counted. For most people, however, it will be more
49 | % convenient to run the shell script wordcount.sh, giving the document
50 | % name as the first argument. The comments in wordcount.sh
51 | % give further information about the usage and limitations of this tool.
52 |
53 | % The fundamental idea is to mark each character and interword space
54 | % with a unique tag that will show up in TeX "showbox" output. Then
55 | % arrange to make the output routine trigger a TeX overfull vbox message
56 | % for the page box so that everything gets reported in the TeX log.
57 | % Then run grep -c (or an equivalent text search utility, e.g., perl) on
58 | % the log file to count the occurrences.
59 | %
60 | % In showbox output, a character is typically represented by a line of
61 | % the form
62 | %
63 | % ...\T1/cmr/m/n/10 e
64 | %
65 | % where "\T1/cmr/m/n/10" is the font name. We arrange things so that all
66 | % characters are typeset in a font named "\3.08632". Furthermore, for
67 | % interword spaces, which are normally represented in the output as
68 | % something like
69 | %
70 | % ...\glue 3.33333 plus 1.66666 minus 1.11111
71 | %
72 | % we use \spaceskip to ensure that it always appears as
73 | %
74 | % ...\glue(\spaceskip) 3.08633 plus 9.0
75 | %
76 | % Then to count characters (including word spaces) we can
77 | % grep for lines matching "3.0863", while to count words we
78 | % count the number of lines in the log that match "3.08633" but not
79 | % "3.08632".
80 | %
81 | % For an accurate count in multi-line paragraphs we also need
82 | % to set rightskip to the flag value, since interword spaces are
83 | % discarded at line breaks; and on the presumption that hyphenated
84 | % compounds like "steady-state" should be counted as two words, we set
85 | % \exhyphenpenalty to a value that ensures a line break at *every*
86 | % explicit hyphen---then we will get a rightskip glue node between the
87 | % two parts. (How about em-dashes? you may ask. Answer: it works all
88 | % right, try it and see.)
89 |
90 | \ifx\relax\?\def\wcQUIET{TT}\else\def\wcQUIET{TF}\fi
91 |
92 | % We want accented letters to be represented by a single font char, to
93 | % the extent possible:
94 | \RequirePackage[T1]{fontenc}
95 | \makeatletter
96 | \thickmuskip=5.55555mu plus5.55555mu minus 2.22222mu
97 | \let\@@thickmuskip\thickmuskip \newmuskip\thickmuskip
98 | \let\@@spaceskip\spaceskip \xspaceskip=0pt
99 | \newskip\@charfontname
100 | % Ensure that these cannot be overridden.
101 | \let\spaceskip\@charfontname \let\xspaceskip\spaceskip
102 | \def\FreezeSpaces{%
103 | \setbox\z@\hbox{\mathsurround=\z@ $\mkern\@@thickmuskip$}%
104 | \@@spaceskip=\wd\z@ plus 9pt\relax
105 | % Setting rightskip to a slightly different value means that
106 | % end-of-line spaces can be distinguished if desired. (By default,
107 | % they are not.) Using fil units means there won't be any hyphenation
108 | % except for explicit hyphens.
109 | \dimen@\@@spaceskip \advance\dimen@ 1sp\relax
110 | \rightskip\z@ plus\strip@pt\dimen@ fil\relax
111 | % Prevent any further changes to \rightskip.
112 | \let\rightskip\spaceskip
113 | \@charfontname=\wd\z@ \advance\@charfontname -1sp\relax
114 | % Cancel all non-1000 sfcodes, which might otherwise affect the value
115 | % of inter-sentence spaces and keep them from being counted as
116 | % interword spaces.
117 | \def\do{\sfcode\fam\@m \advance\fam\@ne
118 | \ifnum\fam>\@cclv\expandafter\@gobble\fi \do
119 | }%
120 | \fam\z@ \do
121 | \let\frenchspacing\relax \let\nonfrenchspacing\relax
122 | }
123 | \def\DashPatch{%
124 | \exhyphenpenalty=-\@M \let\exhyphenpenalty\count@
125 | \hyphenpenalty\@M \let\hyphenpenalty\count@
126 | % LaTeX bug? Default definitions for these don't have \exhyphenpenalty
127 | % built in. Change them to just print a hyphen so that the penalties
128 | % will work.
129 | \def\textemdash{-}\def\textendash{-}%
130 | }
131 | % Here we set the font to ptmr8t (a T1 encoded font for which most
132 | % people are likely to have a tfm file) and then make it difficult for
133 | % the font to be changed ever again. We use two names for the font,
134 | % \ptmr and \3.08632, the former being easier to use when resetting math
135 | % fonts. The name that is defined last when TeX processes a document is
136 | % the one that will be used in the log file.
137 | \def\FreezeFont{%
138 | \expandafter\font
139 | \csname\strip@pt\@charfontname\endcsname
140 | =ptmr8t \relax
141 | \ptmr
142 | \check@mathfonts \let\check@mathfonts\relax
143 | \def\do{%
144 | \textfont\fam=\ptmr\scriptfont\fam=\ptmr\scriptscriptfont\fam=\ptmr
145 | \advance\fam\@ne \ifnum\fam<\sixt@@n \else\expandafter\@gobble\fi
146 | \do
147 | }%
148 | \fam=0 \do
149 | }
150 | \font\ptmr=ptmr8t \relax
151 | \fontdimen22\ptmr=1sp % to permit use as math font 2
152 | \let\selectfont\relax
153 | \let\fontshape\@gobble \let\fontseries\@gobble \let\fontfamily\@gobble
154 | \let\fontencoding\@gobble \let\fontsize\@gobbletwo
155 | \let\linespread\@gobble
156 | \let\try@load@fontshape\relax
157 | \@namedef{U/msa/m/n}{}% disable special test in AMS documentclasses
158 | \def\define@newfont{\expandafter\let\font@name=\ptmr}
159 | \let\@@hfuzz\hfuzz \let\@@vfuzz\vfuzz
160 | \newdimen\hfuzz \let\vfuzz\hfuzz
161 | \def\QuietBoxes{%
162 | \@@hfuzz\maxdimen \@@vfuzz\maxdimen
163 | }
164 | \AtBeginDocument{\FreezeSpaces \FreezeFont \DashPatch \QuietBoxes}
165 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 | % Halign patches
167 | \def\ialign{\everycr{}\tabskip\@@spaceskip\halign} % initialized \halign
168 | \def\extracolsep#1{\tabskip\@@spaceskip}
169 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 | \nofiles \let\makeindex\@empty % work-around for a natbib problem
171 | % Seize control of the output routine to avoid extra clutter of dvi
172 | % files. Handling \AtBeginDVI would take some extra work--for now I
173 | % won't bother.
174 | \def\showpagebox{%
175 | \showboxbreadth\maxdimen\showboxdepth99
176 | % This is a devious way of getting the box contents into the log file
177 | % without actually using \showbox (which typically results in a
178 | % nonzero exit code from the TeX run).
179 | \@@vfuzz\z@
180 | \setbox\z@\vbox to\z@{\box\@outputbox}%
181 | \setbox\z@\box\voidb@x
182 | \global \@colht \textheight
183 | \stepcounter{page}%
184 | \let\firstmark\botmark
185 | }
186 | \AtBeginDocument{\def\@outputpage{\showpagebox}}
187 | \if\wcQUIET\else
188 | \begingroup \def\msg^^J#1]^^J{\endgroup\message{#1]^^J^^J}}
189 | \endlinechar=`\^^J \catcode`\ =12 \msg
190 |
191 | The 'wordcount' utility provides a way of estimating the number of
192 | words in a LaTeX document.
193 |
194 | [Warning: The counting method uses a large log file as an intermediate
195 | step. If your disk space is extremely low the counting process
196 | may end prematurely for lack of space.]
197 |
198 | \fi
199 |
200 | % \typein has extraneous blank lines and slightly idiosyncratic prompt.
201 | \begingroup \endlinechar=-1
202 | \message{File name [press RETURN to cancel]: }
203 | \def\do{%
204 | \let\do\@makeother \dospecials
205 | \global\read-1 to\wcFileName
206 | \endgroup
207 | }
208 | \do
209 |
210 | \ifx\@empty\wcFileName
211 | \typeout{No file name given; quitting.}\expandafter\@@end\fi
212 |
213 | \expandafter\filename@parse\expandafter{\wcFileName}
214 | \edef\jobname{\filename@base}
215 |
216 | \if\wcQUIET
217 | \message{\wcFileName}
218 | \else
219 | \typeout{^^JTo get a count of characters, run^^J^^J \space
220 | %
221 | grep -c '3[.]0863[23]' wordcount.log^^J^^J%
222 | %
223 | or equivalent when processing is finished.^^J%
224 | }
225 | \fi
226 |
227 | \typeout{Processing \wcFileName...^^J}
228 |
229 | \IfFileExists{\wcFileName}{\batchmode}{\nonstopmode}
230 | \edef\@tempa{\noexpand\makeatother
231 | \noexpand\input{\wcFileName}\noexpand\stop
232 | }
233 | \@tempa
234 | """
235 |
236 | def is_first_line(line):
237 | if line.strip() == '':
238 | return False
239 | if '%' in line:
240 | return False
241 | return True
242 |
243 | def count_main_text_words_detex(detex_lines, tex_lines):
244 | # remove images and empty lines
245 | detex_lines = [line for line in detex_lines if len(line.strip()) > 0]
246 | detex_lines = [line for line in detex_lines if ' /dev/null 2>&1' % opts.latex)
328 | os.system('bibtex tmp_tex_file > /dev/null 2>&1')
329 | os.system('%s tmp_tex_file > /dev/null 2>&1' % opts.latex)
330 | os.system('%s tmp_tex_file > /dev/null 2>&1' % opts.latex)
331 | os.system(r'echo tmp_tex_file.tex | %s wordcount.tex > /dev/null 2>&1' % opts.latex)
332 | wordcount_log = open('wordcount.log', encoding="latin-1").readlines()
333 |
334 | rm_files = glob.glob('wordcount*') + glob.glob('tmp_tex_file*')
335 | for file in rm_files:
336 | os.unlink(file)
337 |
338 | wordcount = len([line for line in wordcount_log if '3.08633' in line or '3.08635' in line])
339 | return wordcount
340 |
341 | def find_equation_lines(tex_lines):
342 | return [i for (i, line) in enumerate(tex_lines) if (r'\begin{equation' in line or
343 | r'\begin{eqnarray' in line or
344 | r'\begin{align' in line or
345 | r'\begin{displaymath' in line)]
346 | def count_equation_words(lines, start_line):
347 | count = 1
348 | array = False
349 | array_lines = 0
350 | two_column = '*' in lines[start_line]
351 | if two_column:
352 | eqn_words_per_line = 32
353 | else:
354 | eqn_words_per_line = 16
355 |
356 | for line in lines[start_line+1:]:
357 | if (r'\end{equation' in line or
358 | r'\end{eqnarray' in line or
359 | r'\end{align' in line or
360 | r'\end{displaymath' in line):
361 | break
362 | if r'\\' in line:
363 | if not array:
364 | count += 1
365 | else:
366 | array_lines += 1
367 | if r'\begin{array}' in line:
368 | array = True
369 | array_lines = 0
370 | if r'\end{array}' in line:
371 | array = False
372 |
373 | if array_lines != 0:
374 | count *= array_lines
375 |
376 | return count*eqn_words_per_line
377 |
378 | def count_equations_words(tex_lines):
379 | eqn_line_nos = find_equation_lines(tex_lines)
380 | eqn_words = [count_equation_words(tex_lines, eqn_line_no) for eqn_line_no in eqn_line_nos]
381 | print(('Equations: %r' % eqn_words))
382 | return sum(eqn_words)
383 |
384 | def find_table_lines(tex_lines):
385 | return [i for (i, line) in enumerate(tex_lines) if r'\begin{tabl' in line]
386 |
387 | def count_table_words(tex_lines, table_line):
388 | two_column = 'table*' in tex_lines[table_line]
389 | count = 1
390 | for line in tex_lines[table_line:]:
391 | if r'\end{table' in line:
392 | break
393 | if r'\\' in line:
394 | count += 1
395 | if two_column:
396 | print('Two-column table with %d lines' % count)
397 | return int(13.*count + 26.)
398 | else:
399 | print('Single-column table with %d lines' % count)
400 | return int(6.5*count + 13.)
401 |
402 | def count_tables_words(tex_lines):
403 | table_lines = find_table_lines(tex_lines)
404 | table_words = [ count_table_words(tex_lines, table_line) for table_line in table_lines ]
405 | print('Tables: %r' % table_words)
406 | return sum(table_words)
407 |
408 |
409 | def count_figures_words(detex_lines, tex_lines, opts):
410 | if opts.var is None:
411 | tex_vars = {}
412 | else:
413 | tex_vars = dict(opts.var)
414 |
415 | # fig_lines = [line for line in detex_lines if ' wl:
544 | over_under = 'OVER'
545 | else:
546 | over_under = 'UNDER'
547 | print(('Manuscript %s is currently %d words (%.0f%%) %s limit of %d words for journal %s' %
548 | (tex_file, abs(total_words - wl),
549 | float(total_words - wl)/wl*100., over_under,
550 | wl, opts.journal)))
551 |
552 |
553 | parser = optparse.OptionParser(usage='%prog [options] tex-files',
554 | description="""
555 | Count length of an APS manuscript formatted in LaTeX, following
556 | guidelines described at http://journals.aps.org/authors/length-guide""")
557 |
558 | parser.add_option('-v', '--var', metavar='key value', nargs=2, action='append',
559 | help='Define TeX variables e.g. to specify location of figure files.')
560 | parser.add_option('-e', '--env', metavar='env1,env2,...',
561 | help='Comma-separated list of LaTeX environments to ignore.',
562 | default='abstract,acknowledgments,acknowledgements,displaymath,equation,eqnarray,thebibliography')
563 | parser.add_option('-m', '--method', metavar='(detex | wordcount)', default='wordcount',
564 | help='''Tool to use to count words in main text. Default is wordcount.
565 | detex is also supported (but tends to underestimate word count).''')
566 | parser.add_option('-f', '--figs', metavar='(identify | gs)',
567 | help='''Tool to use to extract bounding box from figure.
568 | The default is identify, which works for png, pdf, eps, and most image formats, but requires
569 | ImageMagick to be installed. The other option is gs, which works with eps and pdf mages.''',
570 | default='identify')
571 | parser.add_option('--scale-figs', type=float, default=1.1,
572 | help='Scale estimate of figure word counts by factor, default 1.1 (10%)')
573 | parser.add_option('-j', '--journal', metavar='PRL', default='PRL',
574 | help='Journal abbreviation (e.g. PRL, PRB-RC)')
575 | parser.add_option('-l', '--latex', default='pdflatex',
576 | help='Latex executable. Default is "pdflatex".')
577 |
578 |
579 | opts, args = parser.parse_args()
580 |
581 | orig_eps_pdf_files = glob.glob('*-eps-converted-to.pdf')
582 |
583 | for tex_file in args:
584 | process(tex_file, opts)
585 |
586 | for eps_pdf_file in glob.glob('*-eps-converted-to.pdf'):
587 | if eps_pdf_file not in orig_eps_pdf_files:
588 | os.remove(delname)
589 |
--------------------------------------------------------------------------------