How to convert latex files to 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}

\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}

\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}

\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}.

\begin{figure}[bt]
\centering
\includegraphics[width=0.8\textwidth]{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 have not heard of the converter but you can simply copy and paste the latex code in the rmarkdown file. Some editing will be required but it will eventually work.

1 Like

Knit the following in RStudio

---
title: "LaTeX conversion"
author: "Richard Careaga"
date: "12/27/2019"
output: html_document
---


From tex source to markdown with

  pandoc -f latex -t markdown demo.tex -o demo.md

Probability can be illustrated by rolling a die many times. Let p̂ n be
the proportion of outcomes that are after the first n rolls. As the
number of rolls increases, p̂ n will converge to the probability of
rolling a , p = 1 / 6 .

Cut-and-paste tex

Probability can be illustrated by rolling a die many times. Let \^ p n
be the proportion of outcomes that are after the first n rolls. As the
number of rolls increases, \^ p n will converge to the probability of
rolling a , p = 1 / 6 .

There are a few challenges to surmount to transform the text:

  1. It is not UTF-8 encoded, and pandoc will choke without conversion.
  2. It is a complex document with a lot of includes of other documents; pandoc has a much easier time with self-contained documents
  3. To render much of the document, you will need to bring in \LaTeX headers into your yaml front matter. There are a lot in this document.
2 Likes

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