0

I get an error when trying to create a pdf that has a booktabs table in it. If I delete the table code, the pdf is created without a problem.

RStudio: Latest version

R: latest version

tinytex: latest version

Below is my Rmd file:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
  html_document:
    df_print: paged
includes:
  in_header: preamble.tex
---

Something here

\begin{table}[h]
    \centering
    \caption{Descriptive Statistics}
    \label{tab:descriptiveStatistics}
    {
        \begin{tabular}{lr}
            \toprule
             & Weight  \\
            \cmidrule[0.4pt]{1-2}
            Valid & 13  \\
            Missing & 0  \\
            Mean & 170.769  \\
            Std. Deviation & 29.255  \\
            Minimum & 140.000  \\
            Maximum & 235.000  \\
            \bottomrule
        \end{tabular}
    }
\end{table}

Below is my preamble.tex file:

\usepackage{booktabs}
\usepackage{makecell}

The error that I get is as follows:

output file: test.knit.md

! Undefined control sequence.
l.81             \toprule

Error: LaTeX failed to compile test.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See test.log for more info.
In addition: Warning message:
In has_crop_tools() : 
Tool(s) not installed or not in PATH: pdfcrop, ghostcript
-> As a result, figure cropping will be disabled.
Execution halted
  • Another solution for tables in LaTeX would be: https://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf – bttomio Feb 13 '21 at 23:21
  • Thank you for the suggestion. However, I am writing a book on how to teach stats with [JASP](http://jasp-stats.org). JASP allows one to copy tables as LaTex (need booktabs package), which has the format shown above. Thus, it is less time consuming for me to use the format above. – Ovande Furtado Feb 14 '21 at 03:12

1 Answers1

1

Your preamble.tex isn't included because your yaml header is wrong. Should be:

---
title: "table"
author: "Author"
date: "2/13/2021"
output:
  pdf_document:
    latex_engine: xelatex
    includes:
      in_header: preamble.tex
  html_document:
    df_print: paged
---

Then it works.

I would also recommend using kableExtra. It basically produces LaTeX code and saves you a lot of time making tables in my experience. You can set keep_tex: yes (also under pdf_document:) to double-check what is being produced.

JBGruber
  • 11,727
  • 1
  • 23
  • 45
  • Thank you so much. I noticed that it works for pdf files but not for doc or html. Do I need to add anything in the yaml header for it to work? Thanks in advance. – Ovande Furtado Feb 15 '21 at 19:28
  • That's my biggest issue with R Markdown: Tables are not converted to all formats. `knitr::kable` and `kableExtra` work well for PDF and HTML. But if you want to have nice tables in doc and docx you need `flextable` or something similar. – JBGruber Feb 15 '21 at 20:48
  • Personally I wrote a function to get my tables right. I use `knitr:::pandoc_to() == "latex"` to check if something is knitted to PDF and `knitr:::pandoc_to() == "docx"` for word. – JBGruber Feb 15 '21 at 20:50