LaTeX and Markdown

Documents with Markup

2024-10-29

Contents

(in chronological order)

LaTeX

LaTeX: a document prep system

Think of it as the digital version of a printing press.

You will use tags to control the formatting of your document, and then compile it to PDF.

  • \emph{} for italic text
  • \textbf{} for bold text
  • {\HUGE }, {\footnotesize }

The LaTeX system lays out your document according to a set of rules for spacing, margins, figure captions, cross-references, …

Printing press

Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to humans what we want the computer to do.

— Donald E. Knuth, Literate Programming, 1984

useR! 2016 was celebrating the markdown systems as epic milestones for literate programming: https://www.r-project.org/conferences/useR-2016/keynotes.html

LaTeX Editors

While a .tex file is just a text file, it can help to use a dedicated editor that will compile your document for you.

Some options:

  • RStudio (my default)
  • Overleaf.com (real-time compilation, collaboration)
  • TeXWorks
  • LyX - What-you-see-is-what-you-get (WYSIWYG) LaTeX editor

Of these, Lyx and Rstudio support integrating your code with your document using knitr/Sweave.

LaTeX cheatsheet

Learn LaTeX in 30 minutes

Basic LaTeX


\documentclass{article}     % 1
\usepackage[utf8]{inputenc} % 2

\usepackage{natbib}         % 3 
\usepackage{graphicx}       % 4

\title{Demo}                % 5
\author{Susan VanderPlas}
\date{\today}

\begin{document}            % 6

\maketitle                  % 7

\section{Introduction}      % 8
\citet{adams1995hitchhiker}
\section{Conclusion}

\bibliographystyle{plain}   % 9
\bibliography{refs}
\end{document}

Basic LaTeX document

  1. type of document (article)

  2. allow UTF-8 characters

  3. use a bibliography

  4. allow pictures

  5. Document header information

  6. Actual document content

  7. Create document title/header

  8. Create introduction section (with a citation)

  9. Create the bibliography

Overleaf Gallery of possibilities

BibTeX

One of the most wonderful parts of LaTeX is BibTeX, which handles your citations for you.

  • You type your references in only once (in a markup style called bibtex)
  • Citations in your paper are or or
  • You set the reference type at the top of the document (numeric, alphabetical, etc.)
  • At the end of the document you add a command for where the bibliography goes

Bibtex does all of the hard work of cross-linking your references with the bibliography, formatting, etc.

Zotero logo

There are various options for (free!) citation managers - personally, I love Zotero and Mendeley because they also integrate with RStudio.

Other tools: Evernote, Papers

Recent blog with comparison of these and a few other reference managers.

All these programs help to keep track of papers and associated notes and will export BibTeX automatically.

They also come with browser addins that will easily save papers to your library as you surf.

Mendeley logo

Typesetting math

Inline math: $x + 2$

Centered math:
$$ x+2 $$
 
 
 

Systems of equations:
\begin {align} x &= a + b\\
y &= c + d \end{align}
 

Math characters:
\alpha, \beta, \gamma, \infty, \rightarrow, \int,\frac{a}{b}

\(x + 2\) is an inline equation.

We can also center equations within a paragraph, like this: \[x + 2\] The paragraph resumes after we’re done.

Systems of equations:
\(\begin{align} x &= a + b\\ y &= c + d\end{align}\)
 

Math characters:
\[\alpha,\beta,\gamma,\infty,\rightarrow,\int,\frac{a}{b}\]

Cheatsheet for math mode

Try it out!

Minimal LaTeX document:

\documentclass{article}
\begin{document}
Hello \LaTeX!
\end{document}
  1. Add an image

  2. Add the quadratic formula and the PDF of a normal distribution to the document

  3. In extremely large text, print LaTeX using the \LaTeX command

  4. In extremely small, italic text, print your name

But I don’t want a PDF

Pandoc

LaTeX only creates PDF files (and PostScript files, but nevermind those).

Enter pandoc, which was supposed to be a “universal translator” of markup formats.

Markdown document ->

  • html
  • epub
  • docx
  • LaTeX
  • other markdown flavors

With pandoc, the goal is to transfer the content; the exact formatting is less of a priority. Thus, only a small set of formatting options are supported.

You can use the same BibTeX files in markdown with Pandoc!

Try it out!

Minimal markdown document:

---
title: "Demo"
author: "Me"
date: "October 2020"
output: 
  word_document: default
  html_document: default
  pdf_document: default
---

# Introduction

# Conclusion

# References

markdown cheatsheet

  1. Add an image from the internet

  2. Add the quadratic formula and the probability density function of a normal distribution to the document
    Math mode works the same in markdown as in LaTeX (mostly)

  3. Add a citation to the Hitchhiker’s Guide using this references file

Adding in R or Python

 

R logo

 

Python logo

 

LaTeX + R?

Sweave was the original document compilation system for combining LaTeX and R
(created circa 2002)

  • Basic structure was great
  • Caching was terrible (so all code re-ran each time you changed formatting a bit)

knitr: created in 2012 by Yihui Xie to be a better version of Sweave

  • Same basic syntax
  • better caching & dependency management
  • All Sweave features + some
  • Support for Python, Perl, C++, shell scripts

LaTeX + R?

knitr reference manual

To add R code to your .tex file,

  1. Change the file extension from .tex to .Rnw (Rnw = R no weave, a holdover from Sweave)

  2. Add R code chunks

  3. Compile your document

R code chunks in .Rnw documents

<<chunk-name, chunk-options>>=

R code here

@

Markdown + R?

You’ve been using quarto for the entire semester, but it works essentially the same way as adding R to LaTeX:

Rmarkdown Workflow

Quarto is similar, but supports even more formats and allows you to use even more programming languages in code chunks.

Code Chunk Options

Common chunk options:

  • eval = (T/F): whether the code is evaluated by R

  • include = (T/F): should code or output from the code be included in the document?

  • echo = (T/F): should code be printed in the document, or just the output?

  • fig.width, fig.height =: size of the saved image, in inches

  • out.width =: width of the output image IN THE DOCUMENT

  • fig.cap =: Set a figure caption

  • cache = (T/F): Cache the chunk, so that it is only re-run if it changes?

All of these chunk options also work in rmarkdown

Try it out!

Turn your .tex file into an .Rnw file and add some more stuff!

  1. Add a histogram of 100 draws from a normal distribution
  1. Compute the mean of 15 draws from a binomial(10, .5) distribution and print it inline using \Sexpr{}
  1. Use knitr::kable() to print out the first 6 rows of the cars dataframe.

Table Upgrades

Table Packages

Other packages

  • (for HTML) papaja - use APA formatting throughout the whole html document

Special Document Types

Both LaTeX and quarto/Rmarkdown make use of other libraries to expand the types of documents available.

Document Type Library Compiler End Result
Slides Beamer LaTeX or Markdown + LaTeX PDF
  Reveal.js Markdown HTML
  IOSlides Markdown HTML
  Slidy Markdown HTML
  PowerPoint Markdown PowerPoint
  Xaringan Markdown + JS HTML/markdown hybrid
Poster Beamer LaTeX PDF
  Posterdown Markdown HTML
  Pagedown Markdown HTML

These documents use the same basic markdown or LaTeX syntax, but render differently.