[Tex/LaTex] “Missing $ inserted” error; when results=’asis’

errorslyxxtable

I have a problem in LyX when I want to export a PDF file using knitr.

My chunk is as follows:

<<echo=FALSE>>=
read_chunk('resumen_3.R')

@

<<3_0,echo=FALSE,results='asis'>>=

@

And my Rscript in RStudio seems simple:

## @knitr 3_0
library(knitr)
library(xtable)
library(data.table)
library(RMysSQL)

c<-subset(uno, grepl("^[^9]", uno$idcriadorviejo), select=c(origen,nomcriador))

tabla_4<-data.table(c)
tabla_5<-tabla_4[,.(count= .N), by=idcriadorviejo]

print(xtable(tabla_5[order(count,decreasing=TRUE),]),floating=FALSE)
  • The error is Missing $ inserted.

Part of the latex code where I found the error,is as follows:

Package color Info: Redefining color shadecolor on input line 79.
LaTeX Font Info:    Try loading font information for T1+cmtt on input line 82.
(/usr/share/texlive/texmf-dist/tex/latex/base/t1cmtt.fd
File: t1cmtt.fd 1999/05/25 v2.5h Standard LaTeX font definitions
)
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 88.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 88.
! **Missing $ inserted.**
<inserted text> 
                $
l.301 

**I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.**


Overfull \hbox (163.16438pt too wide) in paragraph at lines 141--301
\T1/cmr/m/n/10 "HOLA" "HOLA" "HOLA" "HOLA" ... $\OML/cmm/m/it/10 bu
[]ria \OT1/cmr/m/n/10 : \OML/cmm/m/it/10 chr\OT1/cmr/m/n/10 "0136064""0094401""
0175242""0178927"\OML/cmm/m/it/10 :::$
 []

However, when I set results=**'hide'**, I can export to pdf and the error disappears.

Can you help me? Thanks!

PD: To avoid problems with underscore I set in LaTeX Preamble:

\usepackage[T1]{fontenc}
\usepackage{underscore}

Best Answer

As @MYaseen208 suggested, the problem seems to be with the underscores in xtable output. Even though it automatically escapes them with \_ it is still causing syntax errors in pdfLatex. The fact that you are using data.table also appears to be significant.

I encountered the same problem when trying to format the output of tables() using xtable, when one of the column names contained an underscore.

The following works fine, no need to have \usepackage{underscore} in the preamble:

<<echo=FALSE,results='asis'>>=
library(xtable)
my.df <- data.frame(first_col=c("a","b","c_d"))
print(xtable(my.df, caption="Data frame"))
@

This also works fine:

<<echo=FALSE,results='asis'>>=
library(data.table)
my.tbl <- data.table(my.df)
print(xtable(my.tbl[order(first_col,decreasing=TRUE),], caption="Data table"))
@

but this will cause the error "Missing $ inserted." from pdfLatex:

<<echo=FALSE,results='asis'>>=
print(xtable(tables()))
@

this fixes the problem:

<<echo=FALSE,results='asis'>>=
print(xtable(tables(silent=TRUE)))
@

Unfortunately, you haven't provided enough information about the contents of the table uno (or the other code in resumen_3.R) to be certain whether this would fix your original problem also.

Related Question