Pandoc conversion of markdown to latex with default filename - r

I'm using the R package knitr to generate a markdown file test.md. This file is then processed by pandoc to produce a variety of output formats, such as html and pdf. Because I want to use bibtex when generating the pdf through latex, I believe I have to tell pandoc to stop at the intermediate latex output, and then run bibtex and pdflatex myself (twice). Here's where I found a slight annoyance in my workflow: the only way I found for pandoc to keep the intermediate tex file, and not go all the way to the pdf, was to specify a hard-coded filename through the -o option with a .tex extension. This is problematic for me because I'm using a config file to run pandoc('test.md', "latex", "config.pandoc") via knitr with options, which I would like to keep generic without hard-coded output filename:
format: latex
o: test.tex
s:
S:
biblio: refs.bib
biblatex:
template: 'template.tex'
default-image-extension: pdf
which in turn becomes the following command for pandoc,
pandoc -s -S --biblio=refs.bib --default-image-extension=pdf --biblatex --template='template.tex' -f markdown -t latex -o test.tex 'test.md'
If I skip the o: test.tex option, pandoc produces a pdf and doesn't keep the intermediate latex file. How can I keep the tex file, without specifying this hard-coded filename?

To solve this problem on my side, I added a new argument ext to the pandoc() function. It is available on Github now (knitr development version 1.3.6). You can override the default file extension, e.g.
library(knitr)
pandoc(..., ext = 'tex')

Related

Rmarkdown, pandoc/PDFlatex, and underscores in a temporary directory name - a problem

I have an R markdown document that has a chunk that generates a plot using ggplot2 and a custom function, progress.plot. The chunk looks like this
```{r charts, echo=FALSE, fig.cap="Your progress curve"}
progress.plot(student)
```
This document generates a PDF using a LaTeX engine. I think it uses PDFlatex via pandoc. This is causing a problem.
It seems like Rmarkdown uses a temporary folder to store the diagram I generate with ggplot2. The name of that folder has underscores. This means the path to the graphic file, a string that's used in the .tex source file, has an underscore. And the latex compiler doesn't like that.
! Missing $ inserted.
<inserted text>
$
l.142 ...de2020-05-06_files/figure-latex/charts-1}
\caption[Your progress cu...
I am getting a "Missing $ inserted" error before the latex compiler craps out. No PDF is generated and the directory with the figure is deleted. (I have experience with LaTeX, so I'm familiar with this type of troubleshooting.)
Is there a way to take the underscore out of the temporary directory's name? Or is there any other workaround that will allow me to generate figures to include in my R markdown document?
Thanks for whatever advice people can share.

Is there some convenient way to convert rmarkdown to pandoc markdown?

I use rstudio to write r-markdown, but sometimes it is not compatible with markdown support by pandoc(math for example. If there is a way allow me to convert r-markdown to pandoc markdown, then it will be convenient to export my articles to pdf, org, rts, latex...
https://pandoc.org/ also seems doesn't mention rmarkdown support.
I have tried to export .html form rstudio and use pandoc convert the html file back to markdown, but it seems doesn't work.
Actually pandoc is used to create pdf and other formats from r-markdown. Therefore there is an intermediate file with pandoc compatible markdown. You could retain this file by:
rmarkdown::render("document.Rmd", output_format = "pdf_document", run_pandoc = FALSE)

Converting Rmarkdown to PDF without RStudio

I would like to convert a *.Rmd to document to PDF without rstudio being available.
Current approach
Current approach follows the following steps:
*.Rmd document is passed to knitr: knit(input = "report.Rmd"))
Obtained md is converted via pandoc:
# Convert
pandoc --smart --to latex \
--latex-engine pdflatex \
-s report.md \
-o report.PDF
Problems
This results in the following problems, the top section of the Rmarkdown document:
---
title: "Report Title"
author: "Person"
output: pdf_document
classoption: landscape
---
and shows as:
all text is centered, whereas I would like for it to be left-aligned:
Possible approach
I would like to make use of the rmarkdown::render; however, despite setting RSTUDIO_PANDOC (as discussed here), the command fails on pandoc not being available.
Desired outcome
I don't care much whether the utilised mechanism makes use of the rmarkdown::render, what I want to achieve is:
Landscape page layout across all pages
Left-aligned text
Ability to exercise minimum control over the document by controlling default fonts
Ideally, I would like to do as much as in the *.Rmd file as possible without the need to add parameters to the pandoc command.
Updates, following comments
I'm working on Linux and pandoc is installed, I can execute pandoc command pass files and generate exports with no problems. It only doesn't work with the rmarkdown::render package.
Concerning the hooks and *.Rmd files, this is what I'm trying to understand as I see that that the first section of my *.Rmd file is ignored. The current process looks as follows:
*.Rmd (not much in it, just title section and dummy text and code that renders but wrongly justified) >
*.R file running one line knit(input = "report.Rmd")) >
*.sh file running pandoc command and generating PDF
Concerning:
if all that is in place, it is indeed just a call to
rmarkdown::render(...)
The rmarkdown::render(...) fails:
Error: pandoc version 1.12.3 is required and was not found ...
However:
>> rmarkdown::pandoc_available()
[1] TRUE
and:
$ pandoc -v
pandoc 1.9.4.1 (...)
The RSTUDIO_PANDOC points to pandoc.
A few things:
"the command fails on pandoc not being available." well you must have pandoc installed in order to call it -- but you didn't say what OS you have. On Linux it is pretty trivial to install pandoc from the package manager; otherwise jgm has binaries for you on the site; "should" be similar on OS X
for different styling you need to modify the LaTeX code which you can via numerous hooks to include macro files; see the RMarkdown cheat sheets for detail
if you want to exercise more control, you can supply your own template; I have done so in the tint package
(which is also on CRAN)
if all that is in place, it is indeed just a call to rmarkdown::render(...)
Error: pandoc version 1.12.3 is required and was not found
I think the error says it plainly: you need pandoc 1.12.3 and you have pandoc 1.9.4.1
I do not know, however, why such a specific version is required.

R Markdown - no ODT and LaTeX options as an output

I found R markdown/knitr useful tool to document my work and generate summary document.
I work with .Rmd (R markdown) files in RStudio.
It seems that knitr provide appropriate functionality to generate .odt (Open Document Text) and .tex (LaTeX) documents from .Rmd.
However, R studio allows to choose .docx, .html and .pdf formats only.
I would like to avoid MS Word format since I prefer open standards and working under Linux.
Is it possible to add .odt and .tex options to Rstudio menu?
It doesn't seem possible to output odt directly in RStudio, but you can always use knitr::knit to produce a markdown document and pandoc to produce the odt:
library(knitr)
knit("myDoc.Rmd")
system("pandoc myDoc.md -o myDoc.odt")
You may have to adjust the pandoc options and adapt the template to get a nice looking result.
As for latex, you can keep the tex sources when compiling to pdf with the following option in your yaml front matter:
---
output:
pdf_document:
keep_tex: true
---

Command for adding a LaTeX template to pandoc on R

I am trying to use Pandoc to convert a .md file to PDF. In doing this, I would like to add a LaTeX template into it. Is there a way to do this? If so, what is the command for doing it in RStudio?
The command I am currently using is the following
```{r}
pandoc("foo.md", format="latex")
```
Thank you in advance.
One way to do it is to use the function system and run pandoc directly, adding a Latex header.
For example:
system("pandoc -f markdown -t latex -o foo.pdf -H template.tex -V papersize:\"a4paper\" -V geometry:\"top=2cm, bottom=3cm, left=2cm, right=2cm\" foo.md ")
-f inicates the origin language, though I mix MarkDown and Latex and it works fine.
-t is the result language, though it really compiles the created latex and what you get is a .pdf document
-o the name of the file you want to create
-H a header to add. There is where you can put your template
-V many variables that you can set. Here I set the paper size and margins
at the end you write the name of your MarkDown file
template.tex is a tex file with the header I want in the Latex document. I use it to add packages, headers and some other parameters. For example:
\usepackage{booktabs}
\usepackage[spanish, es-tabla]{babel}
\usepackage{colortbl}
\usepackage{float}
\usepackage{fancyhdr}
\usepackage[singlelinecheck=false]{caption}
\setlength{\headheight}{40pt}
\pagestyle{fancy}
\lhead{My Title}
\rhead{\includegraphics[height=50pt]{MyGraph.png}}

Resources