Skip to content
Elise Keppel edited this page Apr 7, 2020 · 43 revisions

FAQ

  1. How do I include a citation or figure reference or r object value in a figure or table caption?
    ```{r chunk1}
    x <- 7
    ```

    (ref:my-caption) A figure caption. See Appendix \@ref(app:my-appendix)). x has 
    the value `r x`. See @viridis2018.

    ```{r my-figure, fig.cap="(ref:my-caption)"}
    plot(1)
    ```

  1. How do I include extra packages for latex-rendered documents?

In the index.Rmd file, add the following to the YAML part (the section between the --- and ---) for whatever package you want to include. For this example it is the threeparttablex package:

header-includes:
  - \usepackage{threeparttablex}

Note that if you need to include more than one package you will have to have them on the same line like this:

header-includes:
  - \usepackage{threeparttablex} \usepackage{caption}

This will NOT work, only the first one is added:

header-includes:
  - \usepackage{threeparttablex}
  - \usepackage{caption}
  1. I have added appendices after the bibliography, but they are appearing in the document before the bibliography. How do I make the bibliography appear before the appendices?

Add the following line where you want the bibliography to be placed:

<div id="refs"></div>
  1. When I click on links in the PDF (e.g. figures or citations) they don't take me to the appropriate figure or reference. How do I fix that? Try running tinytex::tlmgr_install("hyperref").

Notes

  1. To see the variables declared in _bookdown.yml, you can do the following from any R chunk within the project:
config <- bookdown:::load_config()
  1. To access the YAML metadata located at the beginning of index.Rmd, you can look at the following list from any R chunk within the project:
rmarkdown::metadata

For example if creating a CSAS SR document, the following YAML metadata will be in index.Rmd:

output:
 csasdown::sr_pdf:
   french: true

In the setup R chunk, the following line of code would set up the language for R:

french <- rmarkdown::metadata$output$`csasdown::sr_pdf`$french
  1. If writing the document in French, you can change the decimal point to a comma for all numeric values in the project using the following in the setup chunk:
if(french){
  options(OutDec =  ",")
}
  1. Knitr chunk names can only have numbers, letters, and dashes. Underscores are not permitted and will cause major problems in rendering with bookdown. Here is an example of a bad chunk:
```{r bad_chunkname, results = "asis"}
    d <- data.frame(x = c(1,2,3,4),
                    y = c(5,6,7,8),
                    z = c(9,10,11,12))
    csasdown::csas_table(d,
                         format = "latex",
                         font_size = 8,
                         caption = "XX - This table is broken")
```

The error for this was:

! Misplaced \noalign.
\toprule ->\noalign 
                    {\ifnum 0=`}\fi \@aboverulesep =\abovetopsep \global \@b...
l.125 ...able is broken\}\textbackslash{} \toprule
                                                   x \& y \& z\textbackslash...

Error: Failed to compile resdoc.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See resdoc.log for more info.
Execution halted

And the resulting TeX file had broken code for the table with many \textbackslash{} commands peppered through it:

\textbackslash{}begin\{longtable\}\{rrr\} \textbackslash{}caption\{(\#tab:broken\_table)XX - This table is broken\}\textbackslash{} \toprule x \& y \& z\textbackslash{} \midrule 1 \& 5 \& 9\textbackslash{} 2 \& 6 \& 10\textbackslash{} 3 \& 7 \& 11\textbackslash{} 4 \& 8 \& 12\textbackslash{} \bottomrule \textbackslash{}end\{longtable\}
  1. Do not include spaces or ~ in any folders in the path to your document. LaTeX doesn't play nicely with them.

  2. In order for proper formatting of headers according to CSAS layout requirements, the headers must be typed in the appropriate case in .Rmd files. For example:

Heading 1-beginning of a section or chapter; all caps. Prints as e.g. 1. INTRODUCTION

# THIS IS A MAIN HEADER 

Heading 2-subsection; all caps. Prints as e.g. 1.1 FIRST SUBSECTION

## THIS IS A SUBSECTION

Heading 3; sentence case. Prints as e.g. 1.2.1 This is the next layer

### This is the next layer

Heading 4-sentence case. Prints as e.g. 1.2.4.1 Yet another

#### Yet another