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