Home > Blockchain >  R markdown does not covert .tex table to word document, while it works fine in pdf. How to make both
R markdown does not covert .tex table to word document, while it works fine in pdf. How to make both

Time:08-15

I want to put a iv.tex into a test.rmd and export it as a word_document.

The problem is it does not show the table in the .docx but it does in .pdf.

My question: How to have the same result in both .docx and .pdf? Please see below for additional information. Thank you.

The following word_test.rmd compiles to .docx, but it does not show the table. I tried: 1. \input, 2. =latex. None of them works. See the output image and code below

word_test.rmd

word_text.docx output image here

---
title: "word_test"
output: "word_document"
date: '2022-08-09'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Test Tables with `input`

\input{"iv.tex"}


## Test Tables with `=latex`

```{=latex}
\include{"iv.tex"}
```

While the same code does produce the desired outcome if its output is pdf_document, see image and code below.

pdf_test.rmd

pdf_test.rmd output image here

---
title: "pdf_test"
output: "pdf_document"
date: '2022-08-09'
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Test Tables with `input`

\input{"iv.tex"}


## Test Tables with `=latex`

```{=latex}
\input{"iv.tex"}
```

iv.tex

iv.tex is a table, see below.

\begin{table}[htbp]\centering\scriptsize    
  \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
  \caption{myTable\label{tab:mlogit}}
  \begin{tabular}{l*{9}{c}}
  \hline\hline
                      &\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&\multicolumn{1}{c}{(4)}&\multicolumn{1}{c}{(5)}&\multicolumn{1}{c}{(6)}&\multicolumn{1}{c}{(7)}&\multicolumn{1}{c}{(8)}&\multicolumn{1}{c}{(9)}\\
                    %  &\multicolumn{1}{c}{lexpenditure}&\multicolumn{1}{c}{Poverty}&\multicolumn{1}{c}{Dummy for multidimensional poverty index (k=0.33)}&\multicolumn{1}{c}{lexpenditure}&\multicolumn{1}{c}{Poverty}&\multicolumn{1}{c}{Dummy for multidimensional poverty index (k=0.33)}&\multicolumn{1}{c}{lexpenditure}&\multicolumn{1}{c}{Poverty}&\multicolumn{1}{c}{Dummy for multidimensional poverty index (k=0.33)}\\
  \hline
  var1    &      0.000\sym{***}&    5e-15\sym{***}&    -0.00551         &      -0.000\sym{***}&   5e-15\sym{***}&       0.000\sym{***}&      -0.000\sym{***}&    5e-15\sym{***}&       0.000\sym{***}\\
                      &   (0.000)         &  (5e-15)         &   (0.000)         &    (0.000)         &  (5e-15)         &    (0.000)         &    (0.000)         &  (5e-15)         &    (0.000)         \\
  var2  &      0.000\sym{***}&   5e-15\sym{**} &      -0.000\sym{***}&       0.000\sym{***}&    5e-15\sym{***}&      -0.000\sym{***}&       0.000\sym{***}&   5e-15         &      -0.000\sym{***}\\
                      &   (0.000)         &  (5e-15)         &    (0.000)         &    (0.000)         &  (5e-15)         &     (0.000)         &    (0.000)         &  (5e-15)         &     (0.000)         \\
  var3     &      0.000\sym{***}&    5e-15\sym{***}&     -0.000\sym{***}&      -0.122         &    5e-15         &       0.174         &      -0.135         &   5e-15\sym{***}&       0.235         \\
                      &    (0.000)         &  (5e-15)         &    (0.000)         &     (0.000)         &  (5e-15)         &     (0.000)         &     (0.000)         &  (5e-15)         &     (0.000)         \\
  var4          &      0.000\sym{***}&   5e-15\sym{**} &     -0.000\sym{***}&       0.000\sym{***}&   5e-15\sym{***}&      -0.000\sym{***}&       0.000\sym{***}&    5e-15\sym{***}&      -0.000\sym{***}\\
                      &    (0.000)         &  (5e-15)         &    (0.000)         &    (0.000)         &  (5e-15)         &    (0.000)         &    (0.000)         &  (5e-15)         &    (0.000)         \\
  \hline
  Observations        &       999         &       999         &       999         &       999         &       999         &       999         &       999         &       999         &       999         \\
  \hline\hline
  \multicolumn{10}{l}{\footnotesize Standard errors in parentheses}\\
  \multicolumn{10}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
  \end{tabular}
  \end{table}

CodePudding user response:

The idea behind raw snippets is that it allows to include some extra content, that does not apply to other formats. So normally, R Markdown won't touch these snippets but pass them through, but only when the output format matches. That's why the table shows up in PDF, but not Docx.

R Markdown is very flexible, so we can modify its behavior. The best approach here is to use a Lua filter to parse the LaTeX code when going to a format that does not support LaTeX snippets.

Save the following code to a file parse-latex.lua, and put it in the same directory as your .Rmd file.

if FORMAT == 'latex' then
  return {}
end

function RawBlock (raw)
  if raw.format:match 'tex' then
    print(raw.text)
    return pandoc.read(raw.text, 'latex').blocks
  end
end

Then instruct R Markdown to invoke the filter by changing your YAML header to

---
title: "word_test"
output:
  word_document:
    pandoc_args:
      - '--lua-filter=parse-latex.lua'
date: '2022-08-09'
---

There is one remaining problem though: pandoc is not good at knowing when the parser should be in math mode, so the \ifmmode in your \sym definition doesn't work the way it does in LaTeX. Replace it with this instead:

\def\sym#1{\textsuperscript{#1}}

Your tables should now show up even in docx output.


I've put the filter into a repo at tarleb/parse-latex; see there for more details and Quarto usage instructions.

  • Related