L^{A}TEX: Tips and Tricks

May 6, 2004

1 books

• George Grätzer: Math Into L^{A}TEX, Birkhäuser-Springer, 3rd ed. This is the book I personally like best. It is clearly written, with the mathematician in mind. While other books have a lot of material on how to draw pictures in L^{A}TEX, this one doesn’t: the author thinks one should use a proper drawing program for that. I agree.

• Helmut Kopka and Patrick W. Daly: Guide to L^{A}TEX(4th Edition). The 4th edition just came out. Another very nice book. The reason why I like it a little less than the previous one is that it treats the AMS packages separately in a (small) Appendix (3rd ed). This is somewhat better in the new edition.

• Frank Mittelbach and Michel Goossens: The L^{A}TEXCompanion (2nd Edition). This just came out a week ago. If you absolutely definitely want to know everything, this book is for you. It doesn’t tell how to use a float, it tells you how to write your own float environment. Where other books tell you what styles you can use for BibTEX, this one tells you how to write your own. Not recommended as a quick reference.

• Apostolos Syropoulos, Antonis Tsolomitis, Nick Sofroniou: Digital Typography Using L^{A}TEX. I quite like this book. Difficulty between the Companion and the other two. Quite well organized. It’s a little weird though. For example it describes 5 different Mongolian languages, also Inuktitut, no Chinese though. This book will be available online in the library soon.

Of course there are numerous websites out there. I assume that you can use Google.

2 General

Use the AMS packages. They are almost always superior to their plain counterparts. Put

\usepackage{amssymb,amsmath,amsthm}

in the preamble. I find align the most versatile for displayed math. The AMS theorem package is by far the best. Also amsart looks much nicer than article.

Define your own macros. For example

% complex numbers
\newcommand{\C}{\mathbb{C}}

So you just have to type $\C$ to get ℂ. Also if you want to change it to say C, you just have to change

% complex numbers
\newcommand{\C}{\mathbf{C}}

I have a whole file of such definitions that I use over and over again. It’s loaded to the document (in the preamble) with

\include{definitionen}

Then I have a second file with notation that is specific to the document I’m working on.

3 Pictures in L^{A}TEX

This used to cause me the biggest headache. In fact, when somebody told me how to do it (why didn’t anybody tell me 5 years ealier ...) I had the idea of giving this talk. It is easy enough to put pictures in L^{A}TEX, but one wants to label them with correctly. Two methods I find useful.

Use Xfig. This is a bad program, it will drive you insane. But: it exports to L^{A}TEX nicely. Draw your diagram, label it with “TEXT” in L^{A}TEX. Be sure to set the SPECIAL FLAG first. Go to File-Export. Select Combined PS/LaTeX or Combined PDF/LaTeX (when using pdflatex. As of this writing the version on zeno does not export to pdf. I assume if some people write an email (help@math.) this might change.

Exporting will produce two files: *.pstex and *.pstex_t, or *.pdftex and *.pdftex_t. Put the *_t file in your latex source. This is one example for such a file.

\begin{picture}(0,0)%
\includegraphics{PQdef.pstex}%
\end{picture}%
\setlength{\unitlength}{3947sp}%
%
\begingroup\makeatletter\ifx\SetFigFont\undefined%
\gdef\SetFigFont#1#2#3#4#5{%
  \reset@font\fontsize{#1}{#2pt}%
  \fontfamily{#3}\fontseries{#4}\fontshape{#5}%

  \selectfont}%
\fi\endgroup%
\begin{picture}(3528,3204)(412,-2818)
\put(1108,187){\makebox(0,0)[lb]{\smash{{\SetFigFont{14}{16.8}
{\rmdefault}{\mddefault}{\updefault}{\color[rgb]{1,0,0}
$\{\dist_{\infty}(x,Q)=\epsilon\}$}%
}}}}
\put(1839,-1636){\makebox(0,0)[lb]{\smash{{\SetFigFont{14}{16.8}
{\rmdefault}{\mddefault}{\updefault}{\color[rgb]{0,0,1}$Q$}%
}}}}
\end{picture}%

You can usually easily find the lines which actually put the L^{A}TEXlabels into the picture. So you can easily change the labels. In the above example the original picture is in color. You can add stuff like captions, figure or centering to it. The above produces (admittedly not very impressive)

\begin{matrix}\{dist_{∞}(x, Q) = ϵ\} \\ Q \end{matrix}

All dvi and pdf viewers that I know don’t show L^{A}TEX-labels in the right color. If you print them they will however have the right color.

Be sure to have the pictures in the right size. If you want to use \scalebox you have to remove \SetFigFont. However your labels might move into the picture.

Some hints for “enjoying” Xfig. Use layers. The layer 40 will be in front the layer 50. It is usually a good idea to put labels on a separate layer in front. If you rescale things in Xfig, it tends to forget attributes. Use update.

The second possibility to put L^{A}TEX tags into a picture is the package psfrag. Put

% in the preamble
\usepackage{psfrag,graphics}

in the preamble. Draw your graphic with any program you like that can generate .eps as output (almost all can). Put simple tags (like a,b,c,...) for the places where you want to label your picture. It’s supposed to be able to handle more complicated names, most of the time. Save as *.eps. Add

 % where you want to place the picture
\psfrag{a}{$\mathcal{S}_0$}
\psfrag{b}{$\mathcal{S}_1$}
\psfrag{c}{$\mathcal{S}_2$}
\psfrag{d}{$\mathcal{S}_3$}
\includegraphics{name.eps}

This will replace your tags a, b, c, d with S_{0}, S_{1}, S_{2}, and S_{3}. You’ll get

PSfrag replacements S_{0} S_{1} S_{2} S_{3}

Your dvi viewer will not show the replacements correctly. It will instead put a list called “replacements” on the side. It will print out correctly though. You can use tags globally. \psfrag at beginning will replace all eps pictures in the document. Otherwise put your \psfrag’s in an (any) environment. You can do more stuff with the package.

   \psfrag{tag}[][][scale][rotation]{replacement}

PSfrag replacements S_{0} S_{1} S_{2} S_{3}

4 BibTEX

Produces bibliographies. Place your citations in a file (i. e. lit.bib). Put

before \end{document}
\bibliographystyle{alpha} % can use other styles
\bibliography{lit}

The best way to get the citations is from the AMS website. Go to MathSciNet Search, to find your articles.

Add to Clipboard,
View clipboard,
Select format: Citations (BibTeX),
View all

Enjoy! Bibweb by John Palmieri does this automatically.

The citaions you get this way have more entries that you want (like the name of the reviewer). Have to delete those. Careful with accents. Often they are not right. The correct sequence to resolve all citations and references is:

latex file
bibtex file
latex file
latex file

I wrote a small script to do all four steps in one

#!/bin/sh
#
# this shell script executes latex bibtex latex latex
# on a file to resolve all references and citations
#

latex $1
bibtex $1
latex $1
latex $1

I called this script lll, put it in ~/bin. Make this executable

chmod u+x lll

Then lll file runs the four commands.

5 emacs

Settings of emacs are saved in ~/.emacs.

Possible options are comparable to the number of particles in the known universe. Some things I find useful:

;; turn on font-lock mode
(global-font-lock-mode t)
;; enable visual feedback on selections
(setq-default transient-mark-mode t)
;; make it colorfull
(setq font-lock-maximum-decoration t)
;; show matching parenthesis
(show-paren-mode t)

6 RefTEX

Enhancement for referencing in emacs. I like it very much. It is already part of emacs. You have to activate it though. Add to .emacs

; use in emacs LaTeX mode
(add-hook 'latex-mode-hook 'turn-on-reftex)
; with AUCTeX
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)

See the later section for AUCTEX. By typing C-c ) a window will open which lets you select from the labels you have already defined. It is context sensitive, when you type by equation C-c ) it will only offer equations (meant in a general sense, equations, align, etc. ). If you type by C-c ) you can either get all labels (space) or only certain types (e : equations, f: figures, ...). Some more commands.

• C-c ) Add a reference.

• C-c ( Add label.

• C-c = toc navigate around.

• C-c [ Add citation.

• C-c / Create Index Entry. All these commands are also available from a menu as well.

7 Multifile Documents

Long documents (such as a thesis) should be broken up into several files.

\include{frontmatter}
\include{glossary}
\include{acknoledgments}
\include{intro}
\include{snowdefs}
...

To tell emacs what the master file is put the following at the end of each file (for plain emacs latex mode)

%%% Local Variables:
%%% mode: latex
%%% tex-main-file: ``main.tex''
%%% End:

For AUCTEX

%%% Local Variables:
%%% mode: latex
%%% TeX-master: ``main.tex''
%%% End:

If you add the following to .emacs

;ask for master-file
(setq-default TeX-master nil)

it will always ask you for the master file when you create a new file. It will add the above lines. This only works for AUCTEX.

Referencing as before will then work over all files of the document. It will also enable you to jump to any part in any file of your document.

8 AUCTEX

This is an enhanced L^{A}TEXmode for emacs. By now a lot of funtionality is already in standard latex mode, it’s still nice. Activate by adding to .emacs

;; on zeno
(setq load-path (cons "/usr/share/xemacs
/xemacs-packages/lisp/auctex" load-path))
;; activate AUCTeX
(require 'tex-site)

This is what I got from help@math. Hope it works, didn’t try it out. Some useful commands

   • C-c C-e: enters \begin{} \end{}, ask for name.

• C-u C-c C-e: changes environment.

• C-c ; comments region out.

• C-u C-c ; uncomments region.

• C-c C-c: runs command, default LaTeX.

• M-TAB: auto-completion.

• C-c ~: enter math mode.

In math mode ` + key: will give you a math command. For example ` s: gives \sigma σ, ` 0: gives \emptyset ∅, ` {: gives \subset ⊂ and so on. You can always have math mode on with (in .emacs)

; always activate math mode in AUCTeX
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode

The commands with C-c C-c are run on the master file.

More stuff from my .emacs

; integrate RefTeX with AUCTeX
(setq reftex-plug-into-AUCTeX t)
; add new environments for ReTReX
(setq reftex-label-alist
      '(("theorem" ?h "thm:" "~\\ref{%s}" t ("theorem" "th.") -3)
        ("lemma"   ?l "lem:" "~\\ref{%s}" t ("lemma" "lem.") -3)))
; enable new environments in AUCTeX
(add-hook 'LaTeX-mode-hook
          (lambda ()
            (LaTeX-add-environments
             '("theorem" LaTeX-env-label)
             '("lemma" LaTeX-env-label))))

The last two things tell RefTEXand AUCTEXabout my theorem styles “theorem” and “lemma”.

9 XY-pic, Commutative Diagrams

Most people seem to agree that the best package for commutative diagrams is the XY-pic package. To use it you have to put

   \usepackage[matrix,arrow,curve,cmtip]{xy}

in your document. I got the following from John Palmieri.

  \xymatrix{A \ar[rrr]^{f} \ar[dd]_{g}
  & & & B \ar[dd]^{h} \\
  & & {} \ar@{=>}[dr]\\
  C \ar[rrr]_{i}
  & & & D}

produces this:

[figure: A; f; g; B; h; C; i; D]

This is a diagram with 4 columns and 3 rows, with the double arrow coming from the blank spot in position (2,3). By default, the xy package allocates a certain amount of space to each spot in the diagram, so this is rather large. Changing the beginning to

  \xymatrix@=0.9em{A \ar[rrr]^{f} \ar[dd]_{g} ...

changes the spacing of the diagram, and produces this:

[figure: A; f; g; B; h; C; i; D]

\xymatrix{
\ext{H^{1}} \\
\ext{A} \ar[u] \ar[r] \ar@{=}[d] &
\ext{A} / \text{subgp}\langle z-\Sq^{0}z \rangle \ar[ul] \ar[r]
\ar[d]^{\cong ?} &
\ext{A} / \text{ideal}\langle z-\Sq^{0}z \rangle \ar[ull]_{\text{not}
\ \cong} \ar[d]^{\text{not} \ \cong} \\
H^{*}(\Lambda) \ar[r] &
H^{*}(\Lambda / \text{subgp}\langle x - \theta x \rangle ) \ar[r] &
H^{*}(\Lambda / \text{ideal}\langle x - \theta x \rangle )
}

produces this:

[figure: Ext_{H^{1}}^{∗}(F_{2}, F_{2}); Ext_{A}^{∗}(F_{2}, F_{2}); Ext_{A}^{∗}(F_{2}, F_{2})/subgp⟨z − Sq^{0} z⟩; ≅?; Ext_{A}^{∗}(F_{2}, F_{2})/ideal⟨z − Sq^{0} z⟩; _{_{not} ≅}; _{_{not} ≅}; H^{∗}(Λ); H^{∗}(Λ/subgp⟨x − θx⟩); H^{∗}(Λ/ideal⟨x − θx⟩)]

From the XY-pic guide:

\xymatrix{
\circ \ar `r[d]^{a} `[rr]^{b} `/4pt[rr]^{c} `[rrr]^{d}
`_dl[drrr]^{e} [drrr]^{f} & \circ & \circ & \circ \\
\circ & \circ & \circ & \circ }

produces this:

[figure: ∘; a; b; c; d; e; f; ∘; ∘; ∘; ∘; ∘; ∘; ∘]

From the XY-pic guide:

\xymatrix@1{
A \ar@<-2pt> `d[r] `[r] [r]
\ar@<+2pt> `d[r] `[r] [r] & B }

produces this:

[figure: A; B]

10 Creating pdf-files

pdflatex file generates a pdf-file in one step. Use:

\usepackage{pslatex}  % produces better output
% use following for clickable links
\usepackage[colorlinks=true,linkcolor=black]
    {hyperref}

Can’t use eps-files for graphics. Have to convert.

I got different suggesions for converting to .pdf. Try out for yourself what works best.

latex file
dvips -Pcmz -Pamz -D7200 file.dvi -o file.ps
ps2pdf -dEmbedAllFonts file.ps

Or

dvips -Ppdf -f -G0 filename.dvi > filename.ps

11 Fonts substitutions

Some people dislike standard L^{A}TEXfonts. Can change fonts, but might open can of worms. All different fonts have to match. The following two commands replace text and math fonts simultaneously. By

   \usepackage{mathpazo,bm}

one gets the Palatino font (including bold) in math and text. By

   \usepackage{mathptmx}

get Times in text and math. Careful: not all symbols in bold are available. Get this script font

𝒜 ℬ𝒞 𝒟ℰ ℱ 𝒢

12 Misc Math

Don’t write |a|, L^{A}TEXdoesn’t know whether its left or right. Similar for ∥. Put

\providecommand{\abs}[1]{\lvert#1\rvert}
\providecommand{\norm}[1]{\lVert#1\rVert}
then $abs\{z}$ gives |z| and $\norm{v}$ ∥v∥.

13 More Stuff

Periods: use .\␣ for Abbreviations like etc.\␣.

Use tilde ~ for nonbreakable spaces like i.~e.\␣.

L^{A}TEXknows three dashes:

• - for hyphen, like single-valued.

• -- for number ranges, like pages 12–25.

• --- em dash, like blablabla—and now something completely different.