I was hoping to keep all my R Markdown and other input files in a sub-folder (e.g., .../source
), and have the final output pdf automatically go to the parent folder (.../
). In case it is relevant, the output is to beamer_presentation
. Is this possible with R Markdown or Pandoc?
I want to keep the .tex
file for reference and debugging, but I consider it to be an intermediate output, so I would rather keep it in the same folder as the input file, not the final output destination with the pdf file.
I found this answer, which shows how to set the output_dir
for the render()
function right in the document YAML header, which works ... except that it puts the .tex
file in the same output directory as the final .pdf
file, which is not what I want.
I was able to achieve the desired result with a manual call to xelatex
with the -output-directory=..
option (something similar appears to be possible with pdflatex
, but that engine is not an option for my use case): e.g., xelatex -output-directory=.. input.tex
. But I have not figured out how to pass this argument through rmarkdown to pandoc (and on to xelatex as the pdf-engine
). I tried the following, but the final pdf file still ended up in the same directory as the input file:
pdf-engine-opt: "-output-directory=.."
in the YAML header (based on the pandoc documentation). This appeared to have no effect.pandoc_args: ["--pdf-engine-opt=--output-directory=.."]
in the YAML header.- This seems to add the argument to the pandoc call (visible in the 'render' tab in RStudio), but does not change the location of the pdf file.
- EDIT: I found other answers explaining that the pdf-engine is actually called by tinytex::latexmk, which explains why the pandoc arguments don't do anything. I tried adding this line to an r chunk (the default setup chunk) with the
include=FALSE
option:options(tinytex.engine_args = '--output-directory=..')
- This seemed to produce the desired effect, except that it produced an Error:
Error: LaTeX failed to compile input.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
Execution halted
It looks like there is a way to pass command-line arguments to pandoc via the pandoc_options()
function in the rmarkdown
package, but I can't figure out how to use that without having to manually set all the other arguments in a render()
call. And if it did not work with pandoc_args:
, I'm not sure what more the rmarkdown
functions can do to pass on the --output-directory=
option to xelatex.
Is this a rmarkdown
issue or a or am I doing something wrong?pandoc
limitation,
EDIT:
The general advice is to keep .tex
, auxiliary files, and the final output pdf in the same directory, to avoid problems with TeX rendering. As an alternative, is there a way to run some R code or system commands to automatically move/copy the final output to a desired destination?