LaTeX writing tips

by Thomas Mejtoft

When writing a research paper, thesis, report, or just any type of document, there are many different systems for preparing your document – WYSIWYG systems, such as Word and Pages, and typesetting systems, such as LaTeX to mention a few. When writing reports and research papers, especially within disciplines that requires equations and calculations, LaTeX is popular.

The principles of writing documents in LaTeX is very similar to writing in HTML and styling using CSS. Simplified, the files used in LaTeX are template files for formatting and styling, source code files for your content, and output files. The output files are the formatted pdf-files. Another feature of LaTeX is the handling of the references using e.g., BibTeX.

You can either download and install LaTeX on your local computer or use an online editor, e.g., Overleaf, when writing and compiling your document. Even though these things are general, I strongly recommend using BibTeX with Overleaf when writing.

Below are som Tips & Trix on using LaTeX that I have collected over the years, most often as frequent errors in students’ works.

Hope you find this material useful!
If you are looking for other resources around writing to use, here is a page with resources and material. If you are looking for how to use and cite figures, screenshots, code etc. please refer to the following documents: How to use and cite figures from other sources, How to cite screenshots, References to secondary sources and review articles, Writing references to personal communication, Writing references to programming code, and Citing content created by generative AI. Regarding quotes and visualizing data, please read the following documents: Master quotes in writing and How to visualize your data in an understandable way.

Tips on how to organize the LaTeX project during writing

Include the right packages.

Nice packages for writing and styling:

  • Include the hyperref package to use hyperlinks in your text.
  • Include the graphicx package for enhanced graphics support.
  • Include the xcolor package for color support.
\usepackage{hyperref}
\usepackage{graphicx}
\usepackage{xcolor}
Break up large TeX-files

If you are writing a longer document, e.g., a thesis or report, it might be of value to split the document into smaller parts, e.g., the different chapters of your writing. It becomes easier to find and edit text. The different files are included into the main document using the command \include{ }.

…

\include{introduction.tex}
\include{background.tex}

…
Placement of tables and figures

Placement of figure and table environment is decided by the templet, but it is possible to create ways to suggest placement and override the template. In the example below the figure should be position about where it is in the code (h) and if that violates the principles, place it at the top (of the next page) (t).

\begin{figure}[ht]
  \centering
  \caption{A nice wood-fired pizza.}
  \label{fig:pizza}
  \includegraphics[width=0.9\linewidth]{pizza.jpg}
\end{figure}

h = Place the figure/table approximately here, i.e., where it is placed in the source.
t = Position at the top of the page.
b = Position at the bottom of the page.
p = Position on a special page for figures/tables only.
! = Override positioning at a “good” position instead of “exactly here”

Furthermore, it is possible to place several figures within the same figure environment to create Figure 1a, Figure 1b etc.

\begin{figure}[ht]
  \caption{What makes the pizza amazing.}
  \label{fig:pizza-ingredients}

  \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{dough.jpg} 
    \caption{Cold proof pizza dough.}
    \label{fig:pizza-dough}
  \end{subfigure}

  \begin{subfigure}{0.5\textwidth}
    \includegraphics[width=0.9\linewidth]{sauce.jpg}
    \caption{Spicy pizza sauce.}
    \label{fig:pizza-sauce}
  \end{subfigure}

\end{figure}

This code puts two figures next to each other with different texts. Under the two figures there is a text that summarizes the whole figure.

Pro tip: Using vector graphic images make the best output (eps, svg, etc.). However, high-resolution bitmap is also fine (png, jpg, etc.).

Labels and cross-references

Put labels on figures, tables, sections, chapters, and anything that is included in your writing that you (might) want to cross-reference. Making a cross reference to a label makes e.g., figure number or page number to be updates automatically.

Tip: The \label command must be after (or inside) the \caption command otherwise the reference will be to the (sub)section.

Example:

LaTeX code:
%labeling figures, tables, etc.
\begin{figure}
  …
  \caption{Results from survey.}
  \label{fig:survey-results}
  …
\end{figure}

%Writing cross-reference in LaTeX code:
The results from the survey are presented in Figure \ref{fig:survey-results} on page \pageref{fig:survey-results}.

Output:
The results from the survey are presented in Figure 4 on page 36.

Since the same command (\label) is used to reference anything the de-facto standard is to label the descriptions to know were the reference is pointing. These are the common practice:

fig:Figure
tab:Table
eq:Equation
sec:Section
subsec:Subsection
ch:Chapter
lst:Code listing
itm:Enumerated list item
alg:Algorithm
app:Appendix subsection
Using ToDo-notes while writing

There are always stuff to do while writing. There are a couple of ways to add ToDo-notes in the text while writing. The most simple way is to add a comment in the text. A comment will not show on the output, but is visible in the source code.

LaTeX code:
Previous research has defined this as a ` `total waste of time' ' regarding tracing. % Find source to this statement

A more sophisticated way and to easily show in the text is to use the package todonotes. This package puts a ToDo post-it in the margin of the document. Below is an example with a simple ToDo note (default orange color), a green note, a missing figure, an inline ToDo note in red and a list of all Todo’s in the document.

LaTeX code:
\usepackage[colorinlistoftodos, textsize=scriptsize]{todonotes}

...

Previous research \todo{Find source to this statement.} has defined this as a ` `total waste of time' ' regarding the concept of tracing. \todo[color=green]{More quotes to support this statement.}

Below is a figure showing all the problems that was during the project.
\missingfigure{Create a timeline of all time wasted.}

\todo[inline, color=red]{The statement below needs to be checked with a expert panel}
Furthermore, research show that not is all bad with wasting time. 

\listoftodos{} % Creates a list of all ToDo's in the document.

Output:

Here are some small tips & trix on using LaTeX
(a.k.a. Frequent errors in student papers)

Swedish (or other special) characters

Check for the ability to write Swedish (or other non-English) characters (most often noticed when writing Umeå University).

If you are using Swedish (or other non-English) characters regularly through a paper, e.g., when writing in another language than English, add the UTF-8 package (see below). If you are writing in e.g., English and using just a few non-English (or other special) characters, you can type special characters directly into your code (see example below).

% Adding the UTF-8 package
\usepackage[utf8]{inputenc}

% Swedish special characters in any LaTeX code
\AA    % Å
\aa    % å
\"{A}  % Ä
\"{a}  % ä
\"{O}  % Ö
\"{o}  % ö

% Other useful special characters
-       % Hyphens (-)
--      % en-dash (–) (sv: tankstreck) (Used to mark ranges, e.g., pages in reference lists)
---     % em-dash (—) (Used to separates extra information in text)
\ldots  % Ellipsis (…) (Used when omitting information, e.g., in a quote)

\&      % Ampersand, and-sign (system characters need to be escaped using \ ) 
~       % Non-breaking space
Non-breaking space before (any type of) reference

Put a space between the last word before the reference and the reference: “… as been shown [7].” or “…as been shown (Mejtoft, 2011).” Use “ “ or “~” (ties or non-breaking space) in source code. Most often a non-breaking space might be your best option.

LaTeX code (using natbib):
… as been shown~\cite{Mejtoft:2011}.
… in Chapter~2, Dr.~Mejtoft explains the concept (see Appendix~C).

Output:
… as been shown [7].
… in Chapter 2, Dr. Mejtoft explains the concept (see Appendix C).

or

… as been shown (Mejtoft, 2011).
… in Chapter 2, Dr. Mejtoft explains the concept (see Appendix C).
Superscript and subscript in-line

There might be a need to write superscript (e.g., 2nd) or subscript (O2) in-line in the document. This is done using \textsuperscript{ } and \textsubscript{ }.

Example:

LaTeX code:
If you find yourself at camp on Friday the 13\textsuperscript{th}, don't end up in H\textsubscript{2}O.

Output:
If you find yourself at camp on Friday the 13th, don't end up in H2O.
Quotation marks

Single quotation marks are produced using ` at the start and ' at the end. Double quotation marks (for citation) are produced by typing ` ` in the code before the quote and ' ' after the quote (two directed single quote characters at the beginning and two undirected single quotation characters at the end of the quote, no spaces in-between the characters). Then you will get distinct typographic opening double quotation marks (“) and closing double quotation marks (”).

Example:

LaTeX code:
Previous research has defined this as a ` `total waste of time' ' regarding tracing.

Output:
Previous research has defined this as “a total waste of time” regarding tracing.
LaTeX code (using natbib):
\citet{Mejtoft:2000} states that ` `previous research has defined this as `a total waste of time' regarding tracing' '.

Output:
Mejtoft (2000) states that “previous research has defined this as ‘a total waste of time’ regarding tracing”.

If the single and double quotation follow each other, the control sequence \, (backslash and a comma) should be used to ensure the right spacing in-between the different quotation marks.

Example:

LaTeX code (using natbib):
Tracing --- according to \citet{Mejtoft:2000}, ` `previous research has defined this as `a total waste of time'\,' '.

Output:
Tracing — according to Mejtoft (2000), “previous research has defined this as ‘a total waste of time’ ”.
Width of figures

It is possible to specify the width of figures, to set the appropriate size of the figure compared to the rest of the document. It might be necessary to work with textwidth, columnwidth, and linewidth, depending on the situation. Read more.

% Sets the width to the full width of the text
\includegraphics[width=\textwidth]{results.png}
% Sets the width to 80% of the width of the text
\includegraphics[width=.75\textwidth]{results.png}

% Sets the width to the full width of the column (valuable when having two or more columns)
\includegraphics[width=\columnwidth]{results.png}
% Sets the width to 60% of the full width of the column
\includegraphics[width=.6\columnwidth]{results.png}

% Sets the width to 50% of the length of the line in the current environment
\includegraphics[width=.5\linewidth]{results.png}
Footnotes

In general footnotes should be used sparsely and some publishers do not allow footnotes. Footnotes are included using the \footnote{ ... } command. The footnotes are numbered automatically. If you want a specific number, it can be chosen using the \footnote[XX]{ ... } command.

Example

LaTeX code:

...as been seen in the design\footnote{The product was designed by Thomas Mejtoft.}.

...the papers are available Open Access on the author's web site\footnote[42]{\href{https://www.mejtoft.se}{mejtoft.se}}
Force line break on long weblinks

From time to time, long URLs do not break in the reference list (and sometimes in other parts of the paper). To solve this problem, use the package url and define characters that is used for line break (some characters are predefined for UrlBreaks when the url package is loaded that might work for you). To define your own characters for line break, use UrlBreaks and to define characters that should not allow a line break use UrlNoBreaks.

The package xurl loads url and add some extra breaking points, and can also be used.

LaTeX code:

\usepackage{url}

\def\UrlBreaks{\do\-\do\.}   % Defines "-" and "." as characters for line break

%To define character that do not allow line break
\def\UrlNoBreaks{\do\(\do\)} % Defines "(" and ")" as characters that do not allow line break
Displaying code in the document

To display code there are a couple of common ways – verbatim environment, listings, and minted.

Verbatim environment creates a monospaced font and do not execute commands within the environment

LaTeX code:
\begin{verbatim}
  To make a in-text reference in \LaTeX{}, \cite{ } is used.
\end{verbatim}
Output:
To make a in-text reference in \LaTeX{}, \cite{ } is used.

Listings need the package listings and are using the lstlisting environment. The difference is that by defining the language of the code (e.g., Java, HTML, Python, etc.), the commands in the code can be highlighted.

LaTeX code:
\usepackage{listings}

\begin{lstlisting}[language=Java]
  
  …

\end{lstlisting}

Since the code within the lstlisting environment is ignored, caption, lables etc need to be defined as the example below.

LaTeX code:
\usepackage{listings}

\begin{lstlisting}[language=Java, caption={Code written in Java}, label={lst:java-code}]
  
  …

\end{lstlisting}

There are other commands that could be defined to make it easier to display your code, e.g., breaklines and basicstyle for line break and fontsize:

\usepackage{listings}

\begin{lstlisting}[language=html, breaklines=true, basicstyle=\small]
  
  …

\end{lstlisting}

It is also possible to define a style for the listing using \lstdefinestyle{ }, where the variables above can be defined.

Another useful package is minted. This require the package minted and are using the minted environment. This package color the code according to the programming language defined.

LaTeX code:
\usepackage{minted}

\begin{minted}{html}
  
  …

\end{minted}
BibTeX

For most references you can export references as BibTeX from the publishers’ websites and the databases, e.g. ACM Digital Library, IEEE Xplore, ScienceDirect etc.

Use the right type in the BibTex entries – @article, @inproceedings, @book, @inbook, @online, @techreport being some of the most common.

More on writing references using BibTeX here.

Licensed under a Attribution-ShareAlike 4.0 Creative Commons license.

(First published by Thomas Mejtoft: 2020-02-20; Revised: 2023-05-09; Last updated: 2023-12-21)