pandoc.exe: Error producing PDF - latex

I have tried running the following simple code;
```{r sample}
library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:4]
# LaTeX Table
knitr::kable(dt,format = 'latex')%>%
column_spec(column = 1,width = '10cm',bold = T)
```
This runs fines with the Knit button. but get the following error when using rmarkdown::render();
! Undefined control sequence.
l.173 \centering\rowcolors
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS tst.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output tst.pdf --template "C:\PROGRA~1\R\R-34~1.1\library\RMARKD~1\rmd\latex\DEFAUL~3.TEX" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
Also note that without the format = 'latex' option it works fine with rmarkdown::render()
I tried the suggestions for similar errors but non of them worked (update Rmarkdown, install MikTex and the pdflatex.exe to path variable). Can someone please help me with this.Thanks

Calling the array package in the YAML worked for me.
header-includes:
- \usepackage{array}

Related

R Markdown: PDF Document compile error (pdf backend)

I decided to write my thesis in rmarkdown and have issues in knitting the document to pdf. It is a very large file. The document works with html_output but has issues in compiling using pdf_output. As I save the latex file using keep_tex I can then run it and it safely outputs a pdf_document (with some formatting issues).
date: "8/12/2019"
output:
pdf_document:
keep_tex : true
latex_engine : lualatex
includes :
before_body : Title.sty
fig_width : 6
fig_height : 4
fig_caption : TRUE
citation_package: biblatex
bibliography: Bibliographytxt.bibtex
output file: Test.knit.md
tlmgr search --file --global "/biblatex-dm.cfg"
tlmgr search --file --global "/Test.bbl"
! error: (file "Test_files/figure-latex/""Decision_Trees-1".png) (pdf backend):
Error: Failed to compile Test.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See Test.log for more info.
In addition: Warning messages:
1: In readLines(log) : incomplete final line found on 'Test.log'
2: In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains biblatex-dm.cfg
3: In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains Test.bbl
Execution halted
Does anyone know why R is reading the file like:
! error: (file "Test_files/figure-latex/""Decision_Trees-1".png) (pdf backend):
where the file Decision_Trees-1 is being read somehow in double quotation marks!?
Should it not read "Test_files/figure-latex/Decision_Trees-1.png".
In addition, the knitr::include_graphics("Mypdf.pdf") does not seem to work either when I try to load a file, either in pdf or png format.
Please help.

\newcommand with arguments in Kritr not working

I am quite new with Knitr, and I am trying to write my first document "importing" some "\newcommand" from my latex files.
All my "\newcommand" are processed perfectly with the exception of those with "arguments".
For instance I have added the packages I normally use in latex:
header-includes:
-\usepackage{a4wide,amsmath,amsthm,epsfig,graphicx}
- \usepackage{amsmath,amsthm,amssymb}
- \usepackage{amsfonts}
- \usepackage{graphics}
- \usepackage{subfigure}
- \usepackage{dsfont}
- \usepackage{bbm}
- \usepackage{bm}
- \usepackage{xcolor}
- \usepackage{dcolumn}
- \usepackage{pstricks,fancyhdr,fancybox}
- \usepackage{dcolumn}
- \usepackage{bm}
But the following does not work
\newcommand{\PR}[1]{\bm{P}\left\{{#1}\right\}}
The error is
output file: newfile.knit.md
"../Documents/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS newfile.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output newfile.pdf --template "C:\Users\P14814\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"
! Missing $ inserted.
<inserted text>
$
l.180 \newcommand{\PR}{[}1{]}\{\bm{P}
pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning message:
running command '"../Documents/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS newfile.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output newfile.pdf --template "C:\Users\P14814\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' had status 43
Execution halted
Your help is very much appreciated!!
I strongly recommend you to put these commands in an external file, say, preamble.tex, and include it via the includes option.
output:
pdf_document:
includes:
in_header: preamble.tex
See Section 3.3.7.3 of the R Markdown book: https://bookdown.org/yihui/rmarkdown/pdf-document.html
Using header-includes is not entirely safe. Pandoc may mangle certain characters when generating the LaTeX output. If your Pandoc version is higher than 2.0 (check rmarkdown::pandoc_version()), you may use a raw LaTeX block to protect the LaTeX content, e.g.,
header-includes: |
```{=latex}
\usepackage{a4wide,amsmath,amsthm,epsfig,graphicx}
\usepackage{amsmath,amsthm,amssymb}
......
```

Undefined control sequence error while converting from markdown to PDF with pandoc and xelatex

I'm making some documentation for our project, but I can't enable xelatex in pandoc to setup custom fonts. If I set --latex-engine xelatex pandoc will show that error:
pandoc: Error producing PDF from TeX source.
! Undefined control sequence.
l.17 \sys_if_engine_luatex:T
Here is a command that I run:
pandoc -s --latex-engine xelatex -f markdown build.md -o docs.pdf
I use Ubuntu 14.04 LTS, pandoc 1.12.2.1 and XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013/Debian).
Oops... after removing texlive 2013 with apt-get and installing manualy texlive 2015 pandoc will use old texlive (!!!). After that steps and setting PATH to /usr/local/texlive/2015/bin/x86_64-linux/ pandoc will finaly compile my file with xelatex.

Output of fa2latex command from psych causing LaTeX error in pandoc

I am writing a report using knitr in RStudio. Also using the psych package for factor analysis:
```{r, results='asis'}
library(psych)
data(Thurstone)
fa.Thurstone<-fa(Thurstone)
fa2latex(fa.Thurstone)
```
When I run this code chunk it causes an error:
! LaTeX Error: Unknown float option `d'.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.124 \begin{scriptsize} \begin{table}[htdp]
pandoc: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
When I save the TeX code it runs fine in LaTeX, but not in knitr.
Any suggestions on how to resolve this?

Markdown to PDF using Pandoc since Xetex Deprecation

On my MacBook (OSX Mountain Lion), I used to use this Pandoc command to convert Markdown to PDF:
$ markdown2pdf -N -o pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
But markdown2pdf no longer works, and --xetex option in markdown2pdf -N -o ../../Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate-headers-garamond_date.tex is deprecated.
If I do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --xetex --toc --template=mytemplate.tex myfile.md
I get this:
pandoc: unrecognized option `--xetex'
But if I take out --xetex and do this:
$ pandoc -N -o Desktop/pandoc_output.pdf --toc --template=mytemplate.tex myfile.md
then I get this:
pandoc: Error producing PDF from TeX source.
! Package hyperref Error: Wrong driver option `xetex',
(hyperref) because XeTeX is not detected.
See the hyperref package documentation for explanation.
Type H <return> for immediate help.
...
l.3925 \ProcessKeyvalOptions{Hyp}
What's the solution?
Try --latex-engine=xelatex instead of --xetex
The prior answers to this question were helpful to me, as I had installed pandoc a couple years ago, but never Tex Live. Consequently I had no idea if I had installed it correctly, so putting in the entire path helped me to see that it was working, as follows:
pandoc --latex-engine=/usr/local/texlive/2012basic/bin/universal-darwin/xelatex
This is the default install location for the BasicTex setup which you download from the Pandoc install page.
I had also forgotten about using pandoc -D Latex >my-latex-template.tex to generate a template. After giving a .tex template instead of my .html one (which caused a 'you don't have BEGIN {' error) , I got .PDF: In other words, the default template worked.
Also, I had inaccurately entered -t pdf (not shown above) to set pdf as an output format, but this was not correct. The output format is Latex, which is then translated to PDF. It is not necessary to specify an output format with the dash -t option.
I hope this record of my minor stumbles saves someone some time.
See the pandoc User's Guide (or man page) for the --latex-engine option.

Resources