1 The why

Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.

(Yihui Xie Explaining Markdown, Pandoc, and LaTeX formatting power on SO )[https://stackoverflow.com/a/17648350/9115822]


2 Get the repo

git clone https://github.com/trendlock/picklerick.git

3 Open a .Rnw. file


4 Basics

4.1 Function Syntax

  • \ leads a function
  • {} brackets are used instead of ()

A simple form of a sweave document is:

\documentclass{article}

\begin{document}

Text

\end{document}

5 Spacing / Page Breaks

In general sweave will behave smartly with images, tables and plots. But there are commands to manually adjust the behaviour.

  • {\clearpage} - plush all material and start a new page
  • {\clearpage} - allow to break the line here
  • {\newline} - request a new line
  • {\newpage} - request a new page
  • {\nolinebreak} - no line break should happen here
  • {\nopagebreak} - no page break should happen here
  • {\pagebreak} - encourage page break

Ie. the following will give two pages with the word Text on them

\documentclass{article}

\begin{document}

Text
\newpage
Text

\end{document}

More resources


6 Typeface

6.1 Prior to \begin{document}

A font family is selected with :

\usepackage{<fontpkg>}

6.2 In Body

Then a certain block of text is assigned a font with the wrapper:

{\fontfamily{<fontcode>}\selectfont <text> }

6.3 Example

\documentclass{article}

\usepackage{tgadventor}

\begin{document}

{\fontfamily{qag}\selectfont

This document is a sample document to test font
families and font typefaces.

}
\end{document}

Some examples to choose from:

tibble::tribble(
                           ~Font, ~"fontpkg", ~"fontcode",            ~Example,
         "Computer Modern Roman",                 NA,       "cmr",  "Typeface-cmr.png",
            "Latin Modern Roman",          "lmodern",       "lmr",  "Typeface-lmr.png",
          "Latin Modern Dunhill",          "lmodern",      "lmdh", "Typeface-lmdh.png",
               "TEX Gyre Termes",         "tgtermes",       "qtm",  "Typeface-qtm.png",
              "TEX Gyre Pagella",        "tgpagella",       "qpl",  "Typeface-qpl.png",
                "TEX Gyre Bonum",          "tgbonum",       "qbk",  "Typeface-qbk.png",
               "TEX Gyre Schola",         "tgschola",       "qcs",  "Typeface-qsc.png",
                         "Times",            "times",       "ptm",  "Typeface-ptm.png",
              "Utopia / Fourier", "utopia / fourier",       "put",  "Typeface-put.png",
                      "Palatino",         "palatino",       "ppl",  "Typeface-ppl.png",
                       "Bookman",          "bookman",       "pbk",  "Typeface-pbk.png",
                       "Charter",          "charter",       "bch",  "Typeface-bch.png",
    "Computer Modern Sans Serif",                 NA,      "cmss", "Typeface-cmss.png",
       "Latin Modern Sans Serif",          "lmodern",      "lmss", "Typeface-lmss.png",
             "TEX Gyre Adventor",       "tgadventor",       "qag",  "Typeface-qag.png",
                "TEX Gyre Heros",          "tgheros",       "qhv",  "Typeface-qhv.png",
                     "Helvetica",           "helvet",       "phv",  "Typeface-phv.png",
    "Computer Modern Typewriter",                 NA,      "cmtt", "Typeface-cmtt.png",
  "Latin Modern Sans Typewriter",          "lmodern",      "lmtt", "Typeface-lmtt.png",
               "TEX Gyre Cursor",         "tgcursor",       "qcr",  "Typeface-qcr.png",
                       "Courier",          "courier",       "pcr",  "Typeface-pcr.png"
  ) %>% select(-Example) %>%
  kableExtra::kable()
Font fontpkg fontcode
Computer Modern Roman NA cmr
Latin Modern Roman lmodern lmr
Latin Modern Dunhill lmodern lmdh
TEX Gyre Termes tgtermes qtm
TEX Gyre Pagella tgpagella qpl
TEX Gyre Bonum tgbonum qbk
TEX Gyre Schola tgschola qcs
Times times ptm
Utopia / Fourier utopia / fourier put
Palatino palatino ppl
Bookman bookman pbk
Charter charter bch
Computer Modern Sans Serif NA cmss
Latin Modern Sans Serif lmodern lmss
TEX Gyre Adventor tgadventor qag
TEX Gyre Heros tgheros qhv
Helvetica helvet phv
Computer Modern Typewriter NA cmtt
Latin Modern Sans Typewriter lmodern lmtt
TEX Gyre Cursor tgcursor qcr
Courier courier pcr

Source


7 Table of Contents

Inserted after \begin{document}:

\tableofcontents

This will build a table of contents for that numbers all \section{} and \subsection{} elements.


8 Margin Control

Adjust:

\usepackage[margin=0.8in]{geometry}

9 Footnotes

Footnotes can be inserted with the following function, and are self numbering:

\footnote{A footnote}

10 Bibliographies

Now comes the fun… There are a few components to using a bibliography.

10.1 Prior to \begin{document}

Indicating a bib package to use. There are some arguments used here to specify the style of the citations and the sorting of the reference list. This particular combination was used for a report we did.

\usepackage[backend=bibtex, sorting=none, citestyle=authoryear]{biblatex}

Alternatively a author - year style would be another typical requirement: citestyle=authoryear.

Documentation on sorting: biblatex 3.1.2

Secondly you must call a function to point to your bibliography file (.bib) which must be in the same directory. In this example my file is called refs.bib.

\bibliography{refs}

10.2 In Body

Insert the function at end of your document but before the \end{}.

\printbibliography

10.3 References

In the .bib file a reference might look like the following (the which can often be near the paper on google scholar if it is a academic paper, otherwise you will probably have to prepare them yourself):

@article{garland2005,
  author={Bill Garland},
  title = {Elementary Physics of Reactor Control Module - Fission Product Poisoning},
  volume = {3},
  url = {http://www.nuceng.ca/br_space/2017-09_4d03_6d03/misc_files/xenon_poisoning.pdf},
  publisher = {Nuclear Engineering (Originally from McMaster University)},
  year = {2005}
}

11 Citations

11.1 Author Year

The first element of the reference (which is completely arbitrary) in a record in the .bib file can be called in a citation function like so:

A sentence with info from \cite{garland2005}

With variations to indicate page number of change the format of the citation:

Sentence goes here \parencite{garland2005}.

Or another sentence goes here \parencite[5]{garland2005}.

It could have been from \citeauthor{garland2005} (\citeyear{garland2005})

11.2 Numeric

Try differnt options for numeric citations.

Tip: delete all the temp files in dir to clear bib settings.

\usepackage[backend=bibtex, sorting=none, citestyle=numeric]{biblatex}

12 Page Numbers

Are included by default. To nullify the inclusion of page numbers use:

\pagenumbering{gobble}

13 R Chunks in Sweave

13.1 Syntax

Instead of the markdown usage of backticks and curly brackets, sweave uses the following syntax to define a chunk:

<<chunkname>>=

print(mtcars)

@

13.2 Setup chunk

<<setup, include=FALSE, cache=FALSE, echo=FALSE>>=
opts_chunk$set(echo=F)
library(<pkgs>)
@

13.3 Plotting

<<cars, warning=FALSE, message=F>>=
ggplot(mtcars, aes(disp, mpg, col = as.factor(gear))) +
  geom_point()
@

14 Using Figures

14.1 R Output

\begin{figure}
<<cars, warning=FALSE, message=F>>=
ggplot(mtcars, aes(disp, mpg, col = as.factor(gear))) +
  geom_point()
@
\caption{The relationship between cylinder displacement and miles per gallon for each car.}
\label{fig:Cars}
\end{figure}

14.2 Image from file

\begin{figure}
\includegraphics[width=\textwidth]{imgs/morty.jpg}
\caption{A caption about Morty}
\label{fig:morty}
\end{figure}

14.3 Advanced: Two figures side by side with a single caption

\usepackage{graphicx,caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}

\begin{subfigure}{0.5\textwidth}
\includegraphics[width=0.9\linewidth]{imgs/rick.png}
\caption{}
\end{subfigure}
\begin{subfigure}{0.5\textwidth}
\includegraphics[width=0.9\linewidth]{imgs/morty.jpg}
\caption{}
\end{subfigure}

\caption{(a) Rick and (b) Morty}
\label{fig:rickmorty}

\end{figure}

15 Referring to figures

Here is a sentence with reference to Figure \ref{fig:rickmorty}.

16 Positioning of figures

  • [h] Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot)
  • [t] Position at the top of the page.
  • [b] Position at the bottom of the page.
  • [p] Put on a special page for floats only.
  • [H] Places the float at precisely the location in the LaTeX code. Requires the float package (\usepackage{float}).

[Source][https://www.sharelatex.com/learn/Positioning_of_Figures]


17 Inline code

\Sexpr{Sys.Date()}

(Stands for S expression. S being the precursor to R)

18 Cheat Sheet

Link