Makefile itself (previously forgotten)
[PyX/mjg.git] / manual / tex.tex
blobfd43bc4fd5b97a3c26d18dfc009c638bbdce748d
1 \chapter{Module tex: \TeX/\LaTeX{} interface}
2 \label{tex}
3 \section{Methods}
4 Text in \PyX{} is created by \TeX{} or \LaTeX. From the technical point
5 of view, the text is inserted as an Encapsulated PostScript file
6 (\verb|eps|-file). This \verb|eps|-file is generated by the module
7 \verb|tex| which runs \TeX{} or \LaTeX{} followed by \verb|dvips| to
8 create the requested text. \TeX{} is used by instances of the class
9 \verb|tex| while \LaTeX{} is used by \verb|latex|. Up to the
10 constructor and the advanced possibilities in \LaTeX{} commands both
11 classes \verb|tex| and \verb|latex| are identical. They provide 5
12 methods to the user listed in the following table:
14 \smallskip
15 \begin{tabularx}{\linewidth}{ll>{\raggedright\arraybackslash}X}
16 method&task&allowed attributes in \texttt{*attr}\\
17 \hline
18 \texttt{text(x, y, cmd, *attr)}&print \texttt{cmd}&\texttt{style, fontsize, halign, valign, direction, color, msghandler(s)}\\
19 \texttt{define(cmd, *attr)}&execute \texttt{cmd}&\texttt{msghandler(s)}\\
20 \texttt{textwd(cmd, *attr)}&width of \texttt{cmd}&\texttt{style, fontsize, missextents, msghandler(s)}\\
21 \texttt{textht(cmd, *attr)}&height of \texttt{cmd}&\texttt{style, fontsize, valign, missextents, msghandler(s)}\\
22 \texttt{textdp(cmd, *attr)}&depth of \texttt{cmd}&\texttt{style, fontsize, valign, missextents, msghandler(s)}\\
23 \end{tabularx}
24 \smallskip
26 There are some common rules:
27 \begin{itemize}
28 \item \verb|cmd| stands for a \TeX{} or \LaTeX{} expression. To
29 prevent a backslash plague, python's raw string feature can nicely be
30 used. \verb|x|, \verb|y| specify a position.
31 \item \verb|define| can only be called before any of the other
32 methods. In \LaTeX{} definitions are inserted directly in front of
33 the \verb|\begin{document}| statement. However, this is not a
34 limitation, because by \verb|\AtBeginDocument{}| definitions can be
35 postponed.
36 \item The extent routines \verb|textwd|, \verb|textht|, and
37 \verb|textdp| return true \PyX{} length (see section~\ref{unit}).
38 Usually, the evaluation takes place when performing a write and the
39 results are stored in a file with the suffix \verb|.size|. Therefore
40 you have to run your file twice at first to get the correct value.
41 This default behaviour can be changed by the \verb|missextents|
42 attribute.
43 \item All commands are passed to \TeX{} or \LaTeX{} in the calling
44 order of the methods with one exception: if the same command is used
45 several times (for printing as well as for calculating extents), all
46 requests are executed at the position of the first occurrence of the
47 command.
48 \item All text is inserted into the \verb|canvas| at the position,
49 where the \verb|tex|- or \verb|latex|-instance itself is inserted into
50 the \verb|canvas|. In fact, the \verb|eps|-file created by \TeX{} or
51 \LaTeX{} and \verb|dvips| is just inserted.
52 \item The tailing \verb|*style| parameter stands for a list of
53 attribute parameters listed in the last column of the table. Attribute
54 parameters are instances of classes discussed in detail in the
55 following section.
56 \item There can be several \verb|msghandler| attributes which will be
57 applied sequentially. All other parameters can occure only once.
58 \end{itemize}
61 \section{Attributes}
62 \begin{description}
63 \item[\texttt{style:}] \verb|style.text| (default -- does nothing to
64 the command),\\\verb|style.math| (switches to math mode in
65 \verb|\displaystyle|)
66 \item[\texttt{fontsize:}] specifies the \LaTeX{} font sizes by
67 \verb|fontsize.xxx| where \verb|xxx| is one of
68 \verb|tiny|,
69 \verb|scriptsize|,
70 \verb|footnotesize|,
71 \verb|small|,
72 \verb|normalsize| (default),
73 \verb|large|,
74 \verb|Large|,
75 \verb|LARGE|,
76 \verb|huge|, or
77 \verb|Huge|.
78 \item[\texttt{halign:}] \verb|halign.left| (default),
79 \verb|halign.center|, \verb|halign.right|
80 \item[\texttt{valign:}] \verb|valign.top(length)| or
81 \verb|valign.bottom(length)| --- creates a vertical box with width
82 \verb|length|. The vertical alignment is the baseline of the first line
83 for \verb|top| and the last line for \verb|bottom|. The box width is
84 stored in the \TeX{} dimension \verb|\linewidth|.
85 \item[\texttt{direction:}] \verb|direction.xxx| where \verb|xxx|
86 stands for \verb|horizontal| (default), \verb|vertical|,
87 \verb|upsidedown|, or \verb|rvertical|. Additionally, any angle
88 \verb|angle| (in degree) is allowed in \verb|direction(angle)|.
89 \item[\texttt{color:}] stands for any \PyX{} color (see
90 section~\ref{color}), default is \verb|color.gray.black|
91 \item[\texttt{missextents:}] provides a routine, which is called when a
92 requested extent is not yet available. In the following table a list
93 of choises for this parameter is described:
95 \begin{tabularx}{\linewidth}{l>{\raggedright\arraybackslash}X}
96 missextents&description\\
97 \hline
98 \texttt{missextents.returnzero}&returns zero (default)\\
99 \texttt{missextents.returnzeroquiet}&as above, but does not return a
100 warning via \texttt{atexit}\\
101 \texttt{missextents.raiseerror}&raise \texttt{TexMissExtentError}\\
102 \texttt{missextents.createextent}&run \TeX{} or \LaTeX{} immediately to
103 get the requested size\\
104 \texttt{missextents.createallextent}&run \TeX{} or \LaTeX{} immediately
105 to get the hight, width, and depth of the given text at once\\
106 \end{tabularx}
107 \item[\texttt{msghandler:}] provides a filter for \TeX{} and \LaTeX{}
108 messages and defines, which messages are hidden. In the following table
109 the predefined message handlers are described:
111 \smallskip
112 \begin{tabularx}{\linewidth}{l>{\raggedright\arraybackslash}X}
113 msghandler&description\\
114 \hline
115 \texttt{msghandler.showall}&shows all messages\\
116 \texttt{msghandler.hideload}&Hides messages which are written when loading
117 packages and including other files. They look like \texttt{(file...)}
118 where \texttt{file} is a readable file and \texttt{...} stands for any
119 text. This message handler is the default handler.\\
120 \texttt{msghandler.hidegraphicsload}&Hides messages which are written by
121 \texttt{includegraphics} of the \texttt{graphicx} package. They look like
122 \texttt{<file>} where \texttt{file} is a readable file.\\
123 \texttt{msghandler.hidefontwarning}&Hides \LaTeX{} font warnings. They
124 look like \texttt{LaTeX Font Warning:} and are followed by lines starting
125 with \texttt{(Font)}.\\
126 \texttt{msghandler.hidebuterror}&Hides messages except those
127 with a line which starts with ``\texttt{! }''.\\
128 \texttt{msghandler.hideall}&hides all messages\\
129 \end{tabularx}
130 \end{description}
133 \section{Constructors}
134 Named parameters of the constructor are used to set global options for
135 the instances of the classes \verb|tex| and \verb|latex|.
136 There are some common options for both classes listed in the following
137 table.
139 \smallskip
140 \begin{tabularx}{\linewidth}{ll>{\raggedright\arraybackslash}X}
141 parameter name&default value&description\\
142 \hline
143 \texttt{defaultmsghandler}&\texttt{msghandler.hideload}&default
144 message handler (tuple of message handlers is possible)\\
145 \texttt{defaultmissextents}&\texttt{missextents.returnzero}&default missing extent handler\\
146 \texttt{texfilename}&\texttt{None}&Filename used for running \TeX{} or
147 \LaTeX. If \texttt{None}, a temporary name is used and the files are
148 removed automatically. It can be used to trace errors.\\
149 \end{tabularx}
150 \smallskip
152 Additionally, the class \verb|tex| has another option described in
153 the following table.
155 \smallskip
156 \begin{tabularx}{\linewidth}{ll>{\raggedright\arraybackslash}X}
157 parameter name&default value&description\\
158 \hline
159 \texttt{lts}&\texttt{"10pt"}&Specifies a latex font size file. Those
160 files with the suffix \texttt{.lfs} can be created by
161 \texttt{createlfs.tex}. Possible values are listed when a requested
162 name couldn't be found.\\
163 \end{tabularx}
164 \smallskip
166 Instead of the option listed in the table above, for the class
167 \verb|latex| the options described in the following table are
168 available (additionally to the common available options).
170 \smallskip
171 \begin{tabularx}{\linewidth}{ll>{\raggedright\arraybackslash}X}
172 parameter name&default value&description\\
173 \hline
174 \texttt{docclass}&\texttt{"article"}&specifies the document class\\
175 \texttt{docopt}&\texttt{None}&specifies options to the document class\\
176 \texttt{auxfilename}&\texttt{None}&Specifies a filename for storing the \LaTeX{}
177 \texttt{aux} file. This is needed when using labels and references.\\
178 \end{tabularx}
180 \section{Examples}
181 \subsection{Example 1}
182 \begin{quote}
183 \begin{verbatim}
184 from pyx import *
186 c = canvas.canvas()
187 t = c.insert(tex.tex())
189 t.text(0, 0, "Hello, world!")
191 print "width:", t.textwd("Hello, world!")
192 print "height:", t.textht("Hello, world!")
193 print "depth:", t.textdp("Hello, world!")
195 c.writetofile("tex1")
196 \end{verbatim}
197 \end{quote}
199 The output of this program is:
200 \begin{quote}
201 \begin{verbatim}
202 width: (0.019535 t + 0.000000 u + 0.000000 v + 0.000000 w) m
203 height: (0.002441 t + 0.000000 u + 0.000000 v + 0.000000 w) m
204 depth: (0.000683 t + 0.000000 u + 0.000000 v + 0.000000 w) m
205 \end{verbatim}
206 \end{quote}
208 The file \verb|tex1.eps| is created and looks like:
209 \begin{quote}
210 \includegraphics{tex1}
211 \end{quote}
213 \subsection{Example 2}
214 \begin{quote}
215 \begin{verbatim}
216 from pyx import *
218 c = canvas.canvas()
219 t = c.insert(tex.tex())
221 t.text(0, 0, "Hello, world!")
222 t.text(0, -0.5, "Hello, world!", tex.fontsize.large)
223 t.text(0, -1.5,
224 r"\sum_{n=1}^{\infty} {1\over{n^2}} = {{\pi^2}\over 6}",
225 tex.style.math)
226 c.stroke(path.line(5, -0.5, 9, -0.5))
227 c.stroke(path.line(5, -1, 9, -1))
228 c.stroke(path.line(5, -1.5, 9, -1.5))
229 c.stroke(path.line(7, -1.5, 7, 0))
231 t.text(7, -0.5, "left aligned") # default is tex.halign.left
232 t.text(7, -1, "center aligned", tex.halign.center)
233 t.text(7, -1.5, "right aligned", tex.halign.right)
235 c.stroke(path.line(0, -4, 2, -4))
236 c.stroke(path.line(0, -2.5, 0, -5.5))
237 c.stroke(path.line(2, -2.5, 2, -5.5))
239 t.text(0, -4,
240 "a b c d e f g h i j k l m n o p q r s t u v w x y z",
241 tex.valign.top(2))
243 c.stroke(path.line(2.5, -4, 4.5, -4))
244 c.stroke(path.line(2.5, -2.5, 2.5, -5.5))
245 c.stroke(path.line(4.5, -2.5, 4.5, -5.5))
247 t.text(2.5, -4,
248 "a b c d e f g h i j k l m n o p q r s t u v w x y z",
249 tex.valign.bottom(2))
251 c.stroke(path.line(5, -4, 9, -4))
252 c.stroke(path.line(7, -5.5, 7, -2.5))
254 t.text(7, -4, "horizontal")
255 t.text(7, -4, "vertical", tex.direction.vertical)
256 t.text(7, -4, "rvertical", tex.direction.rvertical)
257 t.text(7, -4, "upsidedown", tex.direction.upsidedown)
259 t.text(7.5, -3.5, "45", tex.direction(45))
260 t.text(6.5, -3.5, "135", tex.direction(135))
261 t.text(6.5, -4.5, "225", tex.direction(225))
262 t.text(7.5, -4.5, "315", tex.direction(315))
264 t.text(0, -6, "red", color.rgb.red)
265 t.text(3, -6, "green", color.rgb.green)
266 t.text(6, -6, "blue", color.rgb.blue)
268 c.writetofile("tex2")
269 \end{verbatim}
270 \end{quote}
272 The file \verb|tex2.eps| is created and looks like:
273 \begin{quote}
274 \includegraphics{tex2}
275 \end{quote}
277 \section{Known bugs}
278 \begin{itemize}
279 \item The end of the last paragraph in a vertical box
280 (\verb|valign.top| and \verb|valign.bottom|) must be explictly
281 written (by the command \verb|\par| or an empty line) when a paragraph
282 formating parameter is changed locally (like the \verb|\baselineskip|
283 when changing the font size). Otherwise, the information is thrown
284 away due to a closing of the block before the paragraph formatting is
285 performed.
286 \item Due to \verb|dvips| the bounding box is wrong for rotated text.
287 The rotation is just ignored in the bounding box calculation.
288 \item Analysing \TeX{} messages is a difficult subject and the message
289 handlers provided with \PyX{} are not at all perfect in that sense.
290 For the message handlers \verb|msghandler.hideload| and
291 \verb|msghandler.hidegraphicsload| it is known, that they do not
292 correctly handle long filenames splited on several lines by \TeX.
293 \end{itemize}
295 \section{Future of the module tex}
296 While we will certainly keep this module working at least for a while,
297 it is likely that another \TeX{} interface will occure soon. The idea
298 is to get rid of \verb|dvips| and integrate \TeX{} more directly into
299 \PyX.