add UnicodeEngine (MultiEngineText and axis texters returning MultiEngineText), texte...
[PyX.git] / faq / tex_latex.rst
blob0517acced6d7c4c699ade2137582acedac29144e
1 =============
2 TeX and LaTeX
3 =============
5 General aspects
6 ===============
8 .. _what_is_tex:
10 What is TeX/LaTeX and why do I need it?
11 ---------------------------------------
13 TeX is a high quality typesetting system developed by Donald E. Knuth which is
14 available for a wide variety of operating systems. LaTeX is a macro package
15 originally developed by Leslie Lamport which makes life with TeX easier, in
16 particular for complex typesetting tasks. The current version of LaTeX is
17 referred to as LaTeX2e and offers e.g. improved font selection as compared to
18 the long outdated LaTeX 2.09 which should no longer be used. 
20 All typesetting tasks in PyX are ultimately handed over to TeX (which is the
21 default) or LaTeX, so that PyX cannot do without it. On the other hand, the
22 capabilities of TeX and LaTeX can be used for complex tasks where both graphics
23 and typesetting are needed.
25 .. _intro_tex_latex:
27 I don't know anything about TeX and LaTeX. Where can I read something about it?
28 --------------------------------------------------------------------------------
30 Take a look at CTAN (cf. :ref:`ctan`) where in `CTAN:info
31 <http://www.ctan.org/tex-archive/info/>`_ you may be able to find some useful
32 information. There exists for example “A Gentle Introduction to TeX” by M. Doob
33 (`CTAN:gentle/gentle.pdf <http://www.ctan.org/tex-archive/gentle/gentle.pdf>`_)
34 and “The Not So Short Introduction to LaTeX2e”
35 (`CTAN:info/lshort/english/lshort.pdf
36 <http:www.ctan.org/tex-archive/info/lshort/english/lshort.pdf>`_) by T. Oetiker
37 et al. The latter has been translated into a variety of languages among them
38 korean (which you will not be able to read unless you have appropriate fonts
39 installed) and mongolian.
41 Of course, it is likely that these documents will go way beyond what you will
42 need for generating graphics with PyX so you don't have to read all of it
43 (unless you want to use TeX or LaTeX for typesetting which can be highly
44 recommended). 
46 There exists also a number of FAQs on TeX at `CTAN:help <http://www.ctan.org/tex-archive/help>`_.
48 .. _ctan:
50 What is CTAN?
51 -------------
53 CTAN is the *Comprehensive TeX Archive Network* where you will find almost
54 everything related to TeX and friends. The main CTAN server is `www.ctan.org
55 <http://www.ctan.org>`_ but there exists a large number of mirrors around the
56 world. You can help to reduce the load on the main server by using
57 `mirror.ctan.org <http://mirror.ctan.org>`_ which will redirect you to a mirror
58 nearby. A list of known mirrors is available at
59 `http://mirror.ctan.org/README.mirrors
60 <http://mirror.ctan.org/README.mirrors>`_.
62 In this FAQ, ``CTAN:`` refers to the root of the CTAN tree, e.g.
63 `http://www.ctan.org/tex-archive/ <http://www.ctan.org/tex-archive/>`_.  The
64 links to CTAN in this document point to the main server but you might consider
65 using a server closer to you in order to reduce traffic load.
67 Is there support for ConTeXt?
68 -----------------------------
70 No, and as far as I know there no plans to provide it in the near future.
71 Given the close ties between ConTeXt and MetaPost, ConTeXt users probably
72 prefer to stick with the latter anyway.
74 TeX and LaTeX commands useful for PyX
75 =====================================
77 How do I get a specific symbol with TeX or LaTeX?
78 -------------------------------------------------
80 A list of mathematical symbols together with the appropriate command name can
81 be found at `CTAN:info/symbols/math/symbols.pdf
82 <http://www.ctan.org/tex-archive/info/symbols/math/symbols.pdf>`_. A
83 comprehensive list containing almost 6000 symbols for use with LaTeX can be
84 obtained from `CTAN:info/symbols/comprehensive/symbols-a4.pdf
85 <http://www.ctan.org/tex-archive/info/symbols/comprehensive/symbols-a4.pdf>`_.
86 In some cases it might be necessary to install fonts or packages available from
87 CTAN (cf. :ref:`ctan`).
89 TeX and LaTeX errors
90 ====================
92 .. _undefined_usepackage:
94 Undefined control sequence ``\usepackage``
95 ------------------------------------------
97 The command ``\usepackage`` is specific to LaTeX. Since by default PyX
98 uses TeX, you have to specify the correct mode::
100    text.set(mode="latex")
102 Undefined control sequence ``\frac``
104 The command ``\frac`` is only available in LaTeX. The equivalent to
105 ``\frac{a}{b}`` in TeX is ``{a \over b}``.  As an alternative you may ask for
106 the LaTeX mode as explained in :ref:`undefined_usepackage`.
108 Missing ``$`` inserted
109 ----------------------
111 You have specified TeX- or LaTeX-code which is only valid in math mode. 
112 Typical examples are greek symbols, sub- and superscripts or fractions. 
114 On the PyX level, you can specify math mode for the whole string by using
115 ``text.mathmode`` as in ::
117    c.text(0, 0, r"{\alpha}", text.mathmode)
119 Keep also in mind that the standard Python interpretation of the backslash as 
120 introducing escape sequences needs to be prevented.
122 On the TeX/LaTeX level you should enclose the commands requiring math mode in
123 ``$``'s. As an example, ``$\alpha_i^j$`` will produce a greek letter alpha with
124 a subscript i and a superscript j.  The dollar sign thus allows you to specify
125 math mode also for substrings. There exist other ways to specify math mode in
126 TeX and LaTeX which are particularly useful for more complex typesetting tasks.
127 To learn more about it, you should consult the documentation
128 :ref:`intro_tex_latex`. 
130 Why do environments like ``itemize`` or ``eqnarray`` seem not to work?
131 ----------------------------------------------------------------------
133 An itemize environment might result in a LaTeX error complaining about a
134 ``missing \item`` or an eqnarray might lead to a LaTeX message ``missing
135 \endgroup inserted`` even though the syntax appears to be correct. The TeXnical
136 reason is that in PyX text is typeset in left-right mode (LR mode) which does
137 not allow linebreaks to occur. There are two ways out.
139 If the text material should go in a box of given width, a parbox can be used
140 like in the following example::
142    text.set(mode="latex")
143    c = canvas.canvas()
144    w = 2
145    c.text(0, 0, r"\begin{itemize}\item a\item b\end{itemize}", [text.parbox(w)])
147 Occasionally, one would like to have the box in which the text appears to be as
148 small as possible. Then the ``fancybox`` package developed by Timothy Van Zandt
149 is useful which provides several environments like ``Bitemize`` and
150 ``Beqnarray`` which can be processed in LR mode. The relevant part of the code
151 could look like::
153    text.set(mode="latex")
154    text.preamble(r"\usepackage{fancybox}")
155    c = canvas.canvas()
156    c.text(0, 0, r"\begin{Bitemize}\item a\item b\end{Bitemize}")
158 Other environments provided by the ``fancybox`` package include ``Bcenter``,
159 ``Bflushleft``, ``Bflushright``, ``Benumerate``, and ``Bdescription``. For more
160 details, the documentation of the package should be consulted.
162 .. _fontshape_undefined:
164 Font shape ``OT1/xyz/m/n`` undefined
165 ------------------------------------
167 You have asked to use font ``xyz`` which is not available. Make sure that you
168 have this font available in Type1 format, i.e. there should be a file
169 ``xyz.pfb`` somewhere. If your TeX system is TDS compliant (TDS=TeX directory
170 structure, cf. `CTAN:tds/draft-standard/tds/tds.pdf
171 <http://www.ctan.org/tex-archive/tds/draft-standard/tds/tds.pdf>`_) you should
172 take a look at the subdirectories of ``$TEXMF/fonts/type1``.
174 File ``…`` is not available or not readable
175 -------------------------------------------
177 Such an error message might already occur when running the example file
178 ``hello.py`` included in the PyX distribution. Usually, the error occurs due to
179 an overly restrictive umask setting applied when unpacking the ``tar.gz``
180 sources. This may render the file mentioned in the error message unreadable
181 because the python distutil installation package doesn't change the file
182 permissions back to readable for everyone. 
184 If the file exists, the problem can be solved by changing the permissions to 
185 allow read access.
187 No information for font ``cmr10`` found in font mapping file
188 ------------------------------------------------------------
190 Such an error message can already be encountered by simply running the example
191 file ``hello.py`` included in the PyX distribution. The likely reason is that
192 the TeX system does not find the cmr fonts in Type1 format.  PyX depends on
193 these fonts as it does not work with the traditional pk fonts which are stored
194 as bitmaps.
196 Therefore, the first thing to make sure is that the cmr Type1 fonts are
197 installed. In some TeX installations, the command ``kpsewhich cmr10.pfb`` will
198 return the appropriate path if the cmr fonts exist in the binary Type1 format
199 (extension ``pfb``) required by PyX. If the command does not work but the TeX
200 system is TDS compliant (:ref:`fontshape_undefined`), a look should be taken at
201 ``$TEXMF/fonts/type1/bluesky/cm`` where ``$TEXMF`` is the root of the ``texmf``
202 tree.
204 If the Type1 fonts do not exist on the system, they may be obtained from the
205 CTAN (cf. :ref:`ctan`) at `CTAN:fonts/cm/ps-type1/bluesky
206 <http://www.ctan.org/tex-archive/fonts/cm/ps-type1/bluesky>`_). See the
207 ``README`` for information about who produced these fonts and why they are
208 freely available.
210 If the Type1 fonts exist, the next step is to take a look at ``psfonts.map``.
211 There may be several files with this name on the system, so it is important to
212 find out which one TeX is actually using.  ``kpsewhich psfonts.map`` might give
213 this information.
215 The most likely problem is that this file does not contain a line telling TeX
216 what to do if it encounters a request for font ``cmr10``, i.e. the following
217 line may be missing ::
219    cmr10           CMR10           <cmr10.pfb
221 It is probable that the required lines (in practice, you do not just need
222 ``cmr10``) are found in a file named ``psfonts.cmz`` which resides in
223 ``$TEXMF/dvips/bluesky``. 
225 One solution is to instruct PyX to read additional map files like
226 ``psfonts.cmz`` or ``psfonts.amz``. This can be achieved by modifying the
227 appropriate ``pyxrc`` file which is either the systemwide ``/etc/pyxrc`` or
228 ``.pyxrc`` in the user's home directory. Here, the names of the map files to be
229 read by PyX should be appended separated by whitespaces like in the following
230 example::
232    [text]
233    fontmaps = psfonts.map psfonts.cmz psfonts.amz
235 The same effect can be achieved by inserting the following line into the
236 PyX code::
238    text.set(fontmaps="psfonts.map psfonts.cmz psfonts.amz")
240 Note that the default map (``psfonts.map``) has to be specified explicitly.
242 An alternative approach consists in modifying the TeX installation by inserting
243 the contents of the desired map files like ``psfonts.cmz`` into
244 ``psfonts.map``. Probably, ``psfonts.map`` recommends not to do this by hand.
245 In this case the instructions given in the file should be followed.  Otherwise,
246 ``psfonts.cmz`` should be copied into ``psfonts.map`` while keeping a backup of
247 the old ``psfonts.map`` just in case. After these changes, PyX most likely will
248 be happy. When inserting ``psfonts.cmz`` into ``psfonts.map`` it may be a good
249 idea to include ``psfonts.amz`` as well. ``psfonts.amz`` contains information
250 about some more fonts which might be needed at some point. Making these changes
251 to ``psfonts.map`` will imply that the TeX system will use the cmr fonts in
252 Type1 format instead of pk format which is actually not a bad thing, in
253 particular if ``latex / dvips / ps2pdf`` is used to generate PDF output. With
254 fonts in pk format this will look ugly and using Type1 fonts solves this
255 problem as well. When ``pdflatex`` is used to create PDF files, Type1 fonts
256 will be used anyway.
258 Fonts
259 =====
261 I have Type1 fonts in ``pfa`` format. How do I obtain the corresponding ``pfb`` files needed by PyX?
262 ----------------------------------------------------------------------------------------------------
264 .. todo:
266    still needs to be answered
268 .. _other_font:
270 I want to use a font other than computer modern roman
271 -----------------------------------------------------
273 As long as you have a font in Type1 format available, this should be no
274 problem (even though it may cost you some time to set up things properly).
276 In the simplest case, your LaTeX system contains everything needed. 
277 Including the following line into your code will probably work::
279     text.set(mode="latex")
280     text.preamble(r"\usepackage{mathptmx}")
282 and give you Times as roman font. 
284 If you own one of the more common commercial fonts, take a look at `CTAN:fonts
285 <http://www.ctan.org/tex-archive/fonts/>`_ and its subdirectories as well as at
286 the web page `http://home.vr-web.de/was/fonts.html
287 <http://home.vr-web.de/was/fonts.html>`_ of Walter Schmidt. It is not unlikely
288 that somebody has already done most of the work for you and created the files
289 needed for the font to work properly with LaTeX. But remember: we are talking
290 about commercial fonts here, so do not expect to find the fonts themselves for
291 free.
293 If none of these cases applies, you should spend some time reading manuals
294 about font installation, e.g. `CTAN:macros/latex/doc/fntguide.pdf
295 <http://www.ctan.org/tex-archive/macros/latex/doc/fntguide.pdf>`_ (of course, I
296 do not expect font wizards to read the last few lines).
298 Can I use a TrueType font with PyX?
299 -----------------------------------
301 Not directly as PyX only knows how to handle Type1 fonts (although it is
302 possible to get LaTeX to work with TrueType fonts). However, you may use
303 ``ttf2pt1`` (from `ttf2pt1.sourceforge.net <http://ttf2pt1.sourceforge.net>`_)
304 to convert a TrueType font into a Type1 font which you then install in your TeX
305 system (cf. :ref:`other_font`). You will loose hinting information in the
306 conversion process but this should not really matter on output devices with not
307 too low resolution.