2

I've installed MacTex and I'm trying to format tables in a PDF with the kable function using knitr/Rmarkdown.

When I output my data as kable(dt, format = 'latex') it works fine, but as soon as I try to add any kind of styling (e.g. booktab = T or kable_stlying()) I receive a variety of errors.

I'm playing whack-a-mole with them, constantly Googling and trying to use a different package, but there's no end in sight.

For example, here are a couple errors I've received:

1)

! LaTeX Error: Unknown float option `H'.
l.87 \begin{table}[H]

So I added to my .Rmd file:

header-includes:
   - \usepackage{float}

2) Then I received this error:

! Undefined control sequence.
l.89 \centering\rowcolors

So I included:

- \usepackage[table]{xcolor}

Another error appeared and now I'm on my fifth iteration of this. It's getting harder to just look up the error on Google, as they're getting more obscure and specific. Any advice on why such a basic use of kable would be causing so many issues with MacTex?

The errors always appear after this call in R:

/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS Payments.utf8.md 
--to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash 
--output pandoc2b922cfdb1c9.pdf 
--template /Library/Frameworks/R.framework/Versions/3.3/Resources/library/rmarkdown/rmd/latex/default-1.17.0.2.tex 
--highlight-style tango --latex-engine pdflatex 
--variable graphics=yes --variable 'geometry:margin=1in' 

I don't know how to interpret that, but it appears there's an issue with pdflatex?

  • 1
    This has very little to do with pdftex. And it's the responsibility of the R package to document what LaTeX packages are required. I suggest learning a bit about LaTeX first, outside the context of pandoc and R markdown. – Alan Munn Dec 2 '17 at 1:41
1

What happens if you type Hello \foo in a Rmarkdown (without any R chunk)? Yes, the same "Undefined control sequence" error that shows up with some options of the kable R function.

This mean that you (or R) have written a control sequence (=command, =macro) that really doesn't yet exist (is not defined) because \foo is not a TeX primitive command, nor a macro of the LaTeX kernel, nor defined in the document class, nor in a package, nor in the template preamble...

If you define it, for instance with \def\foo{Foo!} or \newcommand\foo{Foo!} before using \foo now LaTeX will know that it must change the control sequence to "Foo!" in the PDF, but otherwise ... What else should LaTeX do, except stop and warn you?

With an R chunk producing a LaTeX output the problem is the same. When you use, for instance, the booktabs=T option, R include commands like \toprule in the intermediate .tex file (*) but not the definition of \toprule. This is because definitions usually should be defined just once in the preamble, not each time the command will be used. So, it is your responsibility provide the definition, in this case, loading the package booktabs (with \usepackage{booktabs}) where \toprule is defined.

(*) In Rstudio, go to Output Options > Advanced > Keep tex source file used to produce PDF so you can inspect what what is typed exactly in the intermediate .tex file and see more clearly the problem.

Nearly 100% of "Undefined control sequences" produced by R packages can be solved including a CTAN package as above, but remember that any definition can be also provided (directly or via packages) in pandoc's LaTeX templates as default-1.17.0.2.tex, or in the defined document class (as article.cls). The above template loads some very usual packages like amsmath but cannot have every package that "maybe" you will need.

Providing the right environment is still your job. The good news is that you can construct your own pandoc template or even your own document class, including the packages that usually you want/need (booktabs, float, etc.) to avoid loading a lot of packages in the YAML header of every document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.