LaTeX for Scientists companion guide
Welcome to the LaTeX for Scientists companion guide! This page contains all the information referenced in my YouTube video series of the same name. Together, we’ll create a beautiful template that you can endlessly use and re-use for all your LaTeX documents. We’ll include all the packages and commands you need to know as a STEM (Science, Technology, Engineering, Mathematics) student, without bogging you down in details you might never use. We spend a little more time up-front in creating a template, but we only have to do this once – so we actually save a lot of time in the long run!
You don’t need any prior knowledge of LaTex or computer programming. The videos in this series are short, each covering very specific topics so that you can pick and choose to watch the videos relevant to your needs. Clicking the links in this table of contents will take you to the final code (and other contents) for that episode. To view a particular video on YouTube, click on the video thumbnail you’re interested in.
Table of contents:
- Introduction: LaTeX vs. Word
- Overleaf Quick-Start Guide
- Maths for beginners
- Figures
Series Introduction: LaTeX vs. Word
This 5-minute introductory video is a tongue-in-cheek look at why LaTeX is so great for scientists and why you should abandon Word (or Google Docs) immediately and learn LaTeX instead. This episode has no code.
Episode 1: Overleaf Quick-Start Guide
Final code produced in Episode 1
The following code is the same as what you should end up with by the end of Episode 1, but with additional comments.
I recommend typing along with the video, since the process of typing the code out helps it to stick in your memory. Coding is one of those activities that you get better at by constantly practising, so take the opportunity wherever you can! The earlier the better, too. Ideally, long before you have to write a dissertation or thesis.
However, if you struggle to keep up with the video you can copy/paste this directly into your LaTeX main.tex file, and follow along line-by-line if you prefer to do that. Whatever works for you! This code (if copy/pasted) will compile for you without any errors. Anything with a % symbol in front of it is a comment, and will be ignored by the compiler. If your compiler gives you errors, run through each line in turn and make sure that all open squiggly brackets are paired with a closing squiggly bracket! This is the most common source of errors for beginners.
% ==========================================
% This part of the document is the PREAMBLE this is where you tell the compiler how you
% want the document to look. Most of the formatting commands are contained within the
% document class file, so you only need to add more if you decide to change some
% aspect of the default style.
% ==========================================
\documentclass[a4paper, 11pt]{article}
% These two packages should be called in this order.
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
% This lets you call in dummy paragraphs to fill out your template
\usepackage{lipsum}
% The geometry package lets you control the page geometry, including margin sizes.
% Comment/uncomment one of these usepackage lines, as you prefer!
% This will make all the margin sizes the same:
\usepackage[margin=2cm]{geometry}
% This will let you control the margin sizes separately:
%\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
% This package will let you use and style links within your PDF output
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue}
% The title, author, and date commands are only needed if you use the \maketitle
% command in the BODY. If you don't want any of these, you can just delete \maketitle.
\title{My Template}
\author{Dr. Sally}
\date{} % You can leave the date empty, if you don't want to show a date.
% ==========================================
% This is the end of the PREAMBLE.
% Everything after \begin{document} is the BODY.
% ==========================================
\begin{document}
% Prints the title and table of contents (in this order)
\maketitle
\tableofcontents
% In addition to section, you can use \chapter, \subsection, or \subsubsection
\section{Introduction}
% This calls in a dummy Latin paragraph to fill out some space in the template.
% The number can be from 1 to 150 (inclusive)
\lipsum[1] % Calls paragraph 1 from the "lorem ipsum" text
\lipsum[2-5] % Calls paragraphs 2 to 5
\section{Method}
% The contents of the file "Method.tex" will be entered here:
\input{Method.tex}
\end{document}
Episode 2: Maths for beginners
Final code produced in Episode 2
The code from this episode is split into two parts, since we are now working with two files: our “main.tex” file from tutorial 1, and a new file that we create during tutorial 2 called “maths.tex”. You need to input this maths.tex file into main.tex in order to get your maths to show. If you do not know how to create and input additional files in LaTeX, I highly recommend watching tutorial 1 first, although I do cover this briefly at the start of tutorial 2 (for practice).
The main.tex file should contain the same code as was created in tutorial 1. If you did not watch that episode, you can copy and paste the code from this webpage into your main.tex file. Inside this main.tex file, you will need to include the following lines in the preamble. This will ensure that the required maths packages are called, which will make sure the mathematical code compiles correctly. Again, you can copy and paste this code directly.
% ==========================================
% This section of code goes in your preamble (inside main.tex)
% ==========================================
% These two packages are almost equivalent, and mathtools includes the amsmath package.
% You don't need both, but you do need to call one of them if you do any maths at all.
\usepackage{mathtools}
%\usepackage{amsmath}
% This package allows you to show Greek characters in upright font rather than the default
% italics.
\usepackage{upgreek}
% Creates a new command of our own choosing, in the format:
% \newcommand{\}{}
\newcommand{\dg}{$^{\circ}$}
After you have set up your preamble to call the required packages, you can copy and paste the following code directly into maths.tex file.
% ==========================================
% This section of code goes into maths.tex
% ==========================================
\subsection{The "Equation" environment}
% Recall that you can suppress numbering using equation* in the brackets instead of equation
\begin{equation}
y = mx + c
\label{eq:gradient}
\end{equation}
As one can see in Equation \ref{eq:gradient} \lipsum[1]
\subsection{The "Inline" environment}
\lipsum[2] Something something $\sigma$-clipping. $y=mx+c$.
% For upper-case Greek, capitalise the first letter (e.g., \Sigma)
\subsection{Subscript, Superscript}
\begin{equation}
\int_0^1 x \mathrm{d}x
\end{equation}
Observe the difference between $H_0$ and H$_0$, and the difference between $x^-1$ and $x^{-1}$.\\
As shown later in the video, we can use the mathrm{} command to make the `d' in our integral upright.
\subsection{Definitions}
$^{\circ}$ is not the same as using o, because the letter o has typically thicker strokes at the sides, whereas the circle character has the same width all the way around. Here we'll look at an example using the definition we created: \\ % The double backslash can be used to create a new line anywhere.
Using \dg K will allow us to print degrees (Kelvin) as a unit, while typing the command with the second backslash attached to the \dg\ symbol will print a regular space immediately after it. \\
You can also use \quad (\underline{\quad}) to create a full letter-sized space anywhere you like, while \, (\underline{\,}) will create a small space that can be used to create subtle improvements in spacing. These commands work in both math and text environments. You can also use the \: and \; commands for medium (\underline{\:}) and large (\underline{\;}) spaces, respectively.
\subsection{Fractions}
This is a fraction in the inline (text) style: $(\frac{1}{2})$, vs. display style forced into the inline (text) environment: $\left( \dfrac{3}{2} \right)$. Note how this slightly increases the spacing between the lines in order to accommodate the taller display-style fraction. Note also the difference between scaled (with left and right) brackets and non-scaled brackets.
\begin{equation*}
\text{This is the default display style:} \left( \frac{1}{2} \right)
\text{and the forced text style:} (\tfrac{5}{3})
\end{equation*}
You can create slanted fractions by using a combination of subscripts and superscripts. The output is a bit easier to read than when using text style fractions: $^1/_2$ vs. $\frac{1}{2}$. If you use slanted fractions often, you can check out the `xfrac' or `nicefrac' packages might add some convenience.
You can obtain finer control over the size of scaled brackets using big, Big, bigg, and Bigg in place of left/right (listed in order of increasing size). For example: (($^1/_2$)) vs. \big(($^1/_2$)\big) vs. \Big(($^1/_2$)\Big) vs. \bigg(($^1/_2$)\bigg) vs. \Bigg(($^1/_2$)\Bigg).
\subsection{Matrices}
A simple 2x2 matrix/array. Round brackets can be added using pmatrix, and square brackets with bmatrix. In this way you can avoid specifying the brackets manually.
\begin{equation*}
\begin{matrix}
1 & 2 \\
3 & 4
\end{matrix}
\quad \text{vs.} \quad
\begin{pmatrix}
1 & 2 \\
3 & 4
\end{pmatrix}
\end{equation*}
You can improve the alignment using the matrix*/pmatrix*/bmatrix* environment, and then specifying the optional alignment of left(l), center (c), or right(r) inside square brackets that immediately follow the begin{matrix*} command.
\begin{equation*}
\begin{matrix}
-1 & 2 & 3 \\
4 & -5 & 6 \\
7 & 8 & -9
\end{matrix}
\quad = \quad
\begin{matrix*}[r]
-1 & 2 & 3 \\
4 & -5 & 6 \\
7 & 8 & -9
\end{matrix*}
\end{equation*}
An example of the `align' environment, with the align* variant used to suppress all line numbering. Note that unlike `matrix', `align' acts as its own math environment and so doesn't need to be specifically placed inside a maths environment to work.
\begin{align*}
y &= f(x) \\
f(x) &= mx + c
\end{align*}
Our second example, with the numbering suppressed only on the first line.
\begin{align}
E &= \frac{mc^2}{\sqrt{1-\frac{v^2}{c^2}}} \nonumber \\
&= \gamma mc^2
\end{align}
\subsection{Formatting text}
You can create regular text inside a math environment with the `text' command: $\text{something something text formatting}$ \\
And you can make individual characters upright with mathrm{} or textrm{} commands. You specify bold face using mathbf{} and textbf{}. It's worth noting that mathrm/mathbf and textbf/textbf have the same behaviour, in that the math versions will treat their contents as maths and won't respect the spacing you include. This means that if you need to apply formatting to strings of text, `textrm' and `textbf' are preferred. E.g.,\\
$\textbf{something something textbf}$ vs. $\mathbf{something something mathbf}$\\
For individual letters, the math and text commands are interchangeable, but only the text versions work outside of a math environment (i.e., in regular paragraphs). \\
Units should be shown upright, but \LaTeX can be a bit funny about formatting Greek characters. The default $\mu$m is incorrect and $\mathrm{\mu}$m won't fix it. You need to use the `upgreek' package, and then can prefix the letter name with `up', so use `upmu' instead of `mu': $\upmu$m.
\subsection{Making it gorgeous!}
\begin{equation}
i\hbar \, \frac{\partial}{\partial t} \Psi(\mathbf{r}, t) =
\left[- \, \frac{\hbar^2}{2m} \nabla^2 + V(\mathbf{r}) \right]
\Psi(\mathbf{r}, t)
\end{equation}
Episode 3: Figures
Final code produced in Episode 3
The code in this episode is very short and only needs main.tex. It includes the main.tex features from all the previous episodes, so if you copy and paste this code into your main.tex file, your previous code should still work. Let me know if you run into any problems!
\documentclass[a4paper, 12pt, twocolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\usepackage[margin=2cm]{geometry}
\usepackage{hyperref}
\hypersetup{colorlinks=true, linkcolor=blue}
% These two packages are almost equivalent, and mathtools includes the amsmath package. You don't need both, but you do need to call one of them if you do any maths at all.
\usepackage{mathtools}
%\usepackage{amsmath}
% This package allows you to show Greek characters in upright font rather than the default italics.
\usepackage{upgreek}
% This package helps LaTeX to understand and handle figures.
\usepackage{graphicx}
% Creates a new command of our own choosing, in the format:
% \newcommand{\}{}
\newcommand{\dg}{$^{\circ}$}
\title{\LaTeX for Scientists}
\author{Dr. Sally}
\date{}
% ==== DOCUMENT BODY ==== %
\begin{document}
% Prints the title using the details you input in the preamble.
\maketitle
% Prints the table of contents, using the sections and subsection headings you create in the document.
\tableofcontents
% Prints a list of all the figures printed in the document.
\listoffigures
\section{Introduction}
\begin{figure}[h!] % Options are h (here), t (top), b (bottom), and p (for a new page). Exclamation mark creates priority.
\centering
\includegraphics[width=0.9\columnwidth]{image} % For most documents, you'll use either \textwidth or \columnwidth to scale by. The number can be <1 (to make the image smaller than the text or column) or >1 (for larger).
\caption[Very Important Figure]{Very Important Figure - don't forget to like and subscribe! *shameless plug lol*} % Your figure caption goes in the squiggly brackets {}. You only need to include the short caption in [] if you're printing a list of figures.
\label{fig:my_label}
\end{figure}
As one can see in Fig. \ref{fig:my_label} \lipsum[1]
\section{Method}
\input{Method}
%\section{Maths}
%\input{Maths.tex}
\end{document}
Episode 4: Tables
Final code produced in Episode 4
![]()
This code will need you to call the lscape package in the preamble of your main document in order for the landscape page part to work, but otherwise requires no special packages. Everything you need to create basic tables is already included in LaTeX. You can either copy/paste this code into the body of your document directly, or put it into a file and call that file in your main.tex using \include{filename.tex}, as we have done previously!
\subsection{Super basic table}
\begin{table}[h!] % Your options here are "h", "t", "b", "p", and "!". Some people use "H" instead of "h!", which does the same thing but slightly less reliably.
\centering % Centres your table on the page
\begin{tabular}{c|c} % The vertical bar for creating vertical borders is (on a QWERTY keyboard in English mode) achieved using shift + backslash (next to the Z key)
Heading for column 1 & Heading for column 2 \\
Column 1, row 1 & Column 2, row 1 \\
Column 1, row 2 & Column 2, row 2 \\
Column 1, row 3 & Column 2, row 3 \\
\end{tabular}
\caption[Super basic first table.]{Don't forget to write your caption here. \LaTeX\ will automatically put it in the right place, and give it the correct numbering.}
\label{tab:my_label} % Don't forget to give your table a unique label, to make it easier for you to reference it in the text using \ref{table name}
\end{table}
% Clearpage will clear out the rest of the page and start whatever comes after it on a new page.
\clearpage
\subsection{Default table stretch, with and without cell borders}
These two tables are just examples using random numbers, so you can see how the same table looks with and without cell borders. I've taken the labels out since I'm not referring to any of them in the text. In a scientific document, though, you should always include a label for every table since you should never include something that isn't discussed in the text!
\begin{table}[h]
\centering
\begin{tabular}{|r|r|r|r|r|r|}
\hline
Test 1 & Test 2 & Test 3 & Test 4 & Test 5 & Test 6 \\
\hline
1.6 & 2.0 & 5.0 & 8.00 & 7.00 & 12.25 \\
\hline
2.8 & 3.3 & 2.2 & 11.50 & 13.33 & 10.33 \\
\hline
4.0 & 4.0 & 2.8 & 5.00 & 7.67 & 7.25 \\
\hline
2.4 & 1.4 & 1.4 & 11.50 & 9.33 & 11.67 \\
\hline
4.0 & 1.9 & 1.4 & 11.33 & 15.00 & 6.67 \\
\hline
3.7 & 1.5 & 2.2 & 5.25 & 5.50 & 7.75 \\
\hline
3.7 & 1.9 & 4.0 & 9.67 & 16.67 & 11.00 \\
\hline
2.0 & 3.0 & 4.8 & 7.50 & 9.00 & 16.67 \\
\hline
2.2 & 2.3 & 1.6 & 5.00 & 12.00 & 11.67 \\
\hline
\end{tabular}
\caption[Default table stretch, with all cell borders.]{This table demonstrates the default table stretch, with borders around all of the cells.}
\end{table}
\begin{table}[h]
\centering
\begin{tabular}{r r r r r r}
Test 1 & Test 2 & Test 3 & Test 4 & Test 5 & Test 6 \\
\hline
1.6 & 2.0 & 5.0 & 8.00 & 7.00 & 12.25 \\
2.8 & 3.3 & 2.2 & 11.50 & 13.33 & 10.33 \\
4.0 & 4.0 & 2.8 & 5.00 & 7.67 & 7.25 \\
2.4 & 1.4 & 1.4 & 11.50 & 9.33 & 11.67 \\
4.0 & 1.9 & 1.4 & 11.33 & 15.00 & 6.67 \\
3.7 & 1.5 & 2.2 & 5.25 & 5.50 & 7.75 \\
3.7 & 1.9 & 4.0 & 9.67 & 16.67 & 11.00 \\
2.0 & 3.0 & 4.8 & 7.50 & 9.00 & 16.67 \\
2.2 & 2.3 & 1.6 & 5.00 & 12.00 & 11.67 \\
\end{tabular}
\caption[Default table stretch, typical cell borders.]{This table demonstrates the default table stretch, but uses the absolute minimum of cell borders.}
\end{table}
\clearpage
\subsection{Vertical table stretching}
% The begingroup and endgroup commands create a self-contained little unit, so that any commands that you want to apply only to a specific object (like the arraystretch command, which I only wanted to apply to this table) will not affect any other objects in the document.
\begingroup
\renewcommand{\arraystretch}{1.5}
\begin{table}[h]
\centering
\begin{tabular}{r r r r r r}
Test 1 & Test 2 & Test 3 & Test 4 & Test 5 & Test 6 \\
\hline
1.6 & 2.0 & 5.0 & 8.00 & 7.00 & 12.25 \\
2.8 & 3.3 & 2.2 & 11.50 & 13.33 & 10.33 \\
4.0 & 4.0 & 2.8 & 5.00 & 7.67 & 7.25 \\
2.4 & 1.4 & 1.4 & 11.50 & 9.33 & 11.67 \\
4.0 & 1.9 & 1.4 & 11.33 & 15.00 & 6.67 \\
3.7 & 1.5 & 2.2 & 5.25 & 5.50 & 7.75 \\
3.7 & 1.9 & 4.0 & 9.67 & 16.67 & 11.00 \\
2.0 & 3.0 & 4.8 & 7.50 & 9.00 & 16.67 \\
2.2 & 2.3 & 1.6 & 5.00 & 12.00 & 11.67 \\
\end{tabular}
\caption[Vertical stretch example, arraystretch value 1.5]{Using an arraystretch value greater than one (in this case 1.5) will spread the rows of the table apart.}
\end{table}
\endgroup
\begingroup
\renewcommand{\arraystretch}{0.5}
\begin{table}[h]
\centering
\begin{tabular}{r r r r r r}
Test 1 & Test 2 & Test 3 & Test 4 & Test 5 & Test 6 \\
\hline
1.6 & 2.0 & 5.0 & 8.00 & 7.00 & 12.25 \\
2.8 & 3.3 & 2.2 & 11.50 & 13.33 & 10.33 \\
4.0 & 4.0 & 2.8 & 5.00 & 7.67 & 7.25 \\
2.4 & 1.4 & 1.4 & 11.50 & 9.33 & 11.67 \\
4.0 & 1.9 & 1.4 & 11.33 & 15.00 & 6.67 \\
3.7 & 1.5 & 2.2 & 5.25 & 5.50 & 7.75 \\
3.7 & 1.9 & 4.0 & 9.67 & 16.67 & 11.00 \\
2.0 & 3.0 & 4.8 & 7.50 & 9.00 & 16.67 \\
2.2 & 2.3 & 1.6 & 5.00 & 12.00 & 11.67 \\
\end{tabular}
\caption[Vertical stretch example, arraystretch value 0.5]{Using an arraystretch value of less than one (in this case 0.5) will bring the rows of the table closer together.}
\end{table}
\endgroup
\clearpage
\subsection{Horizontal table stretching (explicitly setting the column width)}
Explicitly declaring your column widths is the best way of controlling the size (width) of your tables. It doesn't matter if the page is landscape or portrait, you can always use this method.
\begin{table}[h]
\begin{tabular}{ c c c }
\hline
First Row & -6 & the quick brown fox jumps over the lazy dog \\
Second Row & 4 & 10\\
Third Row & 20 & 30\\
Fourth Row & 100 & -30\\
\hline
\end{tabular}
\caption[Horizontal stretching example, widths set by the compiler.]{Column widths as set by the \LaTeX\ compiler. The columns are made as wide as they need to be and text will fall off the page rather than be wrapped onto the next line.}
\end{table}
\begin{table}[h]
\begin{tabular}{ p{3.5cm} p{2.2in} p{0.25\textwidth} }
\hline
First Row & -6 & the quick brown fox jumps over the lazy dog \\
Second Row & 4 & 10\\
Third Row & 20 & 30\\
Fourth Row & 100 & -30\\
\hline
\end{tabular}
\caption[Horizontal stretching example, widths declared explicitly by the user.]{Column widths explicitly declared by the user. This takes a little trial and error to get them looking the way you want, but does have the handy added bonus of providing a way of wrapping text onto the next line (if the text is longer than the column width you specified).}
\end{table}
\clearpage
\subsection{The other horizontal table stretching - Landscape tables}
If you have a lot of columns in your data table (to be included in an Appendix, for example), it is often better to use a landscape page. Remember to call the lscape package in your preamble, otherwise this won't work!
Anything included between the begin and end landscape ``brackets'' will also be put onto a landscape page.
(Also, note this big empty space here!...see the page after the table for more info!)
\begin{landscape}
\begin{table}
\centering
\begin{tabular}{r r r r r r}
Test 1 & Test 2 & Test 3 & Test 4 & Test 5 & Test 6 \\
\hline
1.6 & 2.0 & 5.0 & 8.00 & 7.00 & 12.25 \\
2.8 & 3.3 & 2.2 & 11.50 & 13.33 & 10.33 \\
4.0 & 4.0 & 2.8 & 5.00 & 7.67 & 7.25 \\
2.4 & 1.4 & 1.4 & 11.50 & 9.33 & 11.67 \\
4.0 & 1.9 & 1.4 & 11.33 & 15.00 & 6.67 \\
3.7 & 1.5 & 2.2 & 5.25 & 5.50 & 7.75 \\
3.7 & 1.9 & 4.0 & 9.67 & 16.67 & 11.00 \\
2.0 & 3.0 & 4.8 & 7.50 & 9.00 & 16.67 \\
2.2 & 2.3 & 1.6 & 5.00 & 12.00 & 11.67 \\
\end{tabular}
\caption[Table on a landscape page]{This table is on a landscape page!}
\end{table}
\end{landscape}
You can put text on a landscape page as well. Anything inside the begin and end landscape commands will be shown on a landscape page. BUT - because these commands will clear out the rest of the page, the compiler won't automatically wrap anything that appears after this table to fill in the gaps before it. So this piece of text will be shown on a new page \textit{after} the landscape page. If you want to wrap text to appear before the table, you'll need to place it before the table manually.
</code
