# LaTeX

### LaTeX word count

In terminal use:

detex main.tex | wc -w

this removes all LaTeX commands and pipes it into word count. Unfortunatelly it still keeps math content and counts them as words. See http://tex.stackexchange.com/questions/534/is-there-any-way-to-do-a-correct-word-count-of-a-latex-document for more elaborate solutions.

### Nonstop mode

If you encounter an error while running pdflatex it usually drops you to a prompt where you have to type X to quit or <RETURN> to proceed.

Get rid of that by using

pdflatex -interaction=nonstopmode main.tex
pdflatex -interaction=errorstopmode main.tex

The first one tries to continue even if there are errors and the second one stops at the first error and returns you to the terminal.

### Hyphenation

LaTeX automatically hyphenates your words and keeps your text aligned along both margins. If you find that LaTeX incorrectly hyphenates a word (or doesn’t at all) you might suggest good places to break up the word by using (it will not show if not needed)

Maxwell just loved electro\-magnetism!

Useful settings to adjust how often LaTeX uses hyphenation are

\hyphenpenalty=5000  % penalty for hyphenation
\tolerance=1000      % tolerance for long spaces
between words

Hint: Use the package babel to tell LaTeX which language you are typing in for best hyphenation.

### Structure of TeX-files

When writing large projects, your TeX file while probably soon contain thousands of lines and it will be a mess to find the right section or equation when you need to edit it. Not to worry! You can use \input{file} or \include{file} and keep your code in separate files.

\input works like copy-pasting the code into the document, which is perfect for including a preamble

\include is good for including different parts of your document, like chapters or sections. LaTeX will then use different AUX files (temporary files for compiling). This means that LaTeX only needs to re-compile the chapters that you have made some changes in. Unfortunately it can’t be nested, and will put your chapter or section on a new page.

Hint: When working on a single chapter you can use \includeonly to display only the output from certain included files. Since LaTeX then still will know about the other chapters using the AUX files you can still reference to equations and figures in the whole document and page numbers will be shown as in the full document.

This is usually my structure for larger projects:

% In main.tex
\input{preamble}

\begin{document}
\include{front}  % front page and TOC
\include{ch1}
\include{ch2}
\include{ch3}
\include{biblio} % bibliography
\end{document}

### Typography and encoding

Simple text files are handled very differently on different systems. During the stone age each character took one byte with the the possible values 0–255, which meant that characters like ßüåäö or 物理學 couldn’t commonly be used.

Nowadays most self-respecting operating systems use UTF–8 or better. I would recommend saving your files as UTF–8 and use \usepackage[utf8]{inputenc} in the beginning of your preamble to ensure compatibility with other systems.

LaTeX usually makes nice typography (kerning, spaces etc.). Useful packages to make it even better are:

\usepackage[T1]{fontenc}
\usepackage[protrusion=true,expansion=true]{microtype}

The first package makes letters like ö its own character and not an o with two dots (which also make a difference for searching your pdf and copy-pasting). The second package gets into the details of typography and generally makes you pdf look a lot nicer and easier to read. See for example http://web.willbenton.com/writing/2008/better-latex or http://texblog.net/latex-archive/layout/pdflatex-microtype/.

### Margins and papersizes

One common complaint about LaTeX is that it sometimes only fills a small portion of the page with large left and right margins.

This is done by purpose for allowing notes in the margins, but perhaps most importantly for easier reading. In the good old days computers only had 80 characters and 25 rows of width and height screen real estate, but many developers still stick with about 80 characters per line before they wrap to another line. It is much easier to read a text where you don’t loose track of which line you are reading as your eyes travel along a line.

The effect also depends on which paper size you use. In US and Canada the standard size is US Letter (216 x 279 mm) and in Europe the standard is A4 (210 x 297 mm).

To get nice margins and correct paper size use one of:

\usepackage[letterpaper]{geometry}
\usepackage[a4paper]{geometry}

### Nice figures

Nice looking figures are very important for the overall impression of a document. To get good looking figures you need to make sure that they scale properly. There are basically two types of image formats: vector or raster (pixel). The former can be scaled indefinitely without pixelizing while the latter is basically made for one single scale. Example of vector formats are: PDF (some), EPS and SVG while JPG, GIF, PNG and BMP are raster.

To create good looking vector images I recommend using the latest versions of:

• Xfig

• Inkscape

Both programs can create images with equations or other LaTeX commands that will be interpreted when compiling the document.

Remember that pdflatex can’t use EPS files.

My default workflow is:

1. Create image in Inkscape
2. Add text and write $...$ for math mode.
3. Save to an SVG file for future edits
4. Save another copy as PDF
5. Use the options PDF+LaTeX that is available in later versions.
6. Inkscape will create a PDF file and a TeX file.
7. Import the image using
\begin{figure}
\centering
\def\svgwidth{\columnwidth} % optional
\input{image.pdf_tex}
\end{figure}

### BibTeX

BibTeX is a way to manage all your references. You keep all the data of your references in a separate file and LaTeX will format it automatically to the reference style you want.

To read more about the bib format have a look at http://en.wikibooks.org/wiki/LaTeX/Bibliography_Management#BibTeX

To correctly reference arXiv e-prints see http://arxiv.org/hypertex/bibstyles/ which also contains a list of good BibTeX styles.

Hint: Use for example INSPIRE to find a paper and click on the BibTeX link under the entry to get the correct citation with minimal work.

### Custom commands

If you have to write \mathbb{N} for $\mathbb{N}$ a lot of times it is useful to create a new command \N like this

\newcommand{\N}{\mathbb{N}}

To make sure you are in math mode when you do this (otherwise it will take you into math mode) use ensuremath{}. I also like the look of mathbbm better than mathbb which comes with the package bbm.

\newcommand{\N}{\ensuremath{\mathbbm{N}}}

You can also make commands that take arguments and optional arguments. If you want, you are even able to redefine existing commands, create environments and packages. Read more about it here: http://en.wikibooks.org/wiki/LaTeX/Customizing_LaTeX

### latexmk

If you are tired of running

pdflatex main.tex
bibtex main
pdflatex main.tex
pdflatex main.tex
pdflatex main.tex

use for example latexmk main.tex which automatically runs all extra tools and compiles enough times for all references to get right.

It also has the awesome option of running continuously - let it run in the background and type away or edit an image, press save and latexmk will automatically recompile your document. This also enables it to optimize the compilation and will run faster than a complete recompilation.

Another amazing feature is the option -output-directory which enables you to put all the AUX, BBL, LOG et.c. files in a separate directory and keeps your working directory clean.

### Packages and settings I use and why

Here is a quick list of my favorite packages and settings. See http://ctan.org/pkg/packagename for more information.

• \usepackage[colorlinks=true]{hyperref} to make references click-able.

• \usepackage[hyphens]{url} to be able to write url:s easily: \url{http://myser.ver.com/~username/index.html}

• \usepackage[colorinlistoftodos]{todonotes} to make todo:s, missing figures and todolists in document.

• \usepackage[font=small,labelfont=bf]{caption} I like my captions to look different from the rest of the text.

• \usepackage[section]{placeins} this makes sure that no figures are placed outside of their sections

• units to correctly write physical quantities \unit[10]{Hz}

• fancyhdr enables you to create nice headers and footers that are really useful when writing assignments if you don’t want a title page but still show a title and author in the header.

• pdfpages to include whole pages of other PDF documents.

• tensor to properly write the Riemann tensor without the indices displayed directly above each other

• subcaption to be able to have several images (a, b, c) with different captions within the same figure.

• \numberwithin{equation}{section} this will number your equaitons like (2.4) instead of (15).

• For paragraphs I like \setlength{\parskip}{\baselineskip} \setlength{\parindent}{0pt} which gives makes paragraphs be separated by an empty line without indentation.

More, useful packages can be found here:

http://www.howtotex.com/packages/9-essential-latex-packages-everyone-should-use/

http://blog.analogmachine.org/2011/05/16/top-10-latex-packages/

But the best package of all is probably