David M. Kaplan
2020-03-19
First, check out the basic Rmarkdown template here or the knitr output in PDF format.
Attractive, but missing a number of things to be a true publication.
Parts that can be done in basic Rmarkdown:
Parts that require more advanced formatting:
A few useful websites:
output:
pdf_document:
number_sections: yes
Figure captions can be added with the fig.cap chunk optional argument
```{r myfig, fig.cap="This is the caption"}
plot(1:10)
```
tab.cap for R chunks (there is one for SQL chunks).Instead need to explicitly tell the chunk to print the table with a caption
kable, xtable, etc.I will just present kable
```{r}
df = data.frame(id=1:5,res=letters[1:5])
knitr::kable(df,caption="Table caption")
```
booktabs option seems to produce better tables in Latex
$E=mc^2$ produces: \( E=mc^2 \)$ at beginning and end of equation codes$ at beginning and end of display equations$$
E=mc^2
$$
\[ E=mc^2 \]
Equations can get a lot more complex:
$$
\frac{d\tilde{p}}{dx} \bigg|_{x_{\nu,\text{infl}}} = -\frac{\nu}{\alpha} \frac{1}{\nu+1} \left( \frac{\nu}{\nu+1} \right)^{\nu} = -\frac{1}{\alpha} \left( \frac{\nu}{\nu+1} \right)^\nu
$$
Produces:
\[ \frac{d\tilde{p}}{dx} \bigg|_{x_{\nu,\text{infl}}} = -\frac{\nu}{\alpha} \frac{1}{\nu+1} \left( \frac{\nu}{\nu+1} \right)^{\nu} = -\frac{1}{\alpha} \left( \frac{\nu}{\nu+1} \right)^\nu \]
endfloat Latex package (tables and figures are called floats as they float on page)header-includes:
- \usepackage{endfloat}
header-includes:
- \usepackage[nomarkers,tablesfirst]{endfloat}
endfloat for more optionsbibliography: BIBLIOGRAPHY.bib
csl: STYLE.csl
BIBLIOGRAPHY.bib and STYLE.csl with the path to a bibtex file containing your references and a CSL style file determining how to format references.bib web address for Zotero repository and subcollections. Used in Rmarkdown documents to automatically update .bib file using download.file commandctrl+shift+c copy of references for citationThis is an important result deserving citation [@CITEKEY; @OTHERCITEKEY].
@CITEKEY showed important things.
CITEKEY and OTHERCITEKEY are citation keys found at the top of each entry in the .bib file.[] around the citation changes the form of the citation.The bibliography with cited references will automatically be placed at the end of the output document (but before any floats placed at the end by endfloat). Therefore, one generally ends the R document with a section header for the references:
# References
.Rmd file, but rather require that you submit a Latex file. knitr generates a Latex file as an intermediate step to building the final PDF output. .tex file with an extra output format option keep_tex:output:
pdf_document:
number_sections: yes
keep_tex: yes
One can hide all code in a document by setting echo=FALSE in the setup chunk at the start of the document:
knitr::opts_chunk$set(echo = FALSE)
You can check out a document employing the approaches mentioned so far here, along with its PDF output and the associated .bib file.
# My section title
Section \ref{my-section-title}
# My section title {#sect1}
Section \ref{sect1}
Achieved by inserting Latex \label commands in the captions:
```{r fig.cap="\\label{fig:a_fig}Figure caption"}
plot(1:10)
```
```{r}
df = data.frame(id=1:5,res=letters[1:5])
knitr::kable(df,caption="\\label{tab:a_tab}Table caption")
```
Fig. \ref{fig:a_fig}, Table \ref{tab:a_tab}
Note the double slash (\\) inside the quotes because a single slash is treated by R as the start of an escape sequence.
$$...$$ does not permit Latex \label\begin{equation}...\label{eq:a_eq}\end{equation}\begin{equation}
E=mc^2
\label{eq:a_eq}
\end{equation}
Eq. \ref{eq:a_eq}
fig:, tab: and eq: are optionalYou can check out a document employing the approaches mentioned so far here, along with its PDF output.
output:
bookdown::pdf_document2:
df_print: kable
keep_tex: true
number_sections: yes
toc: no
bookdown::word_document2 and bookdown::html_document2 formats also existFigures and tables automatically get a label based on chunk name
```{r fig1,fig.cap="Figure caption"}
plot(1:10)
```
```{r tab1}
df = data.frame(id=1:5,res=letters[1:5])
knitr::kable(df,caption="Table caption")
```
Fig. \@ref(fig:fig1), Table \@ref(tab:tab1)
Bookdown doesn't like special characters (e.g., '_') in chunk labels
Prefixes tab: and fig: obligatory
\begin{equation}...(\#eq:eq1)\end{equation}\begin{equation}
E=mc^2
(\#eq:eq1)
\end{equation}
Eq. \@ref(eq:eq1)
eq: prefix obligatory and no funky charactersauthor: field of the YAML header
abstract: fieldauthor: |
| John Doe $^1$^[Corresponding author: john.doe@nowhere.org], Jane Smith $^{2,3}$
|
| $^1$ Address number 1
| $^2$ Address number 2
| $^3$ Address number 3
abstract: |
| This is a small abstract.
|
| It has two paragraphs.
header-includes field in the YAML header:header-includes:
- \usepackage{endfloat} # From previous
- \usepackage{setspace}\doublespacing
- \usepackage{lineno}
- \linenumbers
You can check out a document employing the approaches mentioned so far here, along with its PDF output.
bookdown::pdf_book output format permits you to specify a base_formatrticles::elsevier_article, to create an Elsevier article with bookdown advantages for cross-referencingoutput:
bookdown::pdf_book:
base_format: rticles::oup_article
keep_tex: yes
df_print: kable
oup_article template as I know it well, but others can easily be adaptedI created the knitrdata package to solve this problem
```{r}
library(knitrdata)
```
```{data output.var="d",load.function=read.csv}
a,b
1,2
3,4
```
```{r}
d
```
Binary and encrypted data possible (see package vignette)
.Rmd document on github, google drive or similar and authors work directly in Rmarkdown.
bookdown::word_document2 that authors can edit.
bibliography and csl entries in YAML header may facilitate copying modified text back into .Rmd documentYou can check out a document employing all approaches mentioned here, along with its PDF output.