Warm tip: This article is reproduced from stackoverflow.com, please click
latex r

Table does not show correctly in R using TeX

发布于 2020-04-15 09:54:27

I am trying to plot a table in R but it just print the text in a dummy format, not sure how to fix it.

My Code:

tex2=TeX('
     \\begin{table}[]
\\begin{tabular}{|l|l|}
     \\hline
     a&b  \\\\ \\hline
    c & d \\\\ \\hline
     \\end{tabular}
     \\end{table}
     ')
plot(tex2, cex=1)

tex2 is equivalent of

\begin{table}[]
\begin{tabular}{|l|l|}
\hline
 a&b  \\ \hline
 c&d  \\ \hline
 \end{tabular}
 \end{table}

as suggested by https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html

Questioner
mohsen hs
Viewed
54
Stéphane Laurent 2020-03-06 22:28

To view a LaTeX snippet in the RStudio viewer pane, with the possibility to export it as an image, you need the texPreview package.

library(texPreview)

tex <- '
\\begin{table}[]
\\begin{tabular}{|l|l|}
\\hline
a & b  \\\\ \\hline
c & d \\\\ \\hline
\\end{tabular}
\\end{table}
'

tex_preview(tex, imgFormat = "svg")

enter image description here

When I tried it the first time, it didn't work. I got this error:

Error in magick_image_readpath(enc2native(path), density, depth, strip) : rsession: not authorized `/tmp/RtmpKZqeBQ/tex_tempDoc.pdf' @ error/constitute.c/ReadImage/412

I'm using Ubuntu. I had to make a change in the file /etc/ImageMagick-6/policy.xml; namely I replaced the line

<policy domain="coder" rights="none" pattern="PDF" />

with

<policy domain="coder" rights="read | write" pattern="PDF" />