azdatasci azdatasci -4 years ago 404
LaTeX Question

Generate Latex code from RMarkdown Code Chunk

I have a small header I want to print at the top of a report. It should look like this in the end:

enter image description here

The report is generated by an RMarkdown/Knitr report. I am mixing in Latex to get the formatting I need. The data is based on a table I generate by pulling results from a REST Query. The dataframe in R looks like:

enter image description here

I coded up the LaTex code to get the result I wanted and that appears as such:

ECC \begin{tabular}{|C|C|} \hline \cellcolor{red} \textbf{\scriptsize \textcolor{white}{TOUCH}} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
DOC \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
CCC \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
GEN \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}
IT \begin{tabular}{|C|C|} \hline \textbf{\scriptsize TOUCH} & \textbf{\scriptsize NO TOUCH} \\ \hline \end{tabular} \hspace{.9em}

So far, so good. But the coloring of the text and the cell colors need to be dynamic. The criteria are as follows:

  1. If the environment is a "Touch" environment, that cell should be colored green and the text white.

  2. If the environment is a "No Touch" environment, that cell should be colored red and the text white.

  3. Cells not colored in each section should have a white background and black text.

Not so hard, I created a code chunk in the report that is wrapped inside of the
items, that looks like this:

```{r echo = FALSE, warning = FALSE, message=FALSE, results='asis'}
# Add row to data frame to build table options
touchNoTouchDF$tableOption <- NA

for(i in 1:nrow(touchNoTouchDF)) {
if(strsplit(touchNoTouchDF[i,1], ":")[[1]][2] == "Touch") {
touchNoTouchDF[i,2] <- paste(strsplit(touchNoTouchDF[i,1], ":")[[1]][1],"\\begin{tabular}{|C|C|} \\hline \\cellcolor{OliveGreen!85} \\textbf{\\scriptsize \\textcolor{white}{TOUCH}} \& \\textbf{\\scriptsize NO TOUCH} \\newline \\hline \\end{tabular} \\hspace{.9em}")
} else {
touchNoTouchDF[i,2] <- paste(strsplit(touchNoTouchDF[i,1], ":")[[1]][1],"\\begin{tabular}{|C|C|} \\hline \\textbf{\\scriptsize TOUCH} \& \\cellcolor{red} \\textbf{\\scriptsize \\textcolor{white}{NO TOUCH}} \\newline \\hline \\end{tabular} \\hspace{.9em}")

for(i in 1: nrow(touchNoTouchDF)) {

What I did here, was make a look to generate the correct LaTex code in a different column of that data frame based on what the first column contains. After running that for loop I get the data frame you see above. So far, excellent. Now is where my problem comes in. I want to have that R code chunk print that LaTex so it is rendered. I figured I simple for loop with a print statement would work, no dice. I tried cat(), that didn't work either.

I'm perplexed as to how I can get the contents of that last row printed out so the pdf report that is generated contains that graphical representation. Any ideas? Tips? I'm sure this is simple, but I have been at this for couple hours and Google'd it various ways... Thanks in advance!


As requested, below is the output of what I am getting. I made sure to use cat(), which, if I run this manually, prints the LaTex out to the console with no issues. When I attempt to knit the document, I get the following:

! Misplaced \noalign.
\hline ->\noalign
{\ifnum 0=`}\fi \let \hskip \vskip \let \vrule \hrule \let...
l.143 ...tbf{\scriptsize NO TOUCH} \newline \hline

pandoc.exe: Error producing PDF
Error: pandoc document conversion failed with error 43
In addition: Warning messages:
1: package 'knitr' was built under R version 3.3.2
2: package 'prettyR' was built under R version 3.3.2
3: package 'magick' was built under R version 3.3.2
4: running command '"C:/Users/z10987/AppData/Local/Pandoc/pandoc" +RTS -K512m -RTS --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output pdfReport.pdf --template "C:\R\R-3.3.1\library\rmarkdown\rmd\latex\default-" --highlight-style tango --latex-engine xelatex --include-in-header header1.tex --variable graphics=yes' had status 43
Execution halted

I thought, maybe, it was because I did not escape the "&" in the table, but when I tried that I got:

Error: \& is an unrecognized escape character in string...

Thanks again!


I figured it out - in the table, originally, at the end of each line there was a
. I attempted to escape this by
, which gave me an error. I decided that since I thought
was a newline, I'd just replace it with
. I guess you have to have
at the end of a table, or it doesn't like it.

So... I put the
back in. Now, but I still had the issue with escaping it. Just on a whim, I figured, well, "if one \ escapes the other \, maybe I need two, one to escape each backslash..." I replaced the
and voila! It worked. Ugh, such simple things.... I wanted to post the solution for this.

Answer Source

All - to post an official answer, my mistake was thinking I could escape "\" with a single "\", thus, I was pasting into my string "\\". However, since each "\" must be escaped. I had to escape the first "\" and then the second "\", therefore ending up with "\\". The first escapes the second, and the third escapes the fourth. Probably something basic, but is an easy oversight if you aren't paying attention. Just wanted to post the answer for others.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download