Latex In R Markdown



Any advice for how to convert a latex file to R markdown, even imperfectly? My problem is that I want to use some of the material from the OpenIntro project, specifically Appendix A on Probability from Intro Stat with Randomization and Simulation. They provide the latex source files from which the PDF is created. How can I turn that latex source into R markdown?

Here is a copy of the key file. I provide it since it is a bit bothersome to get this directly from OpenIntro.

For reference, below is the top of the latex source. I realize that latex has options with no analog in Rmd, so no conversion will be perfect. I just assume/hope that someone else has already written a converter.

chapter{Probability}
label{probability}

Oct 09, 2020 Defining a new LaTeX command in a R Markdown document is also quite straight forward. Define the command as you would in LaTeX. Put it below the YAML header at the beginning of your R Markdown. R markdownis a particular kind of markdown document. Authors should be cautious about following formatting advice for other types of markdown when working on R markdown. The distinguishing feature of R markdownis that it cooperates with R. Like LATEX with Sweave, code chunks can be included. When the document is compiled, the code is executed.

index{probability|(}

Probability forms a foundation for statistics. You might already be familiar with many aspects of probability, however, formalization of the concepts is new for most. This chapter aims to introduce probability on familiar terms using processes most people have seen before.

section{Defining probability}
label{basicsOfProbability}

begin{example}{A ``die', the singular of dice, is a cube with six faces numbered resp{1}, resp{2}, resp{3}, resp{4}, resp{5}, and resp{6}. What is the chance of getting resp{1} when rolling a die?}label{probOf1}
If the die is fair, then the chance of a resp{1} is as good as the chance of any other number. Since there are six outcomes, the chance must be 1-in-6 or, equivalently, 1/6.
end{example}

begin{example}{What is the chance of getting a resp{1} or resp{2} in the next roll?}label{probOf1Or2}
resp{1} and resp{2} constitute two of the six equally likely possible outcomes, so the chance of getting one of these two outcomes must be 2/6 = 1/3.
end{example}

Write Latex In R Markdown

begin{example}{What is the chance of getting either resp{1}, resp{2}, resp{3}, resp{4}, resp{5}, or resp{6} on the next roll?}label{probOf123456}
100%. The outcome must be one of these numbers.
end{example}

Latex

begin{example}{What is the chance of not rolling a resp{2}?}label{probNot2}
Since the chance of rolling a resp{2} is 1/6 or 16.bar{6}%, the chance of not rolling a resp{2} must be 100% - 16.bar{6}%=83.bar{3}% or 5/6.

Alternatively, we could have noticed that not rolling a resp{2} is the same as getting a resp{1}, resp{3}, resp{4}, resp{5}, or resp{6}, which makes up five of the six equally likely outcomes and has probability 5/6.
end{example}

begin{example}{Consider rolling two dice. If 1/6^{th} of the time the first die is a resp{1} and 1/6^{th} of those times the second die is a resp{1}, what is the chance of getting two resp{1}s?}label{probOf2Ones}
If 16.bar{6}% of the time the first die is a resp{1} and 1/6^{th} of emph{those} times the second die is also a resp{1}, then the chance that both dice are resp{1} is (1/6)times (1/6) or 1/36.
end{example}

textA{newpage}

subsection{Probability}

index{random process|(}

We use probability to build tools to describe and understand apparent randomness. We often frame probability in terms of a term{random process} giving rise to an term{outcome}.
begin{center}
begin{tabular}{lll}
Roll a die &rightarrow & resp{1}, resp{2}, resp{3}, resp{4}, resp{5}, or resp{6}
Flip a coin &rightarrow & resp{H} or resp{T}
end{tabular}
end{center}
Rolling a die or flipping a coin is a seemingly random process and each gives rise to an outcome.

begin{termBox}{tBoxTitle{Probability}
The term{probability} of an outcome is the proportion of times the outcome would occur if we observed the random process an infinite number of times.}
end{termBox}

Probability is defined as a proportion, and it always takes values between 0~and~1 (inclusively). It may also be displayed as a percentage between 0% and 100%.

Probability can be illustrated by rolling a die many times. Let hat{p}_n be the proportion of outcomes that are resp{1} after the first n rolls. As the number of rolls increases, hat{p}_n will converge to the probability of rolling a resp{1}, p = 1/6. Figure~ref{dieProp} shows this convergence for 100,000 die rolls. The tendency of hat{p}_n to stabilize around p is described by the term{Law of Large Numbers}.

R Markdown Download

begin{figure}[bt]
centering
includegraphics[width=0.8textwidth]{appendix-probability/figures/dieProp/dieProp}
caption{The fraction of die rolls that are 1 at each stage in a simulation. The proportion tends to get closer to the probability 1/6 approx 0.167 as the number of rolls increases.}
label{dieProp}
end{figure}

I often need to write short reports which are not full blown manuscripts, e.g. annual grant progress reports. Though such documents don’t need to adhere to a strict template, I still want them to look nice. I’ve accomplished this for years by writing directly in LaTeX, but I want to align my process with my recent transition to composing most docs in RStudio/Rmd. Ultimately though, I don’t want to abandon the LaTeX look in the compiled document. Thankfully, RStudio will render a LaTeX pdf, but formatting beyond the defaults (which are still nice!) can be a bit mysterious. This repository holds my working template for such purposes.

Latex In R Markdown

Here’s a minimal example of what the defaults within a .Rmd will give you:

Now, two specific things I’d like to change are:

  • Left-justify the title/author/date section
  • Modify the font specs used in section titles

A solution to these two problems easily generalizes to the broader question of “How do I format the title and H1-H6 specs in the context of LaTeX rendering from .Rmd files?”

To start, we will borrow the LaTeX template R Markdown is currently using (h/t SO). The relevant remote repo is here, and you can copy the local version you’re using into your working directory with this line:

If you look past the pandoc nastiness in this template file (I at least find it nasty, being that I was mostly unfamiliar with pandoc scripting!), you’ll see familiar LaTeX commands that are often surrounded by $if(X)$ statements that are triggered if X appears in your .Rmd YAML. Here’s a straightforward example where, if you have title: in your .Rmd YAML header, the maketitle command will be executed in your LaTeX render:

Now the problem feels more tractable: all we have to do is modify the maketitle defaults in the usual LaTeX manner within the template.tex document. Let’s start with the following:

And don’t forget to include template.tex in your .Rmd YAML header like so:

Rendering gives the below, nice!

Next, we clearly need to fix the fact that section titles are now larger than the document title! Let’s do this with the LaTeX sectsty package – you can basically stuff this code anywhere in the preamble, like so:

Let’s also reduce the overall margins a touch via the geometry argument in the YAML while we’re at it. Here’s the full .Rmd:

Mission accomplished!

Here’s a bonus I learned along the way. You can send custom arguments to your .tex doc by simply defining new variables in the YAML header (relevant info in the pandoc docs here). Suppose I want an optional subtitle parameter. This is accomplished like so:

With this trick, you can start to do even fancier things (literally), such as include fancyhdr options. This option is ultimately included in the template.tex provided in the repository, and here’s the relevant YAML and output:

Latex In R Markdown Cheat Sheet

Happy R Markdowning!