Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KnitR: Is there a command to compile a tex file to a PDF?

Could anyone tell me how to compile a tex file to a pdf using Knitr commands in R?

I am automating a process of report creation for my job and there is a specific report type where I need to knit a tex file, this is done in the following way:

knitr::knit('\somedirectory..')

I then use the shell command to execute a Python script for 'regex' reasons.

At this point I am left with the tex file in the correct format but cannot figure out how to execute the final step of creating the PDF from within R.

The whole process needs to be automated so loading up TeXmaker or the like is unfortunately not an option.

The closest thing I have found to an answer is in this post: Is it possible to compile .tex files to PDF with 'pandoc'?

However, I am not at all familiar with using pandoc directly. Can these pandoc commands be executed in R somehow?

Many thanks to any help in advance.

like image 527
Frost_Maggot Avatar asked Jan 24 '17 14:01

Frost_Maggot


1 Answers

In the tools package, you will find a function called texi2pdf a wrapper of texi2dvi. This will build a pdf from a .tex file provided that you have all of the proper applications installed, etc.

So, assuming you copy the below text into a file named myTexFile.tex, and put the file in your working directory,

texi2pdf("myTexFile.tex")

will compile a pdf.

Latex to copy to file

\documentclass{article}

\begin{document}


\title{My Article}
\author{somebody\\
        some institution\\
        some place}

\date{\today}
\maketitle

\begin{abstract}
Here is an exciting abstract. Look at all the important stuff in my paper and publish it immediately.
\end{abstract}

\section{Introduction}
This is a longer set of reasons why this paper is so important.

\section{Data}
Here is a description of my amazing dataset. No one else has ever had access to this in the history of civilazation.

\subsection{Main Data}
Some more stuff about it.

\end{document}
like image 81
lmo Avatar answered Oct 15 '22 05:10

lmo