How to build a latex kable through bookdown::render_book? - r

Here is a file kable.Rmd that knits properly in RStudio:
---
title: "latex kable"
output: pdf_document
---
```{r table1, echo=FALSE}
library(magrittr)
library(kableExtra)
kable(data.frame(a=c(1,2,3), b=c(4,5,6))) %>%
kable_styling(latex_options = c("striped", "hold_position", "repeat_header"),
full_width = F)
```
The output shows a table:
Here is a YAML file to configure bookdown called kable_bookdown.yml:
# See https://bookdown.org/yihui/bookdown/usage.html
rmd_files: ["kable.Rmd"]
delete_merged_file: true
Here is a call to render:
bookdown::render_book(input='kable.Rmd', config_file='kable_bookdown.yml')
Here is the error I get from that render:
/Applications/RStudio.app/Contents/MacOS/pandoc/pandoc +RTS -K512m -RTS _main.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output _main.tex --self-contained --highlight-style tango --pdf-engine pdflatex --variable graphics --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rmarkdown/rmd/lua/latex-div.lua --variable 'geometry:margin=1in'
! Undefined control sequence.
<recently read> \rowcolor
The rowcolor likely comes from the "striped" latex option.
Why does it work in RStudio, but not through the render call?
RStudio seems to be using the same latex (pdflatex).
I'm using RStudio Version 1.2.5042, R 3.6.3, bookdown 0.18, kableExtra 1.1.0, knitr 1.28, tinytex 0.20.
EDIT: I was not able to get header_includes to work. Adding this to kable.Rmd worked for me:
header-includes:
- \usepackage{colortbl}
- \usepackage{xcolor}
EDIT 2: This also worked for me (from here):
output:
pdf_document:
# list latex packages:
extra_dependencies: ["colortbl", "xcolor"]

This happens because kableExtra adds extra packages to the .tex header, and those are getting lost when bookdown does the processing. I believe \rowcolor comes from the colortbl package. So you need to tell bookdown to include that package. If I put this line into the YAML in kable.Rmd, it works:
header_includes: "colortbl"
but in a more complicated example, you may need more includes. Here are the ones that kableExtra causes to be inserted:
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\usepackage{xcolor}

Related

bookdown is taking pdflatex even though I explicitly stated xelatex

I'm trying to create a book with bookdown as pdf. It works well creating an EPUB file.
My YAML is this:
---
title: "Title Here"
subtitle: "Subtitle here"
author: "me"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib]
biblio-style: apalike
link-citations: yes
description: "description goes here."
cover-image: "cover.png"
lang: "es"
always_allow_html: true
output:
pdf_document:
latex_engine: xelatex
pdf_document2:
latex_engine: lualatex
---
At some point in chapter 4 I have a Japanese letter, U+305B.
I've tried creating the pdf book with:
bookdown::render_book("index.Rmd", output_format = bookdown::pdf_document2())
and
bookdown::render_book("index.Rmd", output_format = bookdown::pdf_book())
Both of them complain that:
! Package inputenc Error: Unicode character せ (U+305B)
(inputenc) not set up for use with LaTeX.
and recommend me to check this part of the rmarkdown cookbook. From there is where I took the 4 last lines of the YAML preamble.
I couldn't help but noticing that the call to pandoc is taking pdflatex as the pdf-engine (see third-to-last line):
/usr/lib/rstudio/bin/pandoc/pandoc +RTS -K512m -RTS myFileName.knit.md --to latex
--from markdown+autolink_bare_uris+tex_math_single_backslash
--output herramientasBasicasMejoramiento.tex
--lua-filter /usr/local/lib/R/site-library/bookdown/rmarkdown/lua/custom-environment.lua
--lua-filter /usr/local/lib/R/site-library/rmarkdown/rmarkdown/lua/pagebreak.lua
--lua-filter /usr/local/lib/R/site-library/rmarkdown/rmarkdown/lua/latex-div.lua
--metadata-file /tmp/RtmpAbwGga/file565f3d087e3c --self-contained
--table-of-contents --toc-depth 2 --number-sections
--highlight-style tango --pdf-engine pdflatex --variable graphics
--wrap preserve --variable tables=yes
--standalone -Mhas-frontmatter=false --citeproc
I'm working in Ubuntu and have installed both texlive and tinytex.
whereis xelatex
xelatex: /home/gp/bin/xelatex /usr/local/texlive/2019/bin/x86_64-linux/xelatex
There is an unanswered question with a problem that looks similar.
What am I doing wrong, and what can I do to make it work?
You need to specify the output format in a way that will see your latex_engine argument, e.g.
bookdown::render_book("index.Rmd", output_format = "pdf_document2")
(naming one of the output formats from your YAML), or
bookdown::render_book("index.Rmd",
output_format = bookdown::pdf_document2(latex_engine = "xelatex"))
(specifying it independently).

LearnR Rmd knit to pdf failed to compile after update of MikTex

After I updated MikTex to the latest version, my Rmd files don't compile anymore. I installed tinytex, reinstalled miktex, R and RStudio several times. I use some newcommands like \C, \G etc. in a tex document which I include in my Rmd file. I use windows 10, the latest versions of R, RStudio and Miktex.
I asked a colleague (who had no compiling issues) to update his MikTex as well. After updating his MikTex, he had the same issue.
Here is the Rmd code:
---
documentclass: scrartcl
fontsize: 11pt
output:
pdf_document:
includes:
in_header: commands.tex
---
This is an example:
\begin{itemize}
\item hello
\item world
\end{itemize}
And here is the tex code:
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{icomma}
\usepackage[right,official]{eurosym}
\usepackage{hyperref}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage[shortlabels]{enumitem}
\usepackage{setspace}
\singlespacing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Seitenformat und Layoutparameter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\hbadness=1000
\tolerance=10000
\voffset-2.80cm
\hoffset-2.60cm
\topmargin1.50cm
\headheight0.65cm
\headsep1.0cm
\topskip0cm
\textheight24.00cm
\footskip1.5cm
\footnotesep11pt
\evensidemargin2.50cm
\oddsidemargin2.50cm
\textwidth16cm
%\parindent0cm
%\parskip1.5ex plus0.5ex minus 0.5ex
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\renewcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
And I get:
processing file: example1.Rmd
|......................................................................| 100%
ordinary text without R code
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS example1.utf8.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output example1.tex --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\saski\Documents\R\win-library\4.0\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --include-in-header commands.tex --variable graphics
output file: example1.knit.md
! LaTeX Error: Command \C undefined.
Fehler: LaTeX failed to compile example1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See example1.log for more info.
Ausführung angehalten
The best solution would be to avoid one letter macro names
your code no longer works because \C is now only defined by hyperref under certain circumstances, see https://tex.stackexchange.com/questions/582625/command-c-already-defined-and-the-hyperref-package/582630#582630 for more information
If you use \newcommand instead of \renewcommand you can see that the error vanishes
% simple letters
\newcommand{\A}{{\mathbb A}}
\newcommand{\B}{{\mathbb B}}
\newcommand{\C}{{\mathbb C}}
\newcommand{\D}{{\mathbb D}}
\newcommand{\E}{{\mathbb E}}
(but you should still not do that, use some other macro names instead if the one letter names)
Conclusion: don't use one letter macro names

rstudio knitr not running pdfcrop

I'm trying to knit the mwe below to pdf using rstudio (1.2.5019)
---
title: "Test"
author: "Me"
output:
pdf_document:
fig_caption: yes
keep_tex: yes
fig_crop: yes
---
This should be cropped
```{r}
library(knitr)
```
```{r named-chunk}
plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
main="Not cropped", xlab="Looks good", ylab="Nok")
```
The output pdf looks fine except the plot has not been cropped with pdfcrop.
Here's the log output.
processing file: cropping.Rmd |..................
| 25% ordinary text without R code
|...................................
| 50% label: unnamed-chunk-1
|....................................................
| 75% ordinary text without R code
|......................................................................|
100% label: named-chunk
output file: cropping.knit.md
"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS
cropping.utf8.md --to latex --from
markdown+autolink_bare_uris+tex_math_single_backslash --output
cropping.tex --self-contained --highlight-style tango --pdf-engine
pdflatex --variable graphics --lua-filter
"C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/pagebreak.lua"
--lua-filter "C:/Users/marti/Documents/R/win-library/3.6/rmarkdown/rmd/lua/latex-div.lua"
--variable "geometry:margin=1in"
Output created: cropping.pdf
pdfcrop works just fine from my command line but isn't called by rstudio. It is also located by rstudio.
Sys.which('pdfcrop')
pdfcrop
"C:\texlive\2019\bin\win32\pdfcrop.exe"
What am I missing?
This actually solved it.
``` {r crop-hook,cache=FALSE}
knit_hooks$set(crop=hook_pdfcrop)
```
```{r named-chunk, crop=TRUE}
plot(x <- 0:100, dbinom(x, size=100, prob=0.6), type='l',
main="Not cropped", xlab="Looks good", ylab="Nok")
```
As far as I understood from the documentation this shouldn't be necessary?
Install pdfcrop via livetex or miktex --- the one you are using to write latex documents.

Latex shell-escape options in YAML header don't use

I'm writing a report on Rmarkdovn but when you try to compile - pandoc_args options ignored when compile pdf.
I find this question:
Is it possible to include svg image in pdf document rendered by rmarkdown?
But it does not work because --latex-engine-opt replaced by --pdf-engine-opt. I replaced, but also did not work.
Compilation command:
Rscript -e "rmarkdown::render('test.rmd')"
test.rmd
---
title: Test
papersize: a4paper
output:
pdf_document:
latex_engine: pdflatex
pandoc_args: [
--pdf-engine-opt, -shell-escape,
--pdf-engine-opt, -interaction=nonstopmode]
header-includes:
- \usepackage{minted}
---
Output:
processing file: test.rmd
|................................ | 50%
ordinary text without R code
|.................................................................| 100%
label: unnamed-chunk-1 (with options)
List of 1
$ engine: chr "python"
output file: test.knit.md
/usr/bin/pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --template /home/renesat/R/x86_64-pc-linux-gnu-library/3.5/rmarkdown/rmd/latex/default-1.17.0.2.tex --highlight-style tango --pdf-engine pdflatex --variable graphics=yes --pdf-engine-opt --shell-escape --pdf-engine-opt -interaction=nonstopmode --variable 'geometry:margin=1in' --variable 'compact-title:yes'
! Package minted Error: You must invoke LaTeX with the -shell-escape flag.
Error: Failed to compile test.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See test.log for more info.
Execution halted
I can use knitr -> pandoc -> xelatex, but it would be more convenient to use YAML header.
R Markdown documents are compiled to PDF via tinytex::latexmk(). To enable the -shell-escape option for your LaTeX engine, you may set the global option tinytex.engine_args, e.g., add this code chunk to your document:
```{r, include=FALSE}
options(tinytex.engine_args = '-shell-escape')
```
See the help page ?tinytex::latexmk for more information.
The LaTeX engine is called by tinytex::latexmk() instead of Pandoc, so the Pandoc argument --pdf-engine-opt won't work.

R knitr PDF problems with \includegraphics

Using a new empty .rmd document, this code works:
![](RainbowDolphin.png)
\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}
But without the first line, it doesn't:
\begin{center}
\includegraphics[width=4in]{RainbowDolphin.png}
\end{center}
I get an error:
! Undefined control sequence.
l.71 \includegraphics
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" Sampling_03.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Sampling_03.pdf --template "C:\PROGRA~1\R\R-31~1.2\library\RMARKD~1\rmd\latex\default.tex" --highlight-style tango --latex-engine pdflatex --variable "geometry:margin=1in"' had status 43
Execution halted
Weird. Any help appreciated!
It's because the latex template doesn't load the graphicx package by default. You can do it manually by adding this to the yaml header:
---
title: "Untitled"
header-includes: \usepackage{graphicx}
output:
pdf_document:
keep_tex: true
---
You may let Pandoc know that you have graphics in this document by specifying the YAML metadata:
---
graphics: yes
---

Resources