LaTeX notes
Contents
- Useful references
- Making nice PDFs (including from old .ps files)
- Making small PDF files
- Spacing issues
- Italic issues
- More spacing issues — hitting page limits
- Recommended packages
- hyperref problems
- Upside-down slides
- Centering stuff vertically within a line
- Gaps to match
- Fonts
- Embedding LaTeX text in diagrams
Useful references
- When I was first learning LaTeX, I used a version of this hypertext reference a lot. You can download a .tar.gz archive of the files.
- This TeX FAQ is good.
- For fancy math stuff with LaTeX see the AMS documentation.
- A comprehensive review of slide-making packages (last updated 2005). Warning: huge time sink. Simple might be better.
- The TeXbook is great; see details on Knuth’s webpage or look through the source of the book.
- Kopka and Daly’s Guide to LaTeX looks like one of the better LaTeX books. But none I’ve seen look as fun or interesting as the TeXbook.
- This Plain TeX reference card is largely compatible with LaTeX.
- TeX - LaTeX - Stack Exchange.
Making nice PDFs (including from old .ps files)
If possible, use pdflatex over latex for hassle-free PDF generation. Include \usepackage{microtype} and get more sophisticated typesetting for free.
For normal latex, the newest recommended tool for making pdf seems to be dvipdfmx, however that won’t work with all packages. The switches I used to use with dvips are below. These avoid bitmap fonts, ensure ligatures don’t turn into pound-sterling symbols and force the page size to A4.
#!/bin/sh dvips -Ppdf -G0 -o "${1%.dvi}"_pdf.ps "$1" ; ps2pdf -sPAPERSIZE=a4 "${1%.dvi}"_pdf.ps "${1%.dvi}".pdf;
You may find that the above mangles some of your colour figures, as it might jpeg compress them (sometimes aggressively). The following switches to ps2pdf:
-dAutoFilterColorImages=false -sColorImageFilter=FlateEncode
losslessly compresses your images. You may experience an increase in file size.
What if you no longer have the .tex or .dvi source? If you have a document made with an old version of tex/latex, it is still possible to make a decent .pdf as long as you have a .ps file made by dvips:
pkfix-helper input.ps tmp.ps pkfix tmp.ps output.ps
output.ps will now have Type 1 fonts, and can be converted to a good PDF with ps2pdf. The pkfix-helper man page says how to check and tweak the font substitutions.
Making small PDF files
pdfsizeopt is a tool that can rewrite graphics, compress your PDF stream, and dramatically cut down the overhead of your fonts. Although it’s sensible to compress graphics once, before TeXing. It’s pdfsizeopt’s font handling I find really useful: documents made with pdflatex can now be as small or smaller than those from xelatex and lualatex.
Here are the arguments I use.
pdfsizeopt.py --do-unify-fonts=false --do-unify-pages=false infile.pdf
The first option is important: several of my PDFs are horribly corrupted without it. The second option prevents a (benign?) error message with some PDFs in xpdf/poppler. The following options are implicitly set and can be changed to false if you don’t have the required helper program:
--use-pngout=true --use-jbig2=true --use-multivalent=true
Fonts will still be reorganized if any of these options are set to false.
Spacing issues
Somehow I missed out on knowing this stuff for a long time. lacheck and chktex are programs that will point out problems in your LaTeX markup (possibly with a load of false positives). Those tools know about these issues.
- If a period (full-stop) does not end a sentence then the space needs
escaping so that it does not end up too long:
Hello Dr.\ Murray.
- If a sentence ends with a capital letter then you need to tell LaTeX
that it really is the end of a sentence:
I modeled the data with a GP\@.
The\@
is still needed if there is a closing parenthesis between the word and the period:The result is a Gaussian Process~(GP)\@.
- Use
\textit{this is in italics}
for italic text (or\emph{...}
for more semantic markup). Otherwise you have to know about italic correction:{\it this is in italics\/} and {\it so is this}.
- Similarly
\textbf{...}
is sometimes different from{\bf ...}
. Compare`{\bf f}'
to`\textbf{f}'
or`{\bf f\/}'
(including the quote marks).
Italic issues
As explained above, use \textit{...}
for italic text, not {\it
...}
. However, you might not really want italic text. A long
block of special text, for example a theorem statement, can be hard to read in
italic. Consider using slanted Roman text, \textsl{...}
, which is often
easier to read, and mathematics stands out better.
Math mode should not be used to write words. For example, $a_{arg}$ looks
awful, as the subscript is rendered as “a times r times g”.
Instead use $a_\text{arg}$
or $a_\mathrm{arg}$
. If you really
want italic text, then use $a_\mathit{arg}$
.
More spacing issues — hitting page limits
Here are some notes on tricks for squeezing a paper to within conference page limits. There’s always PSSQ, but I like to stay more within the rules than that. Many “9 page” papers can be squashed into 8 pages without changing the font size or main spacing parameters.
- Look for suspiciously large vertical whitespaces between paragraphs. This is often due to TeX not wanting to split a paragraph over a page break, or separate it from a heading. Convincing TeX to pull some text back a page into this white space can have dramatic effects on the page count.
- There are various parameters documented in the TeXbook that make TeX more willing to do ugly things.
- If a paragraph has only a word or two on its last line try adding
\looseness=-1
to the end of it. If possible TeX will reduce the length of the paragraph by a line. This won’t always work because there is a limit to how close TeX will move words. - Moving figures around is a frustrating but often fruitful activity.
- In desperate situations negative kerns, e.g.
\kern-1pt
can enable words to be scrunched more than normally possible. Although rewording is probably a better option. - bibhacks.sty makes the references heading third level and optionally scrunches the font size and spacing of references. See comments inside for more information.
- Move footnotes and short unreferenced equations into the main text. Consider turning third level headings into bold words at the beginning of a paragraph.
- It's usually pointless to engage in space scrunching until finishing the paper. And it's worth keeping a copy of the unscrunched version.
Recommended packages
The following are packages I use regularly, some of which I didn’t discover for a long time:
- microtype — makes justification
look better, especially with narrow columns. Requires pdflatex.
Users of normal latex can get a subset of microtype’s features with:
pdflatex -output-format=dvi
- booktabs — much nicer rules and spacing in tables.
- amsmath — makes a lot of common math constructs prettier and easier. Check out the good documentation.
- natbib — flexible referencing system.
- subfigure — allows you to create sub-figures optionally labelled with subcaptions preceded by (a), (b), etc. Use the [tight] option for better spacing. You can \ref and \subref labels within subfigures to get links to figure 1a) and a). As instructed by the subfigure documentation I now use subfig instead. Having a recent version of the caption package (which subfig includes) is recommended.
- url — better than putting URLs inside \texttt{...}. Line-wrapping works better and you don’t have to escape tildes.
- hyperref — creates clickable links for all types of cross-references. But there are issues (see below) and lots of options to read through and get right.
- artikel3 — an alternative class to article.
- parskip — a hack to get any style to have vertical space between paragraphs rather than indenting them (as artikel3 already does).
- textpos — allows absolute positioning on a page. Can be useful when press-ganging LaTeX into doing slides or a poster. I sometimes use this to put a DRAFT notice, or publication details in the top margin of a paper.
hyperref problems
I have wasted a lot of time pinning down problems when using the hyperref package. Partly because I didn’t RTFM, which it turns out is more important than normal for this package.
- Links go to wrong page? Try putting
\clearpage
before the references section and\phantomsection
before any\addcontentsline
commands. - Complaints about footnotes? Is hyperref included after all other
packages, particularly setspace? If you cannot solve the problem then
passing the
hyperfootnotes=false
option turns off footnote links. - Complaints about duplicate entries? If it is a duplicate page then try
passing
plainpages=false,pdfpagelabels
. If it is complaining about other objects then maybe you have altered the counters and need to be more careful somehow(?). Or maybe (again) it is due to including hyperref before other packages; try including hyperref later in your header. Package hyperref Warning: Token not allowed in a PDFDocEncoded string... removing `math shift' on input line XXX
. Solution: provide alternative plain text for maths that will end up in links. E.g. change\section{The joy of $\pi$}
to\section{The joy of \texorpdfstring{$\pi$}{pi}}
.- Include the algorithm package after hyperref, but manually include the float package (which algorithm will normally include for you) before hyperref. (This is in the README of recent hyperref versions).
- Links to figures point to the caption, so the figure itself goes off the
top of the screen. As in the hyperref README use the hypcap package:
\usepackage[all]{hypcap}
But! If you are using the subfig package make sure you have a recent version of the caption package from CTAN (subfig uses caption). Otherwise references to subfigures can go wrong.
If you have problems with a particular package see if hyperref’s README file says how to deal with it.
Upside-down slides
If dvips produces upside-down slides, put this in your document’s preamble:
% This for broken installs that flip page wrong way \special{! TeXDict begin /landplus90{true}store end }
This doesn’t seem to do any harm in general.
Centering stuff vertically within a line
I currently use:
\newcommand{\myvcenter}[1]{\ensuremath{\vcenter{\hbox{#1}}}}
when I want graphics or other over-sized oddities to be vertically centered.
Normally all objects have their baselines at the same height. Alternatives
include fiddling with the baseline or using \raisebox
.
I still haven’t got around to working out why plain \vcenter doesn’t work in LaTeX.
Gaps to match
To create an empty box with the same size as some particular text use the
\phantom
command. Horizontal or vertical struts of the same width or
height are made with \hphantom
and \vphantom
. See p178 of the
TeXbook and more
help on alignment with boxes.
Fonts
It’s easiest to stick with using Computer Modern. But alternatives are good, especially for those that don’t like ‘modern’ typefaces.
I often do \usepackage{palatino} as a quick way of having something different, but there are other options. See The LaTeX Font Catalogue, and maybe this older survey of TeX fonts.
For non-math documents, I’m currently playing with “Linux Libertine”, with xelatex:
\usepackage{xltxtra} \defaultfontfeatures{Mapping=tex-text} \setromanfont [Ligatures={Common}]{Linux Libertine O}
One could also try getting the font in pdflatex with:
\usepackage[T1]{fontenc} \usepackage{libertine}
The PDF file will be bigger (which is fixable), and the typesetting isn’t quite the same as xelatex’s.
Embedding LaTeX text in diagrams
fragmaster will make .eps and .pdf files from a .eps file with plain text placeholders and a separate file with LaTeX markup. This strategy means you can create a range of diagrams in different packages and have a uniform way of typesetting text in them. It’s worked pretty well for me.
Of course many programs have their own ways of calling LaTeX. This is how I used to put LaTeX in Matlab figures (now I use fragmaster):
% The magic line is: set(0,'defaulttextinterpreter','latex'); % Example: plot(1:3,-(1:3)) legend('${\mathcal F}_1(x)$') print(gcf, 'picture.eps', '-depsc'); system('epstopdf picture.eps'); % To get a small .eps file, convert back again: system('pdftops -eps picture.pdf picture.eps');