0

I would like to import a .tex file into my RMarkdown document. I am using bookdown and importantly need to export to Word via word_document2. Is it possible to do this? Here is the way I would do this using LaTeX:

\begin{table}[H]
\caption{Table Title}
\label{usnwr_id}
\centering
\scalebox{0.8}{
\input{\dir/filename.tex}}
\caption*{\footnotesize \emph{Notes: ...}}
\end{table}

If there is a way to retain the LaTeX code, that would be ideal (because I am transferring everything over).

And here is a skeleton version of filename.tex

\begin{tabular}[t]{llc}
\toprule
AA & BB & CC\\
\midrule
a & b & c\\
\bottomrule
\end{tabular}
bill999
  • 2,147
  • 8
  • 51
  • 103
  • 2
    Unrelated to the question, but instead of scaling content which contains text, better choose an appropriate font size, this will give a better typographical result. – samcarter_is_at_topanswers.xyz Oct 15 '21 at 19:01
  • Good point. Thank you, @samcarter_is_at_topanswers.xyz. – bill999 Oct 15 '21 at 21:14
  • 1
    you can run tex code from anywhere inside the rmd by adding \input{filename.tex} (filename.tex can not inclue \usepackage (use header-includes) nor \begin{documen}) . This only works with pdf output to my knowledge. – user12256545 Oct 20 '21 at 00:03
  • Could you share the filename.tex file or at least a part of it? That would make it easier to test a few things. – JBGruber Oct 21 '21 at 13:16
  • Great suggestion, @JBGruber. I have updated the question. – bill999 Oct 21 '21 at 14:50

1 Answers1

2

I tried solving this problem using pandoc. You can see an example from this question.

But as it was written in another question:

This does not work for more complicated tables.


I asked some of my friends: "What would you do in this situation?"

One of them told me about this tool to convert LaTeX tables to Markdown tables. It works nicely without tricky and big tables.

Converting

Let's have a look at your table in LaTeX.

\begin{tabular}[t]{llc}
\toprule
AA & BB & CC\\
\midrule
a & b & c\\
\bottomrule
\end{tabular}

The tool will convert the table into Markdown.

---
title: "test"
output:  
  word_document: default
---

| AA | BB | CC |
|:---:|:---:|:---:|
| a  | b  | c  |

And it looks like this in Microsoft Word.

Word Document


This is all that I wanted to say. If it is not suitable for you or somebody couldn't solve this problem as expected – another option could be to try some R table packages like gt, flextable, kableExtra, or other packages of your choice.

manro
  • 3,529
  • 2
  • 9
  • 22