Intro to LaTeX Workshop
Crystal Nguyen
University of North Carolina at Chapel Hill
10/13/2017
Nguyen (UNC) LaTeX Workshop 10/13/2017 1 / 31
Overview
1 Getting Started in LaTeX
2 Typesetting Equations
3 Lists
4 Floats
5 Including Code
6 My Project is Finished. Now What?
7 Tips and Tricks
8 Articles
9 Presentations
10 Conclusion
Nguyen (UNC) LaTeX Workshop 10/13/2017 2 / 31
What is LaTeX?
A text editor for more advanced documents, especially those including
complex tables and equations
Uses “code” to create a PDF
Useful for projects, homework, manuscripts, quals, posters, figures
Examples (These are local to my computer and won’t open if you
click them.)
Conference Slides
760 homework
Applied qualification exams
Figure
Nguyen (UNC) LaTeX Workshop 10/13/2017 3 / 31
Getting Started
1 Go to the BSA website and dowload slides:
bsa.web.unc.edu/computing/latex/
2 Download the workshop .zip here (Solutions here)
3 Go to Overleaf and sign up with any email
4 Sign in, and click the green upload button on the upper left corner
5 Select “upload zip”
6 Select the .zip downloaded from the BSA website
7 Change the name, date, and title in your document accordingly
Nguyen (UNC) LaTeX Workshop 10/13/2017 4 / 31
Getting Started
\section{Homework Examples}
\begin{enumerate}
\item
\end{enumerate}
% is how to comment the code
\section
{Homework Examples}
Nguyen (UNC) LaTeX Workshop 10/13/2017 5 / 31
Equation Environments
$’s allow you to enter in and out of equation mode.
Exercise 1: In words, describe what m and b are in the equation
y = mx + b.
$m$ is the slope and $b$ is the intercept.
Nguyen (UNC) LaTeX Workshop 10/13/2017 6 / 31
Equation Environments
$’s allow you to enter in and out of equation mode.
Double $ centers the equation.
Exercise 2: Write y = mx + b as a centered equation.
$$y = mx + b$$
Nguyen (UNC) LaTeX Workshop 10/13/2017 7 / 31
Equation Environments
$’s allow you to enter in and out of equation mode.
Double $ centers the equation.
The equation environment will also give you centered equations.
Exercise 3: Write y = mx + b as a centered equation in the equation
environment.
\begin{equation} y = mx + b \end{equation}
Nguyen (UNC) LaTeX Workshop 10/13/2017 8 / 31
Equation Environments
Alignment in LaTeX:
Align columns with & symbols
Align rows with \\
Notice the nested enumerate environments so that we have problems 3a
and 3b.
Nguyen (UNC) LaTeX Workshop 10/13/2017 9 / 31
Equation Environments
Exercise 3a: Rewrite the equation using the equation and aligned
environments.
\begin{equation}
\begin{aligned}
& & y & = mx + b \\
& & & = (m \times x) + b \\
& \implies & 0 & = \{(m \times x) + b\} - y
\end{aligned}
\end{equation}
Nguyen (UNC) LaTeX Workshop 10/13/2017 10 / 31
Equation Environments
Exercise 3b: Split the last line of 3a onto two different lines
0 = {(m × x)
+ b} y
\begin{equation}
\begin{aligned}
0 &= \{(m \times x) \\
&\hspace{1cm} + b \} - y \nonumber
\end{aligned}
\begin{equation}
Nguyen (UNC) LaTeX Workshop 10/13/2017 11 / 31
Piecewise functions
Exercise 4: Use alignment syntax and the cases environment to make the
following piecewise function
y =
(
x, 0 < x < 1
mx + b, x 1
$$ y = \begin{cases}
x, & 0 < x < 1
mx + b, & x \geq 1
\end{cases} $$
Nguyen (UNC) LaTeX Workshop 10/13/2017 12 / 31
Matrices and vectors
What if we have matrices and vectors? Exercise 5: Replicate the following.
E[Y |X] = β
1 0 0
0
.
.
.
0
0 0 1
+
$$ E[\bs{Y}|\bs{X}] = \bs\beta \begin{bmatrix}
1 & 0 & 0 \\
0 & \ddots & 0 \\
0 & 0 & 1
\end{bmatrix}
+ \bs{\epsilon} $$
Nguyen (UNC) LaTeX Workshop 10/13/2017 13 / 31
More Math Practice
Exercise 6: Replicate the following.
s =
P
n
i=1
(y
i
y)
2
n 1
1/2
$$ s = \l( \frac{ \sum {i=1}^n (y i - \overbar{y})^2 }{n - 1} \r)^{1/2} $$
Nguyen (UNC) LaTeX Workshop 10/13/2017 14 / 31
Lists
Use itemize instead of enumerate to make a bulleted list
Sometimes we want to skip numbers or letters in enumerate
Note the commands \addtocounter{enumi}{3} and
\addtocounter{enumii}{2} in our next section
The number of i’s in “enumi” the first set of brackets determine what
level of the enumerate to add a counter to
The number in the second set of brackets determines how many to
add to the counter (Does not set the counter to that value)
Nguyen (UNC) LaTeX Workshop 10/13/2017 15 / 31
Floats
Tables, figures, and equation environments are what are called floats
LaTeX will try to put these wherever it thinks is best
It is generally best to add these to your document last and then play
around with how to position it properly
Nguyen (UNC) LaTeX Workshop 10/13/2017 16 / 31
Figures
Typically, you would do the following, but because we uploaded the entire
.zip, we can skip steps 1-2 for now.
1 Select PROJECT > Files... > Computer
2 Drag and drop or select the image file from your downloaded folder.
3 Use the following code to include the image in your document.
Exercise 4c:
\begin{figure}[h!]
\centering
\includegraphics[scale = 0.3]{Histogram.png}
\label{fig:malezinc} \caption{A histogram of serum
zinc levels in men.}
\end{figure}
Nguyen (UNC) LaTeX Workshop 10/13/2017 17 / 31
Figures
The [h!] forces the location as best as possible, based on the size of
the image. Some options are t for top, b for bottom, h for here
(inline).
You can play with the [scale = ] option to resize. Instead, you
could use [height = <value>, width = <value>,
keepaspectratio]
You can also include a command in the figure environment,
\label{<name>}. Then in your article text, you can use Figure
\ref{<name>} to correctly call the figure number, e.g., “Figure 1”
Nguyen (UNC) LaTeX Workshop 10/13/2017 18 / 31
Tables
\begin{table}[h!]
\caption{Parameter estimates for Model 1.}
\centering
\begin{tabular}{ccc}
\hline
Effect & Variable & Estimate \\
\hline
Intercept & $\beta_0$ & 0.45 \\
Gender & $\beta_1$ & 4.74 \\
Age & $\beta_2$ & 7.32 \\
\hline
\end{tabular}
\label{tab:Model1}
\end{table}
Now, try changing one of the c’s after tabular to an l or r. Then try
adding a | between them. Note the changes in your table.
Nguyen (UNC) LaTeX Workshop 10/13/2017 19 / 31
Tables
\begin{table}[h!]
\caption{Parameter estimates for Model 1.}
\centering
\begin{tabular}{ccc}
\hline
Effect & Variable & Estimate \\
\hline
Intercept & $\beta_0$ & 0.45 \\
Gender & $\beta_1$ & 4.74 \\
Age & $\beta_2$ & 7.32 \\
\hline
\end{tabular}
\label{tab:Model1}
\end{table}
Now, try changing one of the c’s after tabular to an l or r. Then try
adding a | between them. Note the changes in your table.
Nguyen (UNC) LaTeX Workshop 10/13/2017 19 / 31
Including Code
We sometimes wish to include our code as an appendix or in a
problem
We can use the lstlisting environment for R or the minted
environment for SAS
Whatever is typed in this environment is produced exactly as is in the
document in a different font
Nguyen (UNC) LaTeX Workshop 10/13/2017 20 / 31
Including Code
R in text
\begin{lstlisting}[language = R, caption = R Code]
x <- rnorm(100)
\end{lstlisting}
R file
\lstinputlisting{<filename>.R}
SAS in text
\begin{lstlisting}[language = SAS, caption = SAS Code]
PROC FREQ ...
\end{lstlisting}
Nguyen (UNC) LaTeX Workshop 10/13/2017 21 / 31
My Project is Finished. Now What?
To download a copy of the pdf, simply click PDF
To download a copy of the pdf, tex file, and all accompanying files,
select PROJECT > DOWNLOAD AS ZIP
Works similarly to Google Docs
You can also create templates and quickly make copies on Overleaf
Nguyen (UNC) LaTeX Workshop 10/13/2017 22 / 31
Tips and Tricks
Use \text{} to include text in equation environments.
\textbf{text} will produce boldface font
\emph{text} will produce italicized font
\hat{} and \tilde{} work similarly to \overbar{}
Use the tilde apostrophe to make “quotes” face the correct way.
Nguyen (UNC) LaTeX Workshop 10/13/2017 23 / 31
Tips and Tricks
Use \text{} to include text in equation environments.
\textbf{text} will produce boldface font
\emph{text} will produce italicized font
\hat{} and \tilde{} work similarly to \overbar{}
Use the tilde apostrophe to make “quotes” face the correct way.
Nguyen (UNC) LaTeX Workshop 10/13/2017 23 / 31
Tips and Tricks
Use \text{} to include text in equation environments.
\textbf{text} will produce boldface font
\emph{text} will produce italicized font
\hat{} and \tilde{} work similarly to \overbar{}
Use the tilde apostrophe to make “quotes” face the correct way.
Nguyen (UNC) LaTeX Workshop 10/13/2017 23 / 31
Tips and Tricks
Use \text{} to include text in equation environments.
\textbf{text} will produce boldface font
\emph{text} will produce italicized font
\hat{} and \tilde{} work similarly to \overbar{}
Use the tilde apostrophe to make “quotes” face the correct way.
Nguyen (UNC) LaTeX Workshop 10/13/2017 23 / 31
Tips and Tricks
Use \text{} to include text in equation environments.
\textbf{text} will produce boldface font
\emph{text} will produce italicized font
\hat{} and \tilde{} work similarly to \overbar{}
Use the tilde apostrophe to make “quotes” face the correct way.
Nguyen (UNC) LaTeX Workshop 10/13/2017 23 / 31
Tips and Tricks
We can make different colored text with \{red}{text}
If you are including a float in your homework for a part of a question
(say problem 10c, for example), LaTeX may not place the float where
you like it with respect to the problem numbering. If you add text
before your table or figure environment, this should make it more
aesthetically pleasing.
Rmarkdown package in R allows you to write up your LaTeX file in R
so you can more directly import tables and figures as you edit them.
You can also create presentations in LaTeX (called beamer). A
template is available on the BSA website
Cheatsheet and symbols .pdfs are useful resources.
Nguyen (UNC) LaTeX Workshop 10/13/2017 24 / 31
Tips and Tricks
We can make different colored text with \{red}{text}
If you are including a float in your homework for a part of a question
(say problem 10c, for example), LaTeX may not place the float where
you like it with respect to the problem numbering. If you add text
before your table or figure environment, this should make it more
aesthetically pleasing.
Rmarkdown package in R allows you to write up your LaTeX file in R
so you can more directly import tables and figures as you edit them.
You can also create presentations in LaTeX (called beamer). A
template is available on the BSA website
Cheatsheet and symbols .pdfs are useful resources.
Nguyen (UNC) LaTeX Workshop 10/13/2017 24 / 31
Tips and Tricks
We can make different colored text with \{red}{text}
If you are including a float in your homework for a part of a question
(say problem 10c, for example), LaTeX may not place the float where
you like it with respect to the problem numbering. If you add text
before your table or figure environment, this should make it more
aesthetically pleasing.
Rmarkdown package in R allows you to write up your LaTeX file in R
so you can more directly import tables and figures as you edit them.
You can also create presentations in LaTeX (called beamer). A
template is available on the BSA website
Cheatsheet and symbols .pdfs are useful resources.
Nguyen (UNC) LaTeX Workshop 10/13/2017 24 / 31
Tips and Tricks
We can make different colored text with \{red}{text}
If you are including a float in your homework for a part of a question
(say problem 10c, for example), LaTeX may not place the float where
you like it with respect to the problem numbering. If you add text
before your table or figure environment, this should make it more
aesthetically pleasing.
Rmarkdown package in R allows you to write up your LaTeX file in R
so you can more directly import tables and figures as you edit them.
You can also create presentations in LaTeX (called beamer). A
template is available on the BSA website
Cheatsheet and symbols .pdfs are useful resources.
Nguyen (UNC) LaTeX Workshop 10/13/2017 24 / 31
Tips and Tricks
We can make different colored text with \{red}{text}
If you are including a float in your homework for a part of a question
(say problem 10c, for example), LaTeX may not place the float where
you like it with respect to the problem numbering. If you add text
before your table or figure environment, this should make it more
aesthetically pleasing.
Rmarkdown package in R allows you to write up your LaTeX file in R
so you can more directly import tables and figures as you edit them.
You can also create presentations in LaTeX (called beamer). A
template is available on the BSA website
Cheatsheet and symbols .pdfs are useful resources.
Nguyen (UNC) LaTeX Workshop 10/13/2017 24 / 31
Tips and Tricks: Efficiently creating tables
Excel2Latex add on allows you to create tables in Excel and produce
the LaTeX code needed.
This online table generator will do something similar without any
downloading.
xtable package in R will take dataframes in R and output the LaTeX
code.
Nguyen (UNC) LaTeX Workshop 10/13/2017 25 / 31
Tips and Tricks: Efficiently creating tables
Excel2Latex add on allows you to create tables in Excel and produce
the LaTeX code needed.
This online table generator will do something similar without any
downloading.
xtable package in R will take dataframes in R and output the LaTeX
code.
Nguyen (UNC) LaTeX Workshop 10/13/2017 25 / 31
Tips and Tricks: Efficiently creating tables
Excel2Latex add on allows you to create tables in Excel and produce
the LaTeX code needed.
This online table generator will do something similar without any
downloading.
xtable package in R will take dataframes in R and output the LaTeX
code.
Nguyen (UNC) LaTeX Workshop 10/13/2017 25 / 31
Article/Journal Writing
Cheatsheet has lots of formatting help
fontsize
margins
in text changes to fontsize
quotation environment
hspace and vspace
You can label equations the same way we label figures and tables
External files: .cls, .sty, .bib
Nguyen (UNC) LaTeX Workshop 10/13/2017 26 / 31
References....the worst
.bib files
Google scholar is your friend
biblatex package in your preamble (not .bib)
\usepackage[style=apa,backend=biber]{biblatex}
\addbibresource{<filname>.bib}
\begin{document}
.
.
.
\printbibliography
\end{document}
\cite{} in text for the bib-key
multibib package for multiple .bib files in different sections
Nguyen (UNC) LaTeX Workshop 10/13/2017 27 / 31
Presentations
Beamer document class
Sections, enumerate, itemize, almost everything is the same
To make a new frame:
\begin{frame}
\frametitle{Frametitle goes here}
.
.
.
\end{frame}
\begin{frame}[< + >]... to give the PowerPoint effect
\vfill is your friend, please use it
Different themes are available here, or use our template on the BSA
website!
Nguyen (UNC) LaTeX Workshop 10/13/2017 28 / 31
Presentations: Blocks
Sometimes you want a block of text.
\begin{block}{Theorem}
If something happens, then this other thing happens.
\end{block}
Theorem
If something happens, then this other thing happens.
Nguyen (UNC) LaTeX Workshop 10/13/2017 29 / 31
Conclusion
LaTeX has a lot of capabilities and flexibility in total customization
via coding language
The ceiling is the roof
Online documentation, StackExchange, Cheatsheet, LaTeX symbols
document, Friends
BSA website templates for MS paper, beamer, homework
Use the LaTeX package tikz to draw figures in LaTeX. Examples
here.
Nguyen (UNC) LaTeX Workshop 10/13/2017 30 / 31
Conclusion
Questions?
Nguyen (UNC) LaTeX Workshop 10/13/2017 31 / 31