LaTeX Tips and Tricks - ANU - Australian National University

advertisement
Tips and Tricks for
James Montgomery with contributions from many other
users
Research School of Computer Science
Australian National University
View brief explanation
How LaTeX works (logical structure)
Your .tex document
pdfLaTeX
frontend
XeTeX
frontend
...
frontend
document structure
typesetting
2
Boxes: How TeX typesets
TeX tries to make things
look ‘good’, so avoids
‘windows’ (last line of
paragraph on next page)
and ‘orphans’ (headings
at end of page)
Floating environments
(tables, figures, etc.) are
boxes that cannot be split
\mbox command can
prevent contents from
breaking
3
How LaTeX ‘works’
.cls
.sty
.sty
.sty
.aux
.toc
.out
Lots and
lots of files
.tex
latex
pdflatex
xelatex
etc.
.bbl
.bib
.eps, .pdf
...
4
LaTeX processing steps
Section 1
>pdflatex doc.tex
>pdflatex doc.tex
>bibtex doc
>pdflatex doc.tex
>pdflatex doc.tex
See Section ?2 on page ?3.
[2].
Knuth created TeX in 1978 [?]
But Davenport is responsible
for
, which has given us
[?].
much joy [1]
.tex
References
[1] L. Davenport. etc.
[2] D. Knuth, etc.
.bib
.aux
.bbl
Note: This has one more step than is necessary
5
Workflows
Use source control
Git
SVN
svn-multi
to include SVN version
data in drafts
Use an editor
with syntax highlighting
and code completion
• Emacs (with AUCTeX)
• Lyx
• TeXnicCenter ( only)
• TeXlipse
• TeXShop (in MacTeX on )
• Vim (with Vim-LaTeX)
It’s code, so when things don’t work, debug
• \iffalse (or %comment)
• Create a \comment{...} command
6
Working with others
...
Option 1:
Break up the
document
\input{james}
\input{mark}
\input{steve}
intro.tex
james.tex
...
method.tex
mark.tex
results.tex
steve.tex
Option 2:
Agree to work
on different
parts
Margin notes using any
convenient package
doc.tex
todonotes
(and let source
control handle
merging)
doc.tex
marginal notes and figure
placeholders
doc.tex
7
Inside your document
Style
Title
Ann Author
Heading 1
Table 1. Has a caption
This is a sample document,
illustrating key features The
contents don’t matter, only
that the key elements of a
document be present. As
Equation 1 shows, authors
like to include equations.
Math
Col 2
—
—
—
—
—
Col 3
—
—
—
—
—
(1)
Authors
also
include
figures, tables, algorithms
and code listings.
Algorithm 1
Algorithms &
code listings
Col 1
—
—
—
—
—
COBOL := 1
while true
add 1 to COBOL
end
Figure 1. Has a caption
Tables
Graphics
creation &
figures
Arranging
floats
References
Knuth, D. E. (1984) The
TeXbook. Addison-Wesley.
Lamport, L. (1994). LaTeX:
A Document Preparation
System. Addison-Wesley.
Bibliographies
Skip over these
8
Math
Many LaTeX editors
provide menus of
symbols
Early on, have a
cheat sheet handy
amsmath, amsfonts, amssymb
theorems, better math fonts & symbols
mathtools
ntheorem
fixes some things in amsmath;
adds new functionality
predefined theorem & proof
environments
return
9
Tables can be hard
An over-engineered table; avoid all this if possible
\begin{table}[t]
\caption{Best combinations of $\alpha$ and $\gamma$ by problem from BBOB}
\label{tab:bbob_results}
\centering
\begin{tabular}{@{}c@{\ \ }c@{\ \ }c@{\quad}c@{\quad}c@{\quad}c@{\quad}c@{\ \ }c@{\ }r@{\ }r@{}}
\hline
& & \multicolumn{2}{c}{Standard DE} & \multicolumn{2}{c}{Modified DE} & \multicolumn{2}{c}{Param.} & \\
Set & fn & mean & stdev & mean & stdev & $\alpha$ & $\gamma$ & \%-diff & \textit{t}-test \\
\hline
& 1 & 0.00e+0 & 0.00e+0 & & & & & & \\%0.00e+0 & 0.00e+0 & 0.04 & 3 & 0\% & --- \\
& 2 & 6.44e+1 & 2.93e+2 & 8.12e-6 & 4.68e-6 & 0.1 & 2 & 100\% & 15\% \\
1 & 3 & 2.29e+1 & 1.09e+1 & 1.42e+1 & 5.96e+0 & 0.1 & 3 & \textbf{38\%} & \textbf{0\%} \\
& 4 & 2.62e+1 & 8.33e+0 & 1.90e+1 & 6.08e+0 & 0.04 & 1 & \textbf{28\%} & \textbf{0\%} \\
& 5 & 2.16e+0 & 8.59e+0 & 0.00e+0 & 0.00e+0 & 0.1 & 1 & 100\% & 11\% \\
\hline
Use an editor for
complex tables
e.g. LaTable on Windows
excel2latex
VBA add-in for converting Excel
data to LaTeX tables
Use \begin{table*}...\end{table*} to
make a table span columns in two-column
papers.
\begin{tabular}{lcr @{sep} p{width} }
width
left
centre
right
@{sep} replaces default gap
with sep, which can be text
or spaces.
And for styling:
Text in this cell will
be wrapped and
justified.
In p{width}, width must be unit of
distance, including mm, cm, m, in,
or even printing measures like ‘ex’
and ‘em’.
booktabs
colortbl
better spacing of lines
around table contents
control cell background
colour & line colour
11
longtable
Table 1. This caption should not be legible
More tricks with tables
allows tables to split over
pages
lscape
rotate part of document 90
rotating
multirow
rotate part of document
by any angle
support multi-row cells
12
Captions and labels
List of Tables
Table 1. Brief
Table 2. Succinct
Table 3. Laconic
v
\caption[Short caption for list of tables]
{Long, descriptive caption to appear with table}
Table 1. Described
in full, with lots of
explanation.
thesis.tex
Works with \section commands, too
Make labels (i.e., \label) in papers unique
across papers. This will make it easier when
creating chapters from those papers.
return
13
Graphics creation
Use vector graphics
formats where possible;
will print better and
(generally) be smaller
Gnuplot can produce a
variety of high quality
charts and graphs
Inkscape is one option
Add textext plug-in to
include LaTeX
generated content
PDFs are suitable when
using pdfLaTeX
14
Figures
graphicx
support for loading image files
into document
(a) Box
(b) Arrow
Figure 1. Shared caption for (a) and (b)
\begin{figure}
figure* to span columns
\centering
\subfloat[Box]{
\includegraphics{subfigure1}
\label{fig:subfig1}
}
\subfloat[Arrow]{
\includegraphics{subfigure2}
\label{fig:subfig2}
}
\label{fig:bothfigures}
\caption{Shared caption for
\subref{fig:subfig1} and
\subref{fig:subfig2}}
\end{figure}
subfig
separate captions for sub-figures
If you really want
separate figures side
by side then use the
minipage environment
return
15
Algorithms and code
Algorithm 1. Collatz
n := from user
while n  1 do
if n is even then
n := n / 2
else
n := n * 3 + 1
end
end
program collatz;
var n : integer;
begin
n := Readln;
while n <> 1 do begin
if not odd(n) then
n := n / 2
else
n := n * 3 + 1;
end;
end;
algorithm
listings
floating algorithm environment
like figure or table
syntax highlighting for different
languages
+
algorithmic
formatting algorithms,
keywords, etc.
+
xcolor
defining and using colour
return
16
Positioning floats – Error 404
• The content here was going to be so lowlevel that it’s been left out. It would have
included tricks like
– Moving where table of figure appears in TeX
source (consider a separate file and \input
to make this easier)
– Forcing the float onto its own page (with other
floats) with [p] placement specifier
– Using \clearfloats
return
17
BibTeX
Use a tool to maintain your
BibTeX databases. For example,
JabRef or BibDesk (on Mac)
According to \citet{smi90},
that is \citet*{jon90}, or
even \citep{jon90}.
According to Smith et al. (1990), that
is Smith, Jones, and Johns (1990), or
even (Smith et al. 1990).
natbib
alternative cite commands
Unless you are an expert in the programming
language Forth, don’t edit BibTeX style files
(.bst); look for one that suits your needs
return
18
Basic styling
fancyhdr
fine-grained control over
header and footer content
savetrees
reduce space usage; but
doesn’t look as nice
fullpage or geometry
easy smaller margins or
control over margins
19
More styling
Balancing Columns
Standard LaTeX font sizes
are not always sufficient
\tiny
\scriptsize
\footnotesize
\small
\normalsize
\large
\Large
\LARGE
\huge
\Huge
flushend
relsize
balance columns at the end of
a paper
scale fonts with finer
control; useful in tables.
return
20
Large documents like your thesis
\documentclass[a4paper]{book}
% packages etc.
\input{macros}
\begin{document}
\frontmatter
% title page, TOC, etc.
\mainmatter
\includeonly{chapter 1}
\include{chapter1}
\include{chapter2}
...
\appendix
\include{appendix1}
...
\backmatter
%bibliography etc.
.sty
Your abbreviations and
other handy utilities
macros.tex
chapter1.tex
chapter2.tex
appendix1.tex
.bib
Note: This is not a complete skeleton document
21
anuthesis
Useful packages
Chapter 1
Some Illustrative Text
This chapter is only here to illustrate
that the cleveref package can generate
the entire text for a cross-reference such
as in Section 1. cleverref must be
loaded last.
Similarly, hyperref makes crossreference numbers hyperlinks, as in
Section 2 on page 3.
The xr package allows for interdocument cross-references (see Chapter
2), if those documents are compiled
separately.
See the source
ANU-specific styling
cleveref
generates all text for crossreferences, not just number
hyperref
hyperlinks in PDFs
setspace
set line spread as needed
xr
inter-document crossreferences; useful if compiling
chapters separately
With some command trickery you can
compile chapters separately or as part of
whole document. See appendix.
22
Other uses for LaTeX
•
In PowerPoint: ‘TeX4PPT’
– Older, but easy install
http://users.ecs.soton.ac.uk/srg/softwaretools/presentation/TeX4PPT/
– Newer, slightly fiddly install, works better
https://sites.google.com/site/tex4ppt/
•
Instead of PowerPoint:
– Packages like beamer
24
Further reading and seeking help
The Not So Short Introduction to LATEX2ε
(1995–2011) by Tobi Oetiker & contributors
http://tobi.oetiker.ch/lshort/
Math into Latex (2000) by George Grätzer
Many free copies available on the web
www TeX
Stack Exchange
http://tex.stackexchange.com/
www CTAN
for package documentation
http://www.ctan.org
25
Appendix
Things to look out for in the future
• New front ends to LaTeX with native
support for Unicode (so no more \"a for ä)
and other features:
• XeTeX
• LuaTeX
26
Download