Search code examples
rlatex

Instant Conversion from Latex to PDF using R


I have the following text in R :

Here is a Multinomial Logistic Regression:


$$
P(Y=k|X_1, X_2, ..., X_p) = \frac{e^{(\beta_{k0} + \beta_{k1}X_1 + \beta_{k2}X_2 + ... + \beta_{kp}X_p)}}{1 + \sum_{j=1}^{K-1} e^{(\beta_{j0} + \beta_{j1}X_1 + \beta_{j2}X_2 + ... + \beta_{jp}X_p)}}
$$

for $k = 1, 2, ..., K-1$, and

$$
P(Y=K|X_1, X_2, ..., X_p) = \frac{1}{1 + \sum_{j=1}^{K-1} e^{(\beta_{j0} + \beta_{j1}X_1 + \beta_{j2}X_2 + ... + \beta_{jp}X_p)}}
$$

Where :

$$
\beta = 
\begin{bmatrix}
\beta_{10} & \beta_{11} & \beta_{12} & \ldots & \beta_{1p} \\
\beta_{20} & \beta_{21} & \beta_{22} & \ldots & \beta_{2p} \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
\beta_{(K-1)0} & \beta_{(K-1)1} & \beta_{(K-1)2} & \ldots & \beta_{(K-1)p}
\end{bmatrix}
$$

Is there some way to take this mixed english/latex text and directly convert it into a PDF document in R?

I have seen some libraries/packages which do parts of this - but is there some direct way to do this?

e.g.

# imaginary function that converts text to pdf (while preserving spaces/indents):

latex_to_pdf_function(output = myfile.pdf, text = "Here is a Multinomial Logistic Regression:


$$
P(Y=k|X_1, X_2, ..., X_p) = \frac{e^{(\beta_{k0} + \beta_{k1}X_1 + \beta_{k2}X_2 + ... + \beta_{kp}X_p)}}{1 + \sum_{j=1}^{K-1} e^{(\beta_{j0} + \beta_{j1}X_1 + \beta_{j2}X_2 + ... + \beta_{jp}X_p)}}


$$ ..... ")

Solution

  • It's not super straightforward, but it is possible. First you can write a function that will handle the relevant inputs. You will have to have a preamble and a document ending - you cannot just render latex text independent of the context, as far as I know. Here's what the function could look like - it uses a default preamble and closing that could be changed:

    latex_to_pdf_function <- function(
      output = "myfile.pdf", 
      text = NULL, 
      preamble = NULL, 
      closing = NULL, 
      ...){
      if(is.null(preamble)){
        pre <- "\\documentclass{article}\n\\usepackage{amsmath}\n\\usepackage{amssymb}\n\\begin{document}\n"
      }
      if(is.null(closing)){
        closing <- "\\end{document}"
      }
    flnm <- gsub("\\.pdf?", "", output)  
    tf <- tempfile(flnm, fileext=".tex")                      
    cat(pre, text, closing, sep="\n", file=tf)
    tinytex::pdflatex(tf, pdf_file = output)
    }
    

    Next, you'll have to define the input text. In this case, we'll need to use the r"(...)" construct of reading in and managing raw strings. This will convert all \ to \\ in the text to ensure the appropriate escaping is done. For example:

    txt <- r"(
    Here is a Multinomial Logistic Regression:
    
    
    $$
    P(Y=k|X_1, X_2, ..., X_p) = \\frac{e^{(\beta_{k0} + \beta_{k1}X_1 + \beta_{k2}X_2 + ... + \beta_{kp}X_p)}}{1 + \sum_{j=1}^{K-1} e^{(\beta_{j0} + \beta_{j1}X_1 + \beta_{j2}X_2 + ... + \beta_{jp}X_p)}}
    $$
    
    for $k = 1, 2, ..., K-1$, and
    
    $$
    P(Y=K|X_1, X_2, ..., X_p) = \frac{1}{1 + \sum_{j=1}^{K-1} e^{(\beta_{j0} + \beta_{j1}X_1 + \beta_{j2}X_2 + ... + \beta_{jp}X_p)}}
    $$
    
    Where :
    
    $$
    \beta = 
    \begin{bmatrix}
    \beta_{10} & \beta_{11} & \beta_{12} & \ldots & \beta_{1p} \\
    \beta_{20} & \beta_{21} & \beta_{22} & \ldots & \beta_{2p} \\
    \vdots & \vdots & \vdots & \ddots & \vdots \\
    \beta_{(K-1)0} & \beta_{(K-1)1} & \beta_{(K-1)2} & \ldots & \beta_{(K-1)p}
    \end{bmatrix}
    $$)"
    

    Finally, we can use the function to generate the pdf:

    latex_to_pdf_function(text = txt)
    

    For me, this made myfile.pdf in R's working directory with the following contents:

    enter image description here