Debian Codegraph @

texmf-dist/scripts/latex-papersize/latex-papersize.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/env python
2 r"""
3 Calculate LaTeX paper and margin settings for arbitrary magnification
4 (C) Silas S. Brown, 2005-2009, 2016, 2019. Version 1.63.
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 When producing enlarged material in LaTeX for people with
19 low vision, it's often not enough to simply add such
20 things as "\Large" because that doesn't enlarge
21 _everything_ and it can be difficult to achieve the exact
22 desired size, especially if unusual packages are being
23 used as well.
24
25 It's more effective to change the LaTeX paper size and
26 margin settings to simulate SMALL PAPER, and then magnify
27 the result up to the desired physical paper size. This
28 magnifies everything, and also adds some clarity for
29 low-vision users because fonts like CMR have different
30 versions at different sizes and the small-sized versions
31 are often meant to be clearer.
32
33 This is a Python script to calculate the necessary
34 settings for arbitrary font and page sizes.
35 Works in both Python 2 and Python 3.
36
37 BASIC USAGE
38
39 In the following instructions, base-size is the point size
40 that the TeX file is based on (if the documentclass
41 specifies 12pt then \small=11 normal=12 \large=14
42 \Large=17 \LARGE=20 \huge=25), and desired-size is the
43 point size that you want to produce.
44
45 The command-line parameters depend on whether you are
46 using latex/dvips or pdflatex.
47
48 FOR USE WITH LATEX AND DVIPS:
49
50 To print the geometry settings:
51
52 python latex-papersize.py base-size desired-size tex
53
54 e.g.: python latex-papersize.py 12 26 tex
55
56 (If you install latex-papersize.py as /usr/local/bin/latex-papersize,
57 then you can say "latex-papersize" instead of "python latex-papersize.py"
58 throughout these examples.)
59
60 The output of this command should then be placed after
61 \documentclass in your .tex file.
62
63 Then run latex to make a DVI file, and then do:
64
65 python latex-papersize.py base-size desired-size dvi-file
66
67 e.g.: python latex-papersize.py 12 26 myfile.dvi
68
69 which will print the appropriate dvips command.
70
71 FOR USE WITH PDFLATEX:
72
73 With pdflatex you need only one command:
74
75 python latex-papersize.py base-size desired-size pdftex
76
77 the output of this command should go just before \begin{document}.
78 NB if using hyperref package, put hyperref AFTER these
79 settings (and if also using pinyin package then put pinyin
80 package after that again)
81
82 PAGE NUMBERS:
83
84 When invoking latex-papersize.py with the "tex" or "pdftex"
85 options, you can optionally add a fourth parameter to
86 specify how many points to leave for page numbers at the
87 bottom of each page. Normally 15 is a good idea. Leaving
88 this out causes no room to be left for page numbers and
89 \pagestyle{empty} to be added. This does not affect the
90 dvips command.
91
92 PAPER SIZES AND MARGINS:
93
94 It is assumed that the final physical printout will be on
95 A4 portrait with 10mm margins. You can override this by
96 setting the paper_width and paper_height environment
97 variables (in millimetres) before running, e.g.:
98 paper_width=297 paper_height=210 python latex-papersize.py ...
99 You can also set the environment variables margin_left and
100 margin_top (the right and bottom margins are assumed to be
101 mirrors of these).
102
103 If using with non-PDF latex, remember to set the
104 environment variables again when asking latex-papersize.py for
105 the dvips command. Or "export" them so they remain set.
106
107 POSTER PRINTING:
108
109 If you want to print on a physical paper size that is
110 larger than your printer can handle, you can make up the
111 larger size by sticking together smaller pieces of paper.
112 Perhaps the best way to do this is to use a separate
113 utility that knows about printable areas, cut margins,
114 etc, such as Jos van Eijndhoven's "poster" utility (last
115 known URL: ftp://ftp.es.ele.tue.nl/pub/users/jos/poster/poster.tar.gz )
116
117 In this case you can give latex-papersize.py a margin_left and
118 margin_top of 0 (because "poster" will handle the
119 margins), set the paper size and desired point size
120 appropriate for the final poster, use latex/dvips, and run
121 the resulting .ps through "poster" with "-s 1". This is
122 better than getting "poster" to scale, because if any of
123 your fonts are rendered into bitmaps by Metafont (as some
124 CJK fonts are) then you will likely get a better
125 resolution if the final size is given to latex-papersize.py
126 rather than to "poster".
127
128 If you're aiming for a specific number of sheets of paper,
129 don't pick a physical size that's an exact multiple of
130 your printer's paper size, because "poster" accounts for
131 unprintable areas and overlaps slightly. Multiply the
132 printer paper's height and width by 0.88 (or 0.86 if you
133 want a visual margin added to the finished poster besides
134 the cutting margins) and try a multiple of that size.
135
136 EXAMPLE USAGE IN A SCRIPT:
137
138 To typeset a LaTeX file "file.tex" and magnify from
139 12-point to 26-point, type a line such as the following
140 (after adjusting the documentclass it specifies, and
141 removing or commenting out the documentclass in file.tex):
142
143 latex "\\documentclass[12pt]{article}$(python latex-papersize.py 12 26 tex)\\input{file.tex}" && mv article.dvi file.dvi
144
145 Or in pdflatex (slightly more complex because we need to
146 put the settings just before \begin{document}):
147 cat file.tex | awk -- "/^ *\\\\begin *\\{document\\}[^#-~]*\$/ { print \"$(python latex-papersize.py 12 26 pdftex | sed -e 's/\\/\\\\/g')\" } { print }" > /tmp/tmp.tex
148 pdflatex "\documentclass[12pt]{article}\\input{/tmp/tmp.tex}" && mv article.pdf file.pdf
149
150 To run dvips on the .dvi file (not needed for pdflatex):
151
152 $(python latex-papersize.py 12 26 file.dvi)
153 """
154
155 import os, sys, math
156 try: from commands import getoutput # Python 2
157 except: from subprocess import getoutput # Python 3
158 def hasKey(a,b):
159 try: return a.has_key(b) # old Python 2
160 except: return b in a # newer Python 2 + Python 3
161 if len(sys.argv)==2 and sys.argv[1]=="--help":
162 print(__doc__.strip()); raise SystemExit
163 if len(sys.argv)==2 and sys.argv[1]=="--version":
164 print(__doc__[:__doc__.find("\n\n")].strip()); raise SystemExit
165
166 base_pointsize = float(sys.argv[1])
167 desired_pointsize = float(sys.argv[2])
168
169 if len(sys.argv)>4:
170 extra_bottom_margin_mm = float(sys.argv[4])*25.4/72
171 pageStyle = ""
172 else:
173 extra_bottom_margin_mm = 0
174 pageStyle = " \\pagestyle{empty}"
175
176 if hasKey(os.environ,"paper_width"): paper_width=float(os.environ["paper_width"])
177 else: paper_width=210
178 if hasKey(os.environ,"paper_height"): paper_height=float(os.environ["paper_height"])
179 else: paper_height=297
180 if hasKey(os.environ,"margin_left"): margin_left=float(os.environ["margin_left"])
181 else: margin_left=10
182 if hasKey(os.environ,"margin_top"): margin_top=float(os.environ["margin_top"])
183 else: margin_top=10
184
185 paper_magstep = 1.0*desired_pointsize/base_pointsize
186 paperwidth = paper_width/paper_magstep
187 textwidth = (paper_width-2*margin_left)/paper_magstep
188 paperheight = paper_height/paper_magstep
189 textheight = (paper_height-2*margin_top)/paper_magstep-extra_bottom_margin_mm # note extra_bottom_margin_mm is NOT divided by paper_magstep because it corresponds to the height of the textual page-number, which will be magnified
190 margin_left_setting = margin_left/paper_magstep
191 margin_top_setting = margin_top/paper_magstep
192
193 if sys.argv[3]=="tex" or sys.argv[3]=="pdftex":
194 s="\\textwidth=%.1fmm \\textheight=%.1fmm \\topmargin=%.1fmm \\marginparwidth=0mm \\oddsidemargin=%.1fmm \\evensidemargin=%.1fmm \\columnsep=%.1fmm%s" % (textwidth,textheight,margin_top_setting,margin_left_setting,margin_left_setting,margin_left_setting,pageStyle)
195 if sys.argv[3]=="pdftex":
196 s += "\\mag=%d \\pdfpagewidth=%d true mm \\pdfpageheight=%d true mm \\pdfhorigin=0 mm \\pdfvorigin=-12.95 mm \\paperwidth=%d true mm \\paperheight=%d true mm" % (1000*paper_magstep,paper_width,paper_height,paper_width,paper_height) # the -12.95mm seems to be a constant regardless of magnification (previous version had -14 but it sems -12.95 is more accurate - at least 12.9 is too small and 13 is too big). Need \paperwidth and \paperheight in there as well in case using hyperref.
197 print(s)
198 else:
199 r = os.system("dvips -T %dmm,%dmm -x %d %s -o bbox_test.ps" % (paper_width*10,paper_height*10,1000*paper_magstep+0.5,sys.argv[3]))
200 assert not r, "dvips failed"
201 # Now, that would have got the origin wrong. I can't
202 # figure out how dvips origin and magstep is supposed to
203 # interoperate, so let's work it out on a case-by-case
204 # basis from the bounding box.
205 # (Note: multiplying paper_width and paper_height by 10 above, because if dealing with very small paper sizes then this may give a reading of 0 if the origin is off the page. Increasing the paper size doesn't seem to affect the origin.)
206 bbox=getoutput("echo|gs -sDEVICE=bbox bbox_test.ps 2>&1|grep BoundingBox")
207 # (previous version used 'head -1' to take only the first page, but that can cause 'broken pipe' errors if the file contains too many pages, and will give an incorrect result if there is only one line per page and it is indented on the first page, so we'll look at ALL the pages and take the outermost bounds. Will also look at high-resolution bounding boxes only, if available.)
208 if "HiResBoundingBox" in bbox: bbox=filter(lambda x:"HiRes" in x,bbox.split("\n"))
209 else: bbox=bbox.split("\n")
210 bbox = map(lambda x:tuple(map(lambda y:float(y),x.split(" ")[1:])), bbox)
211 bbox = filter(lambda x: not x==(0,0,0,0), bbox)
212 assert bbox, "Could not get a sensible bounding box from bbox_test.ps. (If you're on Ubuntu, beware of bug #160203 in Ubuntu's ghostscript package.)"
213 os.unlink("bbox_test.ps")
214 existing_left_margin_mm = min(map(lambda x:x[0],bbox))*25.4/72
215 existing_top_margin_mm = paper_height*10-max(map(lambda x:x[3],bbox))*25.4/72
216 print("dvips -T %dmm,%dmm -O %.1fmm,%.1fmm -x %d %s" % (paper_width,paper_height,margin_left - existing_left_margin_mm,margin_top - existing_top_margin_mm,1000*paper_magstep+0.5,sys.argv[3]))