1

I have a bunch of quite big tables in markdown that I created manually. I was using them in an Rmd document. Since I need more control with LaTeX and all, I am using a Rnw document. How can I put my markdown table in the Sweave file?

Below a minimal example (not working):

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

% my markdown table

col1 | col2 | col3
------|:---:|:---:
row1 | cell1 | cell2 
row2 | cell3 | cell4 
row3 | cell5 | cell6 


\end{document}

I've tried to convert the table inside the document, just to paste the table in markdown in the Sweave document, and get it rendered in LaTeX. My try yields errors, but I am closer:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<texifytable, echo=FALSE, results=tex>>=
mytab = sprintf("col1 | col2 | col3
------|:---:|:---:
row1 | cell1 | cell2 
row2 | cell3 | cell4 
row3 | cell5 | cell6")
system2("pandoc", args = c("-f markdown","-t latex"),
        stdout = TRUE, input = mytab)
@

\end{document}
CL.
  • 14,577
  • 5
  • 46
  • 73
  • 1
    If using LaTeX, then you have to first convert your manually-created markdown tables to TeX, eg via `pandoc` as `pdflatex` doesn't know about markdown. – daroczig Feb 24 '16 at 05:10
  • Thanks for the answer @daroczig OK, I thought it could be possible to call pandoc/pander from inside the Rnw file to transform the table from markdown to TeX. – José Manuel Martínez Martínez Feb 24 '16 at 07:38
  • actually, you could try parsing it with pandoc's markdown reader and latex writer `pandoc myfile.tex -f markdown -t latex -o myfile_transformed.tex`. Pandoc should pass all latex commands unchanged, and convert the tables. (that would work only with your first example obviously) – scoa Feb 24 '16 at 09:26

1 Answers1

1

It's doable:

\documentclass{article}
\usepackage{longtable}
\usepackage{booktabs}
\begin{document}

<<echo = FALSE, results = "asis", message = FALSE>>=
library(knitr)

markdown2tex <- function(markdownstring) {
  writeLines(text = markdownstring,
             con = myfile <- tempfile())
  texfile <- pandoc(input = myfile, format = "latex", ext = "tex")
  cat(readLines(texfile), sep = "\n")
  unlink(c(myfile, texfile))
}

markdowntable <- "
col1 | col2 | col3
-----|:----:|:----:
row1 | cell1 | cell2
row2 | cell3 | cell4
row3 | cell5 | cell6
"

markdown2tex(markdowntable)
@
\end{document}

I wrapped the code in a small helper function markdown2tex. This makes the code quite slim when using it with several markdown tables.

The idea is to simply copy the markdown table in the document and assign it as character string to an object (here: markdowntable). Passing markdowntable to markdown2tex includes the equivalent LaTeX table into the document. Don't forget to use the chunk options results = "asis" and message = FALSE (the latter in order to suppress messages from pandoc).

The workhorse in markdown2tex is knitr::pandoc. With format = "latex", ext = "tex" it converts the input to a TEX fragment and returns the path to the TEX file (texfile). As pandoc needs a filename as input, the markdown string is written to a temporary file myfile. After printing the contents of texfile to the document, myfile and texfile are deleted.

Of course, if the markdown tables are already saved in files, these steps can be simplified. But personally, I like the idea of having a markdown string in the RNW file. That way, it can be easily edited, the content is clear and it supports reproducibility.

Note: You need to add \usepackage{longtable} and \usepackage{booktabs} to the preamble. The TEX code generated by pandoc requires these packages.

The example above produces the following output:

enter image description here

CL.
  • 14,577
  • 5
  • 46
  • 73
  • Thank you @cl! It works like a charm. I also agree that it is really convenient to have the markdown table in the Rnw file itself for the reasons you point out regarding ease of editing and reproducibility. And also thank you for the nice explanation, it is very complete and helpful. – José Manuel Martínez Martínez Feb 24 '16 at 14:07
  • @JoséManuelMartínezMartínez Glad that it helped. I wonder how far one can go with this function ... didn't check yet whether it also works for larger Rmarkdown chunks. – CL. Feb 24 '16 at 14:09