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

My chunk is as follows:





And my Rscript in RStudio seems simple:

## @knitr 3_0

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

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

  • 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.
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> 

**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:

  • Do your resumen_3.R contains ## ---- 3_0 ---- label?
    – MYaseen208
    Oct 2 '15 at 19:14

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:

my.df <- data.frame(first_col=c("a","b","c_d"))
print(xtable(my.df, caption="Data frame"))

This also works fine:

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:


this fixes the problem:


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.