For everything alse go to the dedicated LaTeX Wikibook page or StackExchange page..
For TeX materials (e.g., package details) visit the Comprehensive TeX Archive Network (CTAN).
For starters, I recommend to read first Learn LaTeX in 30 minutes
Table of contents
- Online vs Offline
- Text
- Figure
- Table
- Math
- Bibliography
- How to add citation and reference
- Useful misc arguments
If you are a fresh LaTeX user, choosing the right TeX editor for the first time is not an easy task. In my opinion the best way to approach LaTeX is by using Overleaf. Overleaf is an online and collaborative LaTeX editor, which means that you only need a web browser and an Internet connection to work. You do not need to configure or install nothing, just create an account and start creating a new project, Overleaf will handle everything for you. The free account is enough for starters, if you need more compile timeout or want to add more collaborators to the project or integrations with GitHub, Dropbox, Mendely or Zotero check the Plans and Pricing section.
While, if you are a geek and want to have everything under control you can choose between many editors like: Texmaker, TeXWorks, Lyx or TeXStudio. On the other hand is possible to host a community edition of Overleaf on your on server.
It is worth to mention alternatives like: Papeeria, Authorea or CoCalc.
Here we present basic text formatting:
To bold text in LaTeX we simply use the command \textbf{This text is in bold}
.
To format text in Italic just use the command \textit{It's-a me, Mario!}
.
The same apply to underline text with the command \underline{Text underlined}
.
Sometimes we need to change color of text parts, we only need to use the xcolor
package.
Require package:
\usepackage{xcolor}
And inside the text we apply the color, e.g., this text is not in red and \textcolor{red}{this text is in red}
.
The color can be changed inside the command \textcolor{color_to_use}
.
Further colors and commands to used inside the textcolor
package can be found here.
Combining commands will help us to a format mix, for istance we want to underline and bold a text we will use \underline{\textbf{This text is underlined and bold}}
.
Highlighting text is fundamental, espercially during a paper review will be requested to highlight all the changes. Required packages:
\usepackage{soul}
\usepackage{xcolor}
With soul
we can highlight text lightning fast with \hl{text to highlight}
command, when we will not need the highlights just put the package into comment with %\usepackage{soul}
and all the highlights will disappear without generating errors or warnings.
The default highlighting color is yellow, it can be changed by setting the color as \sethlcolor{color_name}
(i.e., for highlighting in red \sethlcolor{red}
).
But what happen if we want to highlight citations or references?
To highlight citations and bypass all the warnings we need to put a \mbox
command like this:
\mbox{\cite{paper_to_cite}}
While, highlighting references (like figures, tables or math equations) is much easier, just put the reference between curly brackets to avoid warnings like so:
\hl{Fig. {\ref{fig:figure_to_refer}}}
To align text we use the environment ragged2e
which is easy and fast to use. The common command to align text is flush
To align text on the left use flushleft
:
\begin{flushleft}
Insert the text here.
\end{flughleft}
To align text on the centre use center
:
\begin{center}
Insert the text here.
\end{center}
To align text on the right use flushright
:
\begin{flushright}
Insert the text here.
\end{flughright}
To justify the tex use justify
:
\begin{justify}
Insert the text here.
\end{justify}
The following is an example of the above text alignments:
Sometimes you need to align the text vertically:
To align the text at center of the page you will need hspace
and vfill
commands:
\hspace{0pt}
\vfill
Text
\hspace{0pt}
\vfill
To align the text at the bottom of the page you will need hspace
and vfill
commands:
\hspace{0pt}
\vfill
Insert text here.
Sometimes you need to fill space with an invisible text, like a character that can be used to apply distance in text. By using the command phantom
followed by the argument (i.e., the invisible text):
Some text here, this is a value\phantom{-}blablabla.
To add a footnote just write the text inside the command \footnote{add footnote here}
, the footnote counter will be inserted automatically and the text will appear in the footer.
If num is not specified, the footnote counter is incremented and its value is used.
But sometimes you need to place a numbered footnote by using the optional num
argument to change the footnote number \footnote[num]{text}
.
To add an hyperlink you need to import the package hyperref
which handles cross-reference in the text. Hyperref can be setup with the following setings, here a short list of options:
\hypersetup{
colorlinks=true,
linkcolor=blue,
urlcolor=cyan
}
where:
colorlinks
is true, all the links will be coloured in red,linkcolor
internal links are coloured in blue;urlcolor
are all the links to websites and are coloured in cyan.
For example, if we add an hyperlink to an external webiste (i.e., Google) we have to specify the target url and the clickable text as arguments of \href
command:
\href{http:https://www.google.com}{Go to Google}
Some read the text others look at the pictures. And we should be able to plot figures on a LaTeX document:
Package required:
\usepackage{graphicx}
\begin{figure}[ht]
\centering
\includegraphics[width = 1\textwidth]{example-image}
\caption{Insert caption here.}
\label{fig:figure_name}
\end{figure}
The figure is added by using \includegraphics
command which takes as argument the size of the figure (that can be adjusted using the textwdith - e.g., by applying [width = 0.5\textwidth]
the figure will be 50% of the textwidth) and the file path {example-image}
.
Then you can add the caption below the figure with \caption{Insert the caption here.}
command and the label with \label{fig:figure_name}
used as a reference in the text.
Also, the figure can be centered with the \centering
command.
Ok, one single figure is easy stuff, what about muliple figures arranged like a grid?
One possible solution made by using the subcaption
package:
\usepackage{graphicx}
\usepackage{subcaption}
We can arrange the sub figures as we wish, in the following example two figures have been arranged side by side on the same row:
\begin{figure}[ht]
\centering
\subfloat[Figure 1]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\hfil
\subfloat[Figure 2]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\caption{Insert caption here.}
\label{fig:figure_name}
\end{figure}
The main difference with the single figure is related to the sub figure added by \subfloat
command, which takes as argument the caption related to the sub figure (e.g., \subfloat[Figure 1]
). While, \hfil
command is used to set horizontal alignment in matrices and arrays.
Then, a full caption and label can be referred to the entire figure or the single sub figures.
In the following example more sub figures have been added by using the \subfloat
command:
\begin{figure}[ht]
\centering
\subfloat[Figure 1]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\hfil
\subfloat[Figure 2]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\subfloat[Figure 3]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\hfil
\subfloat[Figure 4]
{\includegraphics[height=2cm, width=2cm]{example-image}
\label{fig:sub_figure_name}}
\vspace{2mm}
\caption{Insert caption here.}
\label{fig:multiple_sub_figures_name}
\end{figure}
As you can see a vertical space of 2mm
has been added. The vertical space \vspace{2mm}
has been applied between the last row and the caption.
In the following example more sub figures have been added by using the \subfigure
command but there is only one caption per row:
\begin{figure}[ht]
\centering
\begin{subfigure}[b]{\textwidth}
\centering
\includegraphics[width=0.475\linewidth]{example-image}\label{fig:sub_figure_name}
\hfill
\includegraphics[width=0.475\linewidth]{example-image}\label{fig:sub_figure_name}
\caption{Insert sub-caption of the first row.}
\end{subfigure}
\vskip\baselineskip
\begin{subfigure}[b]{\textwidth}
\centering
\includegraphics[width=0.475\linewidth]{example-image}\label{fig:sub_figure_name}
\hfill
\includegraphics[width=0.475\linewidth]{example-image}\label{fig:sub_figure_name}
\caption{Insert sub-caption of the second row.}
\end{subfigure}
\caption{Insert main caption here.}
\label{fig:figure_name}
\end{figure}
In the following example more sub figures have been added by using the \subfigure
command but there is only one caption per col:
\begin{figure}[ht]
\centering
\begin{subfigure}[b]{0.475\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}\label{fig:sub_figure_name}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.475\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}\label{fig:sub_figure_name}
\end{subfigure}
\vskip\baselineskip
\begin{subfigure}[b]{0.475\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}\label{fig:sub_figure_name}
\caption{Insert sub-caption of the first col.}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.475\textwidth}
\centering
\includegraphics[width=\textwidth]{example-image}\label{fig:sub_figure_name}
\caption{Insert sub-caption of the second col.}
\end{subfigure}
\caption{Insert main caption here.}
\label{fig:figure_name}
\end{figure}
Another complex figure arrangement is the grid-like layout, where you need captions for rows and columns. There are many methods to create a grid-like layout, especially by using a table, in the following example we will directly use figures captions for rows and columns.
The main difference between the other figure layouts is related to:
\settoheight{\tempdim}
which sets a temporary dimension for each cell of the grid;\rotatebox{90}{\makebox
which creates the row-caption inside a box rotated by 90 degrees;\makebox
which creates column-caption.
\begin{figure}[htbp]
\settoheight{\tempdim}{\includegraphics[width=0.3\textwidth]{example-image-a}}
\rotatebox{90}{\makebox[\tempdim]{First row}}\hfil
\includegraphics[width=0.3\textwidth]{example-image-a}\hfil
\includegraphics[width=0.3\textwidth]{example-image-b}\hfil
\includegraphics[width=0.3\textwidth]{example-image-c}
\rotatebox{90}{\makebox[\tempdim]{Second row}}\hfil
\includegraphics[width=0.3\textwidth]{example-image-a}\hfil
\includegraphics[width=0.3\textwidth]{example-image-b}\hfil
\includegraphics[width=0.3\textwidth]{example-image-c}
\rotatebox{90}{\makebox[\tempdim]{Third row}}\hfil
\includegraphics[width=0.3\textwidth]{example-image-a}\hfil
\includegraphics[width=0.3\textwidth]{example-image-b}\hfil
\includegraphics[width=0.3\textwidth]{example-image-c}
\medskip
\hspace{0.65\baselineskip}\hfil
\makebox[0.3\textwidth]{First column}\hfil
\makebox[0.3\textwidth]{Second column}\hfil
\makebox[0.3\textwidth]{Third column}
\caption{Grid-like figure.}
\label{fig:grid_like}
\end{figure}
Tables, tables and tables... The killer of every LaTeX student!
\begin{table}
\centering
\begin{tabular}{l|l|l}
Col1 & Col2 & Col3 \\
\hline
Row1 & 1 & 2 \\
Row2 & 3 & 4 \\
Row3 & 5 & 6
\end{tabular}
\caption{Insert caption here.}
\label{tab:table_name}
\end{table}
The table begin with \begin{table}
command but its content is defined by \begin{tabular}{}
command which defines the table spec inside curly brackets, in the provided example there are three left-justified columns \begin{tabular}{l|l|l}
(the l
stands for left-justified, c
centered and r
right-justified) separated by vertical lines (the |
create borders within columns).
The column name is defined and separated by &
command while \\
command starts a new row (e.g., Col1 & Col2 & Col3 \\
).
The \hline
command adds the horizontal separation line between column name and data.
Now, let's imagine to build and fill a more complex and full-data table, the work will be annoying and prone to human error.
Tables are not easy to manage, I recommend to use the following web-tools (for instance you can upload or copy-paste the data from Excel files):
If you need to change typographic conventions with the same input syntax in a consistent way without replacing values directly, i.e., by using SI convention, you can use siunitx
package. This package allows you to process numbers or to apply SI conventions to table. First of all you need to import the package correctly:
\usepackage{siunitx}
For example if you want to align all the values of a column by the comma, set to the center and modify the column-width you need to create a setup:
\sisetup{
table-format=2.2, % set value format
table-number-alignment=center, % set alingment
table-column-width=50pt % set column width
}
While, in the table you will need to specify the alternative column-type by S
, i.e., if you need to modify only the second column of the table you will have:
...
\begin{tabular}{l|S|c}
...
This table provides values of the first column left-aligned, the second column will take the siunitx
configuration, the third column will be aligned at the center.
Alternatively, instead of using a global configuration, you can over impose a single configuration per column-type, i.e.:
...
\begin{tabular}{
c
S[table-format=2.2]
S[table-column-width=50pt]
}
...
In this case, the value format is applied only on the second column while the third has only the column-width property.
As for the tables, sometimes you need a visual tool to write math equations, the following links will help you in the process:
- tutorialspoint - LaTeX Equation Editor
- CodeCogs - Online LaTeX Equation Editor - create, integrate and download
- HostMath - Online LaTeX formula editor and browser-based math equation editor
- Lagrida - Online LaTeX Equation Editor
While, here you can find cheat sheets of math symbols:
Sometimes you need to split a math equation on multiple lines, one possible way is to use the package amsmath
to display equations and amsfonts
which is an extended set of fonts for use in mathematics.
Required package:
\usepackage{amsmath}
\usepackage{amsfonts}
\begin{equation}
\begin{split}
\upsilon (s) = {\mathbb{E}}\left [ G_t | S_t = s \right ] \\
= {\mathbb{E}}\left [ R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ...| S_t = s \right ] \\
... \\
= {\mathbb{E}}\left [ R_{t+1} + \gamma \upsilon S_{t+1} | S_t = s \right ]
\end{split}
\label{eq:eq_name}
\end{equation}
Write equations inside the split
command and split equations with \\
command to go to the new line.
The progressive number to reference the equation will be automatically inserted in the middle of the split.
Alternatively, you can use align
to split equations.
\begin{align}
\upsilon (s) = {\mathbb{E}}\left [ G_t | S_t = s \right ] \nonumber \\
= {\mathbb{E}}\left [ R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + ...| S_t = s \right ] \nonumber \\
... \nonumber \\
= {\mathbb{E}}\left [ R_{t+1} + \gamma \upsilon S_{t+1} | S_t = s \right ] \label{eq:eq_name}
\end{align}
The difference with split
lives in adding the command \nonumber
before going to a new line and the corresponding progressive number to reference the equation can be inserted where the user wants (i.e., in the corresponding line where the label is put, in the example in the last line).
Bibligraphy management is one of the best feature of LaTeX, just put these two lines at the end of the document:
\bibliographystyle{ieeetr}
\bibliography{refs.bib}
Create a new file refs.bib
where you will add all your citation and LaTeX will compile the bibliography for you!
Also, you can select within the seven bibliography styles without any effort: plain
, acm
, ieeetr
, alpha
, abbrv
and siam
.
The references are stored in a BibTeX .bib
file. The .bib
file consists in many entries:
- entry type: can be a book, article, inproceedings (i.e., conference paper), misc, and so on;
- citekey which is the unique identifier of the entry, used to for citation with the command
\cite{}
; - title identifies the title of the entry;
- author which is the author list;
- publisher;
- DOI, ISSN, ISBN or URL, usually identifies the digital object
- year, date, month, and so on...
The following is a classical reference entry:
@misc{vaswani2017attention,
title={Attention Is All You Need},
author={Ashish Vaswani and Noam Shazeer and Niki Parmar and Jakob Uszkoreit and Llion Jones and Aidan N. Gomez and Lukasz Kaiser and Illia Polosukhin},
year={2017},
}
Recommended read
To limit the number of authors in the bibliography by showing only a defined number of author names and by adding the abbrevation "and others" (et al. in latin), we need to manage directly parameters of BIBTeX style file (.bst file
, according to How to Use the IEEEtran BIBTEX Style).
The parameters below set when to apply the abbrevation et al.
. In particular:
CTLuse_forced_etal
forces the abbreviation;CTLmax_names_forced_etal
set the maximum number of authors without the abberviation;CTLnames_show_etal
set the number of authors before the abbreviation.
The following list of parameters set the abbrevation et al.
only for reference with more than two authors, when the number of authors exceed the limit of two (i.e., three authors) it applies the abbreviation starting from the first author name. These parameters should be placed at the beginning of your bibliography .bib
file.
@IEEEtranBSTCTL{IEEEexample:BSTcontrol,
CTLuse_forced_etal = "yes",
CTLmax_names_forced_etal = "2",
CTLnames_show_etal = "1" }
Then you need to add the following command right after \begin{document}
in the main LaTeX file to apply changes to the BIBTeX style:
\bstctlcite{IEEEexample:BSTcontrol}
If you have just started your adventure in the research field there is a high probabily that you have already encountered arXiv. arXiv is a free distribution service and an open-access archive scholarly articles, although you can expoert directly the BibTeX formatted citation you need to properly cite the article by adding a the arXiv identifier to the note of the citation. For instance, for the paper Attention is all you need the formatted citation is the following:
@misc{https://doi.org/10.48550/arxiv.1706.03762,
doi = {10.48550/ARXIV.1706.03762},
url = {https://arxiv.org/abs/1706.03762},
author = {Vaswani, Ashish and Shazeer, Noam and Parmar, Niki and Uszkoreit, Jakob and Jones, Llion and Gomez, Aidan N. and Kaiser, Lukasz and Polosukhin, Illia},
keywords = {Computation and Language (cs.CL), Machine Learning (cs.LG), FOS: Computer and information sciences, FOS: Computer and information sciences},
title = {Attention Is All You Need},
publisher = {arXiv},
year = {2017},
copyright = {arXiv.org perpetual, non-exclusive license}
}
Just add the related arXiv identifier with note = {arXiv:1706.03762}
.
Adding citation and reference is the main and most profilic advantage of LaTeX and are the first commands that you will learn during your journey.
After having added your citation in the bibliography you can cite it just adding the following command in the text:
\cite{what_we_want_to_cite}
While, after having added your tables, figures, equations and so on, use the following command to add reference in the text:
\ref{what_we_want_to_refer}
As a trick, I suggest to you to add to the command \label{}
a little tag to distinguish between figures, tables, equations, and so on.
E.g. figures can be labelled as \label{fig:figure_name}
, tables as \label{tab:table_name}
or equations \label{eq:equation_name}
, this will help you during writing to remember to what object are adding the reference.
Using the tilde character ~
(CTRL+0126 on Windows) before \cite{}
or \ref{}
place an unbreakable space between the text and the following citation or reference, this will help in the reading process without breaking it between lines. For example:
... as shown in Fig.~\ref{fig:sample} ...
Sometimes you need to refer from a main document, such as you have a main article and a supplementary standalone document which uses some references from the main source.
Cross referencing from an external documents is based on three files: main article (i.e., main.tex
), supplementary or appendix (i.e., supplementary.tex
) and a bridge function (i.e., latexmkrc
). A more detailed guide has been described on Overleaf.
First, we need the xr
package in both the two documents (i.e., main.tex
and supplementary.tex
):
\usepackage{xr}
Then we create a new file called latexmkrc
which is a function to correctly build the LaTeX files, in latexmkrc
write the following function:
add_cus_dep( 'tex', 'aux', 0, 'makeexternaldocument' );
sub makeexternaldocument {
if (!($root_filename eq $_[0]))
{
# FOR PDFLATEX
system( "latexmk -cd -pdf \"$_[0]\"" );
# FOR LATEX+DVIPDF
# system( "latexmk -cd \"$_[0]\"" );
# FOR XELATEX
# system( "latexmk -cd -xelatex \"$_[0]\"" );
# FOR LUALATEX
# system( "latexmk -cd -lualatex \"$_[0]\"" );
}
}
When our main article file has been finished with all the references we can start writing the supplementary standalone file (which can be an appendix, a summary or whatever).
This standalone file (i.e., our supplementary.tex
) uses references from the main document (i.e., main.tex
), to allow Overleaf taking references from the main document we need to add and edit the following code which acts like a bridge with the source file:
\makeatletter
\newcommand*{\addFileDependency}[1]{
\typeout{(#1)}
\@addtofilelist{#1}
\IfFileExists{#1}{}{\typeout{No file #1.}}
}
\makeatother
\newcommand*{\myexternaldocument}[1]{
\externaldocument{#1}
\addFileDependency{#1.tex}
\addFileDependency{#1.aux}
}
\myexternaldocument{main}
The command \myexternaldocument{main}
specifies the main document for references, and now the supplementary file takes the references from that external document and can be compiled without any missing reference warning.
Here you will find useful misc arguments:
Sometimes you need to create your personal commands, LaTeX provides you a simple setup for the most diverse functions, for example: are you tired to make text in bold used as TODO reminder? Easy, create a new command with \newcommand{\command_name}
with your preferrend \command_name
and apply whatever you want:
\newcommand{\todo} [1] {\textcolor{Red}{\textbf{TODO:} #1}}
The new \todo
command will automatically type "TODO:" followed by the text red and bold.
While writing a paper (for instance in the traditional two-column layout) we might be interested if we can make better styling choice by knowing the margin of the chosen template. In this case the package showframe will help us:
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{insert_thickness}
\renewcommand*\ShowFrameColor{\color{insert_color}}
By adding the showframe package we decide how many points the frame thickness will be with the command \renewcommand\ShowFrameLinethickness{0.15pt}
(i.e., for a frame of 0.15pt) and the corresponding color with the command \renewcommand*\ShowFrameColor{\color{red}}
(i.e., for red frame).
The following is an example of IEEE Access Template with page layout borders: