3 :synopsis: High-level interface for text output including a TeX/LaTeX
13 The :mod:`text` module is used to create text output. It seamlessly integrates
14 Donald E. Knuths famous TeX typesetting engine\ [#]_. The module is a
15 high-level interface to an extensive stack of TeX and font related
16 functionality in PyX, whose details are way beyond this manual and completely
17 irrelevant for the typical PyX user. However, the basic concept should be
18 described briefly, as it provides important insights into essential properties
19 of the whole machinery.
21 PyX does not apply any limitations on the text submitted by the user. Instead
22 the text is directly passed to TeX. This has the implication, that the text to
23 be typeset should come from a trusted source or some security measures should
24 have been applied already. PyX just adds a light and transparent wrapper using
25 basic TeX functionality for later identification and output extraction. This
26 procedure enables full access to all TeX features and makes PyX on the other
27 hand dependent on the error handling provided by TeX. However, a detailed and
28 immediate control of the TeX output allows PyX to report problems back to the
31 While we only talked about TeX so far (and will continue to do so in the rest
32 of this section), it is important to note that the coupling is not limited to
33 plain TeX. Currently, PyX can also use LaTeX for typesetting, and other TeX
34 variants could be added in the future. What PyX really depends on is the
35 ability of the typesetting program to generate dvi\ [#]_.
37 As soon as some text creation is requested or, even before that, a preamble
38 setting or macro definition is submitted, the TeX program is started as a
39 separate process. The input and output of TeX is bound to a :class:`texrunner`
40 instance. Typically, the process will be kept alive and will be reused for all
41 future typesetting requests of this :class:`texrunner` instance until the end
42 of the PyX process. There are certain situations when the TeX program needs to
43 be shutdown early, which are be described in detail in the :ref:`texipc`
46 Whenever PyX sends some commands to the TeX interpreter, it adds an output
47 marker at the end, and waits for this output marker to be echoed in the TeX
48 output. All intermediate output is attributed to the commands just sent and
49 will be analysed for problems. This is done by :class:`texmessage` parsers.
50 Here, a problem could be logged to the PyX logger at warning level, thus
51 be reported to stderr by default. This happens for over- or underful boxes or
52 font warnings emitted by TeX. For other unknown problems (*i.e.* output not
53 handled by any of the given :class:`texmessage` parsers), a
54 :exc:`TexResultError` is raised, which creates a detailed error report
55 including the traceback, the commands submitted to TeX and the output returned
58 PyX wraps each text to be typeset in a TeX box and adds a shipout of this box
59 to the TeX code before forwarding it to TeX. Thus a page in the dvi file is
60 created containing just this output. Furthermore TeX is asked to output the box
61 extent. By that PyX will immediately know the size of the text without
62 referring to the dvi. This also allows faking the box size by TeX means, as you
65 Once the actual output is requested, PyX reads the content of the dvi file,
66 accessing the page related to the output in question. It then does all the
67 necessary steps to transform the dvi content to the requested output format,
68 like searching for virtual font files, font metrices, font mapping files, and
69 PostScript Type1 fonts to be used in the final output. Here a present
70 limitation has been mentioned: PyX presently can use PostScript Type1 fonts
71 only to generate text output. While this is a serious limitation, all the
72 default fonts in TeX are available in Type1 nowadays and current TeX
73 installations are alreadily configured to use them by default.
79 .. autoclass:: baserunner
80 :members: preamble, text, text_pt, texmessages_start_default, texmessages_end_default, texmessages_preamble_default, texmessages_run_default
82 .. autoclass:: texrunner
84 .. autoclass:: latexrunner
92 While running TeX (and variants thereof) a variety of information is written to
93 stdout like status messages, details about file access, and also warnings
94 and errors. PyX reads all the output and analyses it. Some of the output is
95 triggered as a direct response to the TeX input and is thus easy to understand
96 for PyX. This includes page output information, but also workflow control
97 information injected by PyX into the input stream. PyX uses it to check the
98 communication and typeset progress. All the other output is handled by a list
99 of :class:`texmessage` parsers, an individual set of functions applied to the
100 TeX output one after the other. Each of the function receives the TeX output as
101 a string and return it back (maybe altered). Such a function must perform one
102 of the following actions in response to the TeX output is receives:
104 1. If it does not find any text in the TeX output it feals responsible for, it
105 should just return the unchanged string.
107 2. If it finds a text it is responsible for, and the message is just fine
108 (doesn't need to be communicated to the user), it should just remove this
109 text and return the rest of the TeX output.
111 3. If the text should be communicated to the user, a message should be written
112 the the pyx logger at warning level, thus being reported to the user on
113 stderr by default. Examples are underfull and overfull box warnings or font
114 warnings. In addition, the text should be removed as in 2 above.
116 4. If the output contains an error information the user should fix, a
117 TexResultError should be raised.
119 This is rather uncommon, that the fourth option is taken. Instead errors can
120 just be kept in the output as PyX considers *all* unhandled TeX output as an
121 error. In addition to the error message, information about the TeX in- and
122 output can be echoed according to the :class:`errordetail` setting in the
125 .. autoclass:: errordetail
128 .. autoexception:: TexResultError
130 To prevent any unhandled TeX output to be reported as an error,
131 :attr:`texmessage.warn` or :attr:`texmessage.ignore` can be used. Here's a list
132 of all available :class:`texmessage` parsers:
134 .. autoclass:: texmessage
138 .. Instances of the class :class:`texrunner` are responsible for executing and
139 .. controling a TeX/LaTeX instance.
141 .. class:: texrunner(mode="tex", lfs="10pt", docclass="article", docopt=None, usefiles=[], fontmaps=config.get("text", "fontmaps", "psfonts.map"), waitfortex=config.getint("text", "waitfortex", 60), showwaitfortex=config.getint("text", "showwaitfortex", 5), texipc=config.getboolean("text", "texipc", 0), texdebug=None, dvidebug=0, errordebug=1, pyxgraphics=1, texmessagesstart=[], texmessagesdocclass=[], texmessagesbegindoc=[], texmessagesend=[], texmessagesdefaultpreamble=[], texmessagesdefaultrun=[])
143 *mode* should the string ``tex`` or ``latex`` and defines whether TeX or LaTeX
144 will be used. *lfs* specifies an ``lfs`` file to simulate LaTeX font size
145 selection macros in plain TeX. PyX comes with a set of ``lfs`` files and a LaTeX
146 script to generate those files. For *lfs* being ``None`` and *mode* equals
147 ``tex`` a list of installed ``lfs`` files is shown.
149 *docclass* is the document class to be used in LaTeX mode and *docopt* are the
150 options to be passed to the document class.
152 *usefiles* is a list of TeX/LaTeX jobname files. PyX will take care of the
153 creation and storing of the corresponding temporary files. A typical use-case
154 would be *usefiles=["spam.aux"]*, but you can also use it to access TeXs log and
157 *fontmaps* is a string containing whitespace separated names of font mapping
158 files. *waitfortex* is a number of seconds PyX should wait for TeX/LaTeX to
159 process a request. While waiting for TeX/LaTeX a PyX process might seem to do
160 not perform any work anymore. To give some feedback to the user, a messages is
161 issued each *waitfortex* seconds. The ``texipc`` flag indicates whether PyX
162 should use the ``--ipc`` option of TeX/LaTeX for immediate dvi file access to
163 increase the execution speed of certain operations. See the output of ``tex
164 --help`` whether the option is available at your TeX installation.
166 *texdebug* can be set to a filename to store the commands passed to TeX/LaTeX
167 for debugging. The flag *dvidebug* enables debugging output in the dvi parser
168 similar to ``dvitype``. *errordebug* controls the amount of information
169 returned, when an texmessage parser raises an error. Valid values are ``0``,
172 *pyxgraphics* allows use LaTeXs graphics package without further configuration
175 The TeX message parsers verify whether TeX/LaTeX could properly process its
176 input. By the parameters *texmessagesstart*, *texmessagesdocclass*,
177 *texmessagesbegindoc*, and *texmessagesend* you can set TeX message parsers to
178 be used then TeX/LaTeX is started, when the ``documentclass`` command is issued
179 (LaTeX only), when the ``\\begin{document}`` is sent, and when the TeX/LaTeX is
180 stopped, respectively. The lists of TeX message parsers are merged with the
181 following defaults: ``[texmessage.start]`` for *texmessagesstart*,
182 ``[texmessage.load]`` for *texmessagesdocclass*, ``[texmessage.load,
183 texmessage.noaux]`` for *texmessagesbegindoc*, and ``[texmessage.texend,
184 texmessage.fontwarning]`` for *texmessagesend*.
186 Similarily *texmessagesdefaultpreamble* and *texmessagesdefaultrun* take TeX
187 message parser to be merged to the TeX message parsers given in the
188 :meth:`preamble` and :meth:`text` methods. The *texmessagesdefaultpreamble* and
189 *texmessagesdefaultrun* are merged with ``[texmessage.load]`` and
190 ``[texmessage.loaddef, texmessage.graphicsload, texmessage.fontwarning,
191 texmessage.boxwarning]``, respectively.
193 :class:`texrunner` instances provides several methods to be called by the user:
196 .. method:: texrunner.set(**kwargs)
198 This method takes the same keyword arguments as the :class:`texrunner`
199 constructor. Its purpose is to reconfigure an already constructed
200 :class:`texrunner` instance. The most prominent use-case is to alter the
201 configuration of the default :class:`texrunner` instance ``defaulttexrunner``
202 which is created at the time of loading of the :mod:`text` module.
204 The ``set`` method fails, when a modification cannot be applied anymore (e.g.
205 TeX/LaTeX has already been started).
208 .. method:: texrunner.preamble(expr, texmessages=[])
210 The :meth:`preamble` can be called prior to the :meth:`text` method only or
211 after reseting a texrunner instance by :meth:`reset`. The *expr* is passed to
212 the TeX/LaTeX instance not encapsulated in a group. It should not generate any
213 output to the dvi file. In LaTeX preamble expressions are inserted prior to the
214 ``\\begin{document}`` and a typical use-case is to load packages by
215 ``\\usepackage``. Note, that you may use ``\\AtBeginDocument`` to postpone the
216 immediate evaluation.
218 *texmessages* are TeX message parsers to handle the output of TeX/LaTeX. They
219 are merged with the default TeX message parsers for the :meth:`preamble` method.
220 See the constructur description for details on the default TeX message parsers.
223 .. method:: texrunner.text(x, y, expr, textattrs=[], texmessages=[])
225 *x* and *y* are the position where a text should be typeset and *expr* is the
226 TeX/LaTeX expression to be passed to TeX/LaTeX.
228 *textattrs* is a list of TeX/LaTeX settings as described below, PyX
229 transformations, and PyX fill styles (like colors).
231 *texmessages* are TeX message parsers to handle the output of TeX/LaTeX. They
232 are merged with the default TeX message parsers for the :meth:`text` method. See
233 the constructur description for details on the default TeX message parsers.
235 The :meth:`text` method returns a :class:`textbox` instance, which is a special
236 :class:`canvas` instance. It has the methods :meth:`width`, :meth:`height`, and
237 :meth:`depth` to access the size of the text. Additionally the :meth:`marker`
238 method, which takes a string *s*, returns a position in the text, where the
239 expression ``\\PyXMarker{s}`` is contained in *expr*. You should not use ``@``
240 within your strings *s* to prevent name clashes with PyX internal macros
241 (although we don't the marker feature internally right now).
243 Note that for the outout generation and the marker access the TeX/LaTeX instance
244 must be terminated except when ``texipc`` is turned on. However, after such a
245 termination a new TeX/LaTeX instance is started when the :meth:`text` method is
249 .. method:: texrunner.reset(reinit=0)
251 This method can be used to manually force a restart of TeX/LaTeX. The flag
252 *reinit* will initialize the TeX/LaTeX by repeating the :meth:`preamble` calls.
253 New :meth:`set` and :meth:`preamble` calls are allowed when *reinit* was not set
261 TeX/LaTeX attributes are instances to be passed to a :class:`texrunner`\ s
262 :meth:`text` method. They stand for TeX/LaTeX expression fragments and handle
263 dependencies by proper ordering.
266 .. class:: halign(boxhalign, flushhalign)
268 Instances of this class set the horizontal alignment of a text box and the
269 contents of a text box to be left, center and right for *boxhalign* and
270 *flushhalign* being ``0``, ``0.5``, and ``1``. Other values are allowed as well,
271 although such an alignment seems quite unusual.
273 Note that there are two separate classes :class:`boxhalign` and
274 :class:`flushhalign` to set the alignment of the box and its contents
275 independently, but those helper classes can't be cleared independently from each
276 other. Some handy instances available as class members:
279 .. attribute:: halign.boxleft
281 Left alignment of the text box, *i.e.* sets *boxhalign* to ``0`` and doesn't set
285 .. attribute:: halign.boxcenter
287 Center alignment of the text box, *i.e.* sets *boxhalign* to ``0.5`` and doesn't
291 .. attribute:: halign.boxright
293 Right alignment of the text box, *i.e.* sets *boxhalign* to ``1`` and doesn't
297 .. attribute:: halign.flushleft
299 Left alignment of the content of the text box in a multiline box, *i.e.* sets
300 *flushhalign* to ``0`` and doesn't set *boxhalign*.
303 .. attribute:: halign.raggedright
305 Identical to :attr:`flushleft`.
308 .. attribute:: halign.flushcenter
310 Center alignment of the content of the text box in a multiline box, *i.e.* sets
311 *flushhalign* to ``0.5`` and doesn't set *boxhalign*.
314 .. attribute:: halign.raggedcenter
316 Identical to :attr:`flushcenter`.
319 .. attribute:: halign.flushright
321 Right alignment of the content of the text box in a multiline box, *i.e.* sets
322 *flushhalign* to ``1`` and doesn't set *boxhalign*.
325 .. attribute:: halign.raggedleft
327 Identical to :attr:`flushright`.
330 .. attribute:: halign.left
332 Combines :attr:`boxleft` and :attr:`flushleft`, *i.e.* ``halign(0, 0)``.
335 .. attribute:: halign.center
337 Combines :attr:`boxcenter` and :attr:`flushcenter`, *i.e.* ``halign(0.5, 0.5)``.
340 .. attribute:: halign.right
342 Combines :attr:`boxright` and :attr:`flushright`, *i.e.* ``halign(1, 1)``.
345 .. figure:: textvalign.*
351 .. class:: valign(valign)
353 Instances of this class set the vertical alignment of a text box to be top,
354 center and bottom for *valign* being ``0``, ``0.5``, and ``1``. Other values are
355 allowed as well, although such an alignment seems quite unusual. See the left
356 side of figure :ref:`fig_textvalign` for an example.
358 Some handy instances available as class members:
361 .. attribute:: valign.top
366 .. attribute:: valign.middle
371 .. attribute:: valign.bottom
376 .. attribute:: valign.baseline
378 Identical to clearing the vertical alignment by :attr:`clear` to emphasise that
379 a baseline alignment is not a box-related alignment. Baseline alignment is the
380 default, *i.e.* no valign is set by default.
383 .. class:: parbox(width, baseline=top)
385 Instances of this class create a box with a finite width, where the typesetter
386 creates multiple lines in. Note, that you can't create multiple lines in
387 TeX/LaTeX without specifying a box width. Since PyX doesn't know a box width, it
388 uses TeXs LR-mode by default, which will always put everything into a single
389 line. Since in a vertical box there are several baselines, you can specify the
390 baseline to be used by the optional *baseline* argument. You can set it to the
391 symbolic names :attr:`top`, :attr:`parbox.middle`, and :attr:`parbox.bottom`
392 only, which are members of :class:`valign`. See the right side of figure
393 :ref:`fig_textvalign` for an example.
395 Since you need to specify a box width no predefined instances are available as
399 .. class:: vshift(lowerratio, heightstr="0")
401 Instances of this class lower the output by *lowerratio* of the height of the
402 string *heigthstring*. Note, that you can apply several shifts to sum up the
403 shift result. However, there is still a :attr:`clear` class member to remove all
406 Some handy instances available as class members:
409 .. attribute:: vshift.bottomzero
411 ``vshift(0)`` (this doesn't shift at all)
414 .. attribute:: vshift.middlezero
419 .. attribute:: vshift.topzero
424 .. attribute:: vshift.mathaxis
426 This is a special vertical shift to lower the output by the height of the
427 mathematical axis. The mathematical axis is used by TeX for the vertical
428 alignment in mathematical expressions and is often usefull for vertical
429 alignment. The corresponding vertical shift is less than :attr:`middlezero` and
430 usually fits the height of the minus sign. (It is the height of the minus sign
431 in mathematical mode, since that's that the mathematical axis is all about.)
433 There is a TeX/LaTeX attribute to switch to TeXs math mode. The appropriate
434 instances ``mathmode`` and ``clearmathmode`` (to clear the math mode attribute)
435 are available at module level.
440 Enables TeXs mathematical mode in display style.
442 The :class:`size` class creates TeX/LaTeX attributes for changing the font size.
445 .. class:: size(sizeindex=None, sizename=None, sizelist=defaultsizelist)
447 LaTeX knows several commands to change the font size. The command names are
448 stored in the *sizelist*, which defaults to ``["normalsize", "large", "Large",
449 "LARGE", "huge", "Huge", None, "tiny", "scriptsize", "footnotesize", "small"]``.
451 You can either provide an index *sizeindex* to access an item in *sizelist* or
452 set the command name by *sizename*.
454 Instances for the LaTeXs default size change commands are available as class
458 .. attribute:: size.tiny
463 .. attribute:: size.scriptsize
468 .. attribute:: size.footnotesize
473 .. attribute:: size.small
478 .. attribute:: size.normalsize
483 .. attribute:: size.large
488 .. attribute:: size.Large
493 .. attribute:: size.LARGE
498 .. attribute:: size.huge
503 .. attribute:: size.Huge
507 There is a TeX/LaTeX attribute to create empty text boxes with the size of the
508 material passed in. The appropriate instances ``phantom`` and ``clearphantom``
509 (to clear the phantom attribute) are available at module level.
514 Skip the text in the box, but keep its size.
517 Using the graphics-bundle with LaTeX
518 ====================================
520 The packages in the LaTeX graphics bundle (``color.sty``, ``graphics.sty``,
521 ``graphicx.sty``, ...) make extensive use of ``\\special`` commands. PyX
522 defines a clean set of such commands to fit the needs of the LaTeX graphics
523 bundle. This is done via the ``pyx.def`` driver file, which tells the graphics
524 bundle about the syntax of the ``\\special`` commands as expected by PyX. You
525 can install the driver file ``pyx.def`` into your LaTeX search path and add the
526 content of both files ``color.cfg`` and ``graphics.cfg`` to your personal
527 configuration files\ [#]_. After you have installed the ``cfg`` files, please
528 use the :mod:`text` module with unset ``pyxgraphics`` keyword argument which
529 will switch off a convenience hack for less experienced LaTeX users. You can
530 then import the LaTeX graphics bundle packages and related packages (e.g.
531 ``rotating``, ...) with the option ``pyx``, e.g.
532 ``\\usepackage[pyx]{color,graphicx}``. Note that the option ``pyx`` is only
533 available with unset *pyxgraphics* keyword argument and a properly installed
534 driver file. Otherwise, omit the specification of a driver when loading the
537 When you define colors in LaTeX via one of the color models ``gray``, ``cmyk``,
538 ``rgb``, ``RGB``, ``hsb``, then PyX will use the corresponding values (one to
539 four real numbers). In case you use any of the ``named`` colors in LaTeX, PyX
540 will use the corresponding predefined color (see module ``color`` and the color
541 table at the end of the manual). The additional LaTeX color model ``pyx`` allows
542 to use a PyX color expression, such as ``color.cmyk(0,0,0,0)`` directly in
543 LaTeX. It is passed to PyX.
545 When importing Encapsulated PostScript files (``eps`` files) PyX will rotate,
546 scale and clip your file like you expect it. Other graphic formats can not be
547 imported via the graphics package at the moment.
549 For reference purpose, the following specials can be handled by PyX at the
552 ``PyX:color_begin (model) (spec)``
553 starts a color. ``(model)`` is one of ``gray``, ``cmyk``, ``rgb``, ``hsb``,
554 ``texnamed``, or ``pyxcolor``. ``(spec)`` depends on the model: a name or some
560 ``PyX:epsinclude file= llx= lly= urx= ury= width= height= clip=0/1``
561 includes an Encapsulated PostScript file (``eps`` files). The values of ``llx``
562 to ``ury`` are in the files' coordinate system and specify the part of the
563 graphics that should become the specified ``width`` and ``height`` in the
564 outcome. The graphics may be clipped. The last three parameters are optional.
566 ``PyX:scale_begin (x) (y)``
567 begins scaling from the current point.
572 ``PyX:rotate_begin (angle)``
573 begins rotation around the current point.
579 The :attr:`defaulttexrunner` instance
580 =====================================
583 .. data:: defaulttexrunner
585 The ``defaulttexrunner`` is an instance of :class:`texrunner`. It is created
586 when the :mod:`text` module is loaded and it is used as the default texrunner
587 instance by all :class:`canvas` instances to implement its :meth:`text` method.
590 .. function:: preamble(...)
592 ``defaulttexrunner.preamble``
595 .. function:: text(...)
597 ``defaulttexrunner.text``
600 .. function:: set(...)
602 ``defaulttexrunner.set``
605 .. function:: reset(...)
607 ``defaulttexrunner.reset``
610 Some internals on temporary files
611 =================================
613 It is not totally obvious how TeX processes are supervised by PyX and why it's
614 done that way. However there are good reasons for it and the following
615 description is intended for people wanting and/or needing to understand how
616 temporary files are used by PyX. All others don't need to care.
618 Each time PyX needs to start a new TeX process, it creates a base file name for
619 temporary files associated with this process. This file name is used as
620 ``\jobname`` by TeX. Since TeX does not handle directory names as part of
621 ``\jobname``, the temporary files will be created in the current directory. The
622 PyX developers decided to not change the current directory at all, avoiding all
623 kind of issues with accessing files in the local directory, like for loading
624 graph data, LaTeX style files etc.
626 PyX creates a TeX file containing ``\relax`` only. It's only use is to set TeXs
627 ``\jobname``. Immediately after processing ``\relax`` TeX falls back to stdin to
628 read more commands. PyX than uses ``stdin`` and ``stdout`` to avoid various
629 buffering issues which would occur when using files (or named pipes). By that
630 PyX can fetch TeX errors as soon as they occur while keeping the TeX process
631 running (i.e. in a waiting state) for further input. The size of the TeX output
632 is also availble immediately without fetching the ``dvi`` file created by TeX,
633 since PyX uses some TeX macros to output the extents of the boxes created for
634 the requested texts to ``stdout`` immediately. There is a TeX hack ``--ipc``
635 which PyX knows to take advantage of to fetch informations from the ``dvi`` file
636 immediately as well, but it's not available on all TeXinstallations. Thus this
637 feature is disabled by default and fetching informations from the ``dvi`` is
638 tried to be limited to those cases, where no other option exists. By that TeX
639 usually doesn't need to be started several times.
641 By default PyX will clean up all temporary files after TeX was stopped. However
642 the ``usefiles`` list allows for a renaming of the files from (and to, if
643 existing) the temporary ``\jobname`` (+ suffix) handled by PyX. Additionally,
644 since PyX does not write a useful TeX input file in a file and thus a
645 ``usefiles=["example.tex"]`` would not contain the code actually passed to TeX,
646 the ``texdebug`` feature of the texrunner can be used instead to get a the full
649 In case you need to control the position where the temporary files are created
650 (say, you're working on a read-only directory), the suggested solution is to
651 switch the current directory before starting with text processing in PyX (i.e.
652 an ``os.chdir`` at the beginning of your script will do fine). You than just
653 need to take care of specifying full paths when accessing data from your
654 original working directory, but that's intended and necessary for that case.
669 .. rubric:: Footnotes
671 .. [#] https://en.wikipedia.org/wiki/TeX
673 .. [#] https://en.wikipedia.org/wiki/Device_independent_file_format
675 .. [#] If you do not know what this is all about, you can just ignore this
676 paragraph. But be sure that the *pyxgraphics* keyword argument is always