LaTeX tips and tricks

Why and how

Ever had problems making a document look nice with your favourite word processor? A couple of section titles are the very last lines of their respective pages, and when you change the font size all images are moved to wrong places?

LaTeX is a typesetting program, which takes care of problems such as these. It is used to produce theses, books, papers and so on. Just tell LaTeX what is a section title by writing \section{My section title}. Then write the section text. LaTeX knows by itself which font sizes and separating spaces to use, and that a title should never end a page. Figures are usually floated to the top or bottom of a page, or to their own figure pages, in order to not interfere with the text. Hyphenation is done automatically. If it is possible, LaTeX will even try to add or remove a small amount of space at paragraph breaks, giving all pages equal height. This looks very good when you flip through the pages. LaTeX is especially good at handling scientific formulae. You can even produce beautiful presentations using the beamer package. LaTeX is completely free, and it produces PDF files by itself.

If you are interested in learning more or even installing LaTeX, you should go through the following list.

If you are new to LaTeX, you should probably not read on in this document until you have had a bit of experience with LaTeX. I will describe how I solved various problems that I have encountered, and the complexity of some of the solutions might seem frightening to newcomers. I have to point out that you will become more familiar with the syntax (even the difficult parts) in time, and also – these are not everyday problems.

Big inline fractions

An example of the differences between \frac and \dfrac

In most cases, you would probably want to put equations and complex formulae on their own separate, centered, equation lines. But sometimes there are reasons to write them 'inline', for example if you just mention something that you have already proven. LaTeX assumes by default that fractions are meant to have the same height as the surrounding text, if they are written inline. This is nice when you write 'a third' or 'a fourth' with numbers instead of words, but more complex fractions can become very difficult to read.

To solve this problem, just include the amsmath package and use \dfrac instead of the usual \frac. The same command can be used inside other dfrac's, to make the formula really high. In the same way, there is a \tfrac to give the fraction text height. (t=text-style, d=display-style) Of course, this is not as interesting, since you can produce the same thing by writing $$\textrm{$\frac{1}{3}$}$$ (although it is a rather disgusting way to do it).

One third is the same as $\frac{1}{3}$.

Introduce the variance estimator

One third is the same as $\dfrac{1}{3}$.

Introduce the variance estimator

Technical graphics with XY-pic

An example of the use of the LaTeX package XY-pic

There are some samples of the uses of XY-pic in The Not So Short Introduction to LaTeX 2ε, but they only show a small amount of all the possibilities. There are single and double framed boxes and circles, arrows of different types and with different arrow heads, and so on. The drawback is that the syntax will not be so easily understandable all the time. However, it is always nice to have figures with vector graphics and the same font as the rest of the document. Read the XY-pic User's Guide for more information.

$$\xymatrix @-1.3pc {

Nice marginpars

The standard and the updated marginpar commands

As you probably know, LaTeX offers the possibility of writing information in the margin, in a very simple way, using the \marginpar command. I personally use it to make keywords stand out, to give grammatical information about a difficult foreign word, and to write other kinds of personal comments to a text. But I think the usual appearance of the margin paragraphs is kind of ugly. First of all, the font size is too large. A smaller font makes it easier to see the difference between the body text and the comment. Second, LaTeX tries to stretch and hyphenate the text so it can be justified between the margins, which most of the time is impossible to do nicely within that limited space.

I always redefine my margin paragraphs. I adjust the width of the paragraph, the font size, and I make the text left-justified on a right page, and right-justified on a left page (when the twoside option is used), to make it 'cling to' the body text.

In my example, I make the margin paragraph 1.2 inches wide, and set the font to footnote size. Adjust these settings so they fit your purposes.

\renewcommand\marginpar[1]{\-\oldmarginpar[\raggedleft\footnotesize #1]%
{\raggedright\footnotesize #1}}
This is some text.\oldmarginpar{This is the old marginpar command.
This is the old marginpar command.}

This is some other text.\marginpar{This is the new marginpar command.
This is the new marginpar command.}

Changing the enumeration counter

Examples of enumerations

This section hardly needs any explanation. Sometimes it is not enough with just the ordinary arabic enumeration counter. With the enumerate package, it is possible to change the arabic numbers into letters or roman numbers (lower or upper case), and the dot into other symbols, like a dash or parenthesis, or even some other text. The enumerate package documentation contains some other examples.

Another thing you sometimes want to do is to change the number of the counter. In my example I have inserted a comment regarding all subsequent items, which means I do not want that comment indented. My solution to this is to end the enumeration, write the comments and then start a new enumeration. Before the first enumeration ended I saved the counter, and before the first \item in the second enumeration I restored it. (The variable enumii_saved has just a dummy name, it could be called something else.) If I had wanted to change the counter of the top level enumeration, I had obviously changed enumi instead. As you can see, I can set the number directly as well. Arabic numbers are always used for LaTeX counters, even if they are represented otherwise in the document. The 7 is the number I want LaTeX to think was the last one written. For small documents, this will probably be the easiest solution to the continuation problem as well, but for large documents there is always the risk that you forget that you added a new item, and you end up with two items number 19, for example.

    \item This is an example of \ldots
    \item \ldots the usual enumeration.
      \item And this is a \ldots
      \item \ldots couple of \ldots
    \begin{enumerate}[-- i --]
      \item \ldots examples of \ldots
      \item \ldots custom-tailored \ldots
      \item \ldots enumerations.
    Some general comments
    \begin{enumerate}[-- i --]
      \item My next point.
      \item My eighth point.

Appearance of the section headers

Example of the uses of the sectsty package

Even though LaTeX knows most there is to know about font sizes and spacing for different parts of the document, it cannot detect the purpose of the text. Although many documents will be fine with the standard fonts, there are also documents which need changes to the headers to look interesting. This is what the sectsty package is for.

There is a lot of things this package will do. Have a look at the documentation. You can prevent the text from being justified between narrow margins, add rules and boxes, or make the header underlined. But that whatever you do, you should think it through thoroughly before making any changes. Underlining, for example, should almost never be used. Pick a book from your bookshelf and try to find underlining. Chances are there will not be a single occurance. Underlining will lower the reading speed (and will probably make your document look ugly as well).

\section{Top level header}
Some text
\subsection{Subsection header}
Some other text