# Keep previous PDF file if error during conversion from .md to .pdf

Sometimes, it happens that R Markdown documents that worked and produced a PDF document don't compile anymore because I changed the location of a file that was used for this document, or because I had to reinstall tinytex and I forgot to install packages used in this document. When I compile this .Rmd file, it throws an error and the PDF that was produced by this file disappears. Is it possible to replace the previous PDF file only if the .Rmd compiles without a problem, and to keep this previous PDF file if the compilation does not work?

For example, the .Rmd file below works and produces a PDF document:

---
title: "something"
author: "somebody"
date: "someday"
output:
bookdown::pdf_document2
toc: false
---

something

But if I add some LaTeX code that doesn't work, the PDF produced by the code above disappears:

---
title: "something"
author: "somebody"
date: "someday"
output:
bookdown::pdf_document2
toc: false
---

something
\textcolor{unknowncolor}{text}

I guess that the deletion of this file occurs when Pandoc converts .md files to .pdf files since the PDF previously produced does not disappear if we replace the bad LaTeX code with some R code that throws an error.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.