How to convert Rmarkdown file to working latex file - latex

I have written a manuscript I would like to submit to a journal in Rmarkdown. The journal accepts word and latex files, so I am looking for a way to generate a working .tex file out of my .Rmd file.
I have read some posts that allude to this being possible (e.g., How to generate LaTeX file without preamble in R markdown?) and this is getting me some of the way, but I am still having problems.
For example, using the method mentioned in the post above, I can convert a test .Rmd into something with a .tex filetype. This is the test Rmarkdown (just the usual template for new files):
---
title: "Test document"
author: "Me"
date: "23 7 2020"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
This compiles fine to PDF as it should. Then, in the console I run:
knitr::knit("test.Rmd")
to get a markdown file test.md in my working directory, and then I can apparently convert this .md file to .tex with
rmarkdown::pandoc_convert("test.md", to = "latex", output = "test.tex")
This produces a .tex file that, when I double click on it, pops up a PDF view of the file that looks fine. Taking a look at the file though, it is incomplete or at least unfamiliar to me:
\hypertarget{r-markdown}{%
\subsection{R Markdown}\label{r-markdown}}
This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.
When you click the \textbf{Knit} button a document will be generated
that includes both content as well as the output of any embedded R code
chunks within the document. You can embed an R code chunk like this:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{summary}\NormalTok{(cars)}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
\end{verbatim}
\hypertarget{including-plots}{%
\subsection{Including Plots}\label{including-plots}}
You can also embed plots, for example:
\begin{figure}
\centering
\includegraphics{figure/pressure-1.png}
\caption{plot of chunk pressure}
\end{figure}
Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
As far as I can tell, it is missing the preamble, \begin{document}, \end{document}, and I have no idea what is going on with hypertarget bit of the section headers. Unsurprisingly, it does not "re-compile" when I hit run in MiKTeX. The verbatim bits for the code chunks look to be what I'm after, though.
So, is there a way to generate a .tex file that compiles out of an .Rmd file? Or will I have to manually write the preamble and all that? If the answer to my problem is "read up pandoc", then fair enough, I will have to bite the bullet and finally have a look at it. But I find it hard to imagine that there is no good (easy) way to prepare submittable manuscripts in Rmarkdown.

Pandoc generates LaTeX snippets by default, i.e., not a full document. This can be changed by calling pandoc with the --standalone option:
rmarkdown::pandoc_convert(
"test.md",
to = "latex",
output = "out.tex",
options = "--standalone"
)
You can let R do the work and shorten your commands to
render("test.Rmd", output_format = "latex_document")
A project of interest might be rticles.

Related

Pandoc fails to render valid LaTeX macro definitions from markdown (and R markdown)

In R, I have character objects that contain LaTeX macro definitions. The
challenge is to use these objects in R Markdown documents, so that the
macro definitions are rendered correctly when the .Rmd files are converted to
LaTeX (and then to PDF). It is a challenge because Pandoc (v2.9.1 and 2.9.2)
fails to render some macro-generating code correctly, even when that
code is valid LaTeX.
Here is a minimal example. First, consider this Rmd file:
---
title: "Rendering LaTeX Macros from R Objects"
output:
pdf_document:
keep_md: true
keep_tex: true
---
```{r}
withoutBraces <- "\\newcommand\\withoutBraces{This is a sentence.}"
withBraces <- "\\newcommand{withBraces}{This is a sentence.}"
```
```{r, results = "asis"}
writeLines(withoutBraces)
writeLines(withBraces)
```
Knitting this .Rmd file from RStudio produces a .tex file that includes the
following output:
\newcommand\withoutBraces{This is a sentence.}
but
\textbackslash newcommand\{withBraces\}\{This is a sentence.\}
In other words, the \withoutBraces command is rendered correctly in the .tex
document, but the \withBraces command is not. Inspection
reveals that the rmarkdown::render() part of the knitting process is fine, in
the sense that it produces an unproblematic .md file. The problem lies with
pandoc: when it converts the .md file to a .tex file, the \withBraces
command doesn't render correctly.
If I were writing .md files instead of .Rmd files, I could use "generic raw
attributes" in my code chunks to get the \withoutBraces macro definition to
render correctly, as in this example from
#mb21. But I don't see a way to
do that when working with R Markdown files. Is there anything that I can do
to get the \withoutBraces definition to render correctly when I am knitting
an .Rmd file to LaTeX and PDF?
The problem lay with a LaTeX formatting error on my part, not in any problem with pandoc. I had written
withBraces <- "\\newcommand{withBraces}{This is a sentence.}"
when I should have written
withBraces <- "\\newcommand{\\withBraces}{This is a sentence.}"
When I use the second string instead of the first, there is no problem with the conversion to LaTeX.

Using knit_child in RStudio to manage thesis .Rnw chapters in master .Rnw. \ref and \label don't work now

To say that I am a LaTeX amateur is an understatement, though by some miracle, I am managing to write my thesis in it. I am using RStudio to write and compile my thesis, due to all of my analysis being done in R and wanting the ability to insert dynamic plots etc.
As my thesis has increased in size, I wanted to break the chapters off into sub .Rnw files so that I could work on each chapter independently (with my need for R code within each chapter ruling out using .tex files). The only way I could get this to work, was using \Sexpr{knit_child('chapter.Rnw')}. I weave my files using knitr rather than Sweave as again, that is the only I can get it to compile correctly. Unfortunately, I've never managed to understand why!
Nevertheless, this is working very nicely, is much easier to manage and my plots and R code are compiling correctly, but my previously functioning \ref and \label commands no longer work. Well, they work within a chapter, but not between them.
I include my main document here and the latex commands that I think are relevant to my question. I have googled this all morning, but am getting nowhere alone.
\documentclass[12pt]{report} %What kind of document
\usepackage{titlesec} %can actually name chapters rather than having "Chapter 1" etc
\usepackage[backend=bibtex,style=authoryear-comp,sorting=nyt,maxcitenames=2,url=false]{biblatex}
\bibliography{library}
% ----------- KNITR SETUP ------------------------
<<setup, include=FALSE, cache=FALSE, echo=FALSE>>=
opts_chunk$set(fig.path='figures/plots-', fig.align='center', fig.show='hold', eval=TRUE, echo=TRUE)
options(replace.assign=TRUE,width=80)
# setwd("C:/Users/cainswor/Box Sync/Imperial/Reports/Thesis")
setwd("D:/BoxSync/BoxSync/Reports/Thesis")
data_loc <- "D:/BoxSync/BoxSync/Reports/Thesis/Data for Thesis"
Sys.setenv(TEXINPUTS=getwd(),
BIBINPUTS=getwd(),
BSTINPUTS=getwd())
x <- c("shiny","flowViz","nls2","plyr","RColorBrewer","abind","MASS","gplots","hexbin",
"data.table","fastmatch","stringr","hypergeo","rgl","mclust","knitr","dbscan")
lapply(x, require, character.only=T)
# Sweave2knitr('ThesisSecondDraft.Rnw')
#
\begin{document}
\Sexpr{knit_child('Th1_Introduction.Rnw')}
\part{The Experiment}
\Sexpr{knit_child('Th2_Characterisation.Rnw')}
\end{document}
Here is an example which work on my files.
In the main.Rnw document :
\documentclass{article}
\begin{document}
\SweaveOpts{concordance=TRUE}
Hello world
\SweaveInput{child_test.Rnw}
\end{document}
In the child_test.Rnw file
%!Rnw root = main.Rnw
\SweaveOpts{echo = TRUE, eval = TRUE}
\section{Analysis}
This is the analysis.
<<analysis, result = tex>>=
summary(cars)
#

Visibility of math formula in rmarkdown after knitting

I am trying to create an R Markdown file and include a formula. The problem is that when I knit the document the formula appears as I have entered in a latex style.
---
output: github_document
---
$$P(E|L) = \frac{P(L|E) * P(E)}{P(L)} = \frac{0.3 * 0.5}{0.3 * 0.5 + 0.5*1} = 0.23$$
So when I knit it, it looks like:
where as in the code it appears.
What should I do so that when I knit the document, the formula looks like in the second picture?
The problem is coming from your use of the output format github_document. The github output format cannot directly support the maths mode. As stated by the rmarkdown package author in this issue:
I'd say that if you really care about LaTeX math in Markdown, you should render to HTML output instead of waiting indefinitely for Github to support MathJax in Markdown, which I doubt will ever happen.
Implementing his advice, we can change the output format to html_document:
---
output: html_document
---
$$
P(E|L) = \frac{P(L|E) * P(E)}{P(L)} = \frac{0.3 * 0.5}{0.3 * 0.5 + 0.5*1} = 0.23
$$

compiling latex file generated by RStudio

I am trying to edit the latex file generated by RStudio but it seems the file generated by RStudio does not compile out of the box.
Here is the template rmarkdown (edited a little as I want to show code in my slides)
---
title: "Untitled"
author: "SN248"
date: "April 17, 2016"
output:
beamer_presentation:
keep_tex: true
theme: "CambridgeUS"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
Some code
```{r eval=FALSE, echo=TRUE}
install.packages("mypackage")
library(mypackage)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=TRUE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Note, that I added keep_tex: true to get the intermediate tex file, the generated tex file is pasted below:
\begin{frame}[fragile]{R Markdown}
This is an R Markdown document. Markdown is a simple formatting syntax
for authoring HTML, PDF, and MS Word documents. For more details on
using R Markdown see \url{http://rmarkdown.rstudio.com}.
Some code
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
\end{frame}
\begin{frame}[fragile]{Including Plots}
You can also embed plots, for example:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{plot}\NormalTok{(pressure)}
\end{Highlighting}
\end{Shaded}
\includegraphics{Untitled_files/figure-beamer/pressure-1.pdf}
Note that the \texttt{echo\ =\ FALSE} parameter was added to the code
chunk to prevent printing of the R code that generated the plot.
\end{frame}
I noticed that the generated tex file does not compile out of the box. So, I added the necessary bits (i.e., package listings for code highlighting) so that it compiles
\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}
\usepackage{listings}
\usetheme{CambridgeUS}
\begin{document}
...
\end{document}
I am still not able to compile this tex file as the bit begin{Shaded} does not compile, I get the following error:
LaTeX Error: Environment Shaded undefined
My question is, how to generate tex file from RStudio which compiles out of the box. If not, which packages should I use to compile the tex code (especially to show code with highlighting) shown below
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{install.packages}\NormalTok{(}\StringTok{"mypackage"}\NormalTok{)}
\KeywordTok{library}\NormalTok{(mypackage)}
\end{Highlighting}
\end{Shaded}
Just to give some context, I am trying to compile the tex file so that I can add sections and subsections in the tex code, I am not sure how to do that in the rmarkdown file.
Thank you very much for your help.
SN248
I can see that you are using RMarkdown and want to generate a stand alone .tex file that can be compiled without knitr.
I do the same thing with SWeave. As SWeave is a latex document with R code, you can easily compile it once SWeave is run and figures are created.
For Example, I have created this document in RStudio and compile it in Rstudio.
\documentclass{article}
\title{Best title ever}
\author{XTriMa, PhD}
\begin{document}
\maketitle
\tableofcontents
\section{Method} \label{intro}
Let me make two vectors with random numbers and plot them.
<<>>=
x<-runif(100)
y <- x^(sin(x))
plot(x, y)
#
\end{document}
My folder looks like this
figure test.log test.Rnw test.tex
test-concordance.tex test.pdf test.synctex.gz test.toc
Now if you run
$pdflatex test.tex
It works as you intended. However command "latex" will not work as it can not work with pdf graphics in figures.
Advantage: I can work with R and latex very efficiently and get rid of intermediate software/clients like RStudio and Sweav.

Pandoc generation of pdf from markdown 4th header is rendered differently

I am using pandoc to generate a pdf from some markdown. I am using h1 through h4 via hash symbols. Example h1=#, h4=####. When I generate my document like this:
pandoc input.md -o output.pdf
I get a document where h1, h2 and h3 have a newline after them but h4 does not have a new line. The text starts on the same line as the header (it isn't formatted the same way, but there isn't a newline character between).
I've tried adding spaces after the #### and adding manual line returns using my editor but nothing seems to work.
Any ideas?
While the mentioned solutions work fine, pandoc offers a build-in variable to enable block-headings for \paragraph.
pandoc -s -o out.pdf some.md -V block-headings
Pandoc Manual
pandoc generates PDFs via LaTeX. In LaTeX, "headers" are generated using the following commands:
\section
\subsection
\subsubsection
\paragraph
\subparagraph
As you can see, a "level four heading" corresponds to the \paragraph command, which is rendered as you describe. There simply isn't a \subsubsubsection command to use.
The only way to get what you want is to redefine the \paragraph command, which is quite tricky. I haven't been able to make it work with Pandoc.
While #tarleb’s answer is surely the best (except that it specifies a
wrong amount of vertical space), here is a “simpler” (by some measure)
but more hacky (in LaTeX terms at least) solution which optionally uses a Pandoc Lua filter or a LaTeX hack but avoids loading another LaTeX package.
We want the LaTeX source to look something like this:
\hypertarget{level-4-heading}{%
\paragraph{Level 4 heading}\label{level-4-heading}}
\hfill
Lorem ipsum dolor sit amet.
This LaTeX looks awful, but if you don’t need to keep or share the LaTeX
source it does what you probably want: a space between the level 4
heading and the paragraph after it equal to the space between a level 3
heading and the paragraph after it.
Here is how it works: since a \hfill on a line on its own is about as
close as you can get to an empty paragraph in LaTeX you get a first
paragraph — the one running in with the heading — containing only
horizontal white space until the end of the line, and then immediately
after a new paragraph — the actual first paragraph after the heading —
with just a normal paragraph space between it and the heading. This
probably also upsets LaTeX’s idea about what a \paragraph should be
like as little as possible.
The “manual” way to do this is as follows:
#### Level 4 heading
````{=latex}
\hfill
````
Lorem ipsum dolor sit amet.
This uses Pandoc’s relatively new raw markup syntax — the “code block”
is actually a raw LaTeX block — but it looks even more awful than the
resulting LaTeX source! It is also a tedious chore to have to insert
this after every level 4 heading. In other words you want to insert that
raw LaTeX automatically, and that can be done with a Lua filter:
--[======================================================================[
latex-h4-break.lua - Pandoc filter to get break after a level 4 heading.
Usage:
$ pandoc --lua-filter latex-h4-break.lua input.md -o output.pdf
--]======================================================================]
-- create it once, use it many times!
local hfill_block = pandoc.RawBlock('latex', '\\hfill')
function Header (elem)
if 4 == elem.level then
return { elem, hfill_block }
else -- ignore headings at other levels!
return nil
end
end
However you can also do a simple LaTeX hack in a header-includes
metadata block to get the same effect:
---
header-includes:
- |
``` {=latex}
\let\originAlParaGraph\paragraph
\renewcommand{\paragraph}[1]{\originAlParaGraph{#1} \hfill}
```
---
#### Level 4 heading
Lorem ipsum dolor sit amet.
This works by first creating an “alias” of the \paragraph command and
then redefining the \paragraph command itself, using the alias in the
new definition so that now wherever the LaTeX source created by Pandoc
contains \paragraph{Foo} it is if it instead had contained
\paragraph{Foo} \hfill which does what we want with zero extra
dependencies! (In case you wonder the wacky spelling of the “aliased”
command is to minimize the risk that it collides with anything which
already exists, since the TeX \let command doesn’t check for that. We
certainly don’t want to overwrite any existing command!)
NOTE: If you really should want more or less space than a normal
paragraph break after the heading just add an appropriate \vspace
command after the \hfill: \hfill \vspace{-0.5\parskip}.
Shifting Headers
Arguably the best way to solve this would be to avoid the problem altogether by shifting what a level 4 header corresponds to. The default for pandoc is to use \section commands for 1st level and \paragraph for 4th level headers. This can be altered via the --top-level-division parameter:
--top-level-division=[default|section|chapter|part]
Treat top-level headers as the given division type in LaTeX [...] output. The hierarchy order is part, chapter, then section; all headers are shifted such that the top-level header becomes the specified type. The default behavior is to determine the best division type via heuristics [...]
So with --top-level-division=chapter, a 4th-level header would be generated via the \subsubsection command.
Styling via LaTeX
If this is not an option, the next best way is to configure the layout of the corresponding LaTeX command: for level-four headers, this is \paragraph by default. The following methods are taken from TeX StackExchange answers.
Please also check the answer by bpj, which is much simpler than what's proposed below.
Default document-classes
The default way would be to configure \paragraph via the titlesec package. We can use the header-includes metadata field for this, which pandoc will include in the intermediate LaTeX document.
---
header-includes: |
``` {=latex}
\usepackage{titlesec}
\titlespacing*{\paragraph}{0pt}{1ex}{-\parskip}
\titleformat{\paragraph}[hang]
{\normalfont\bfseries}
{}
{0pt}
{}
```
---
KOMA document-classes
Using titlesec won't work properly for documents using KOMA classes (like scrartcl), as KOMA has it's own ways of doing things. For these, use this alternative snippet:
---
documentclass: scrartcl
header-includes: |
``` {=latex}
\makeatletter
\renewcommand\paragraph{\#startsection{paragraph}{4}{\z#}%
{-3.25ex \#plus -1ex \#minus -0.2ex}%
{0.01pt}%
{\raggedsection\normalfont\sectfont\nobreak\size#paragraph}%
}
\makeatother
```
---
I am not sure, why, but this works for me:
Put $\ \\ $ in the first line after your #### headline

Resources