8

I have R code that produces latex code like:

\begin{tabular}{D{.}{.}{7}D{.}{.}{7}}
\toprule
\multicolumn{1}{c}{x} & \multicolumn{1}{c}{y} \\
\midrule
[1, 5) &  1 \\
[1, 5) &  2 \\
[5, 8) &  5 \\
[5, 8) &  7 \\
[8,10] &  8 \\
[8,10] & 10 \\
\bottomrule
\end{tabular}

And this is producing math errors:

! LaTeX Error: Bad math environment delimiter.

How do I prevent this from happening? I'd like the square brackets to be printed as-is since they represent intervals - previously in the office a workaround of switching to a { has been done but it looks terrible!

I've read around but not seen anything specific about preventing latex from entering math-mode. However, I'm new to this so I'm probably using the wrong keywords!

Example

I'm producing the code inside knitr chunks with the following parameters:

<<tab-example,echo=FALSE,cache=FALSE,results='asis'>>=
example<-data.frame(cbind(x=1:10,y=1:10))
example$x<-cut(example$x,3)
toLatex(example)
@

Packages currently used

\usepackage{fancyhdr,dcolumn,booktabs,float,titlesec}
\usepackage[margin=0.7in]{geometry}
\usepackage[us,12hr]{datetime}
  • 1
    a square bracket after \` in a tabular` is always assumed to indicate a dimension. if you are instead presenting data, always wrap the bracketed expression in braces to avoid misinterpretation. – barbara beeton Jul 21 '13 at 17:59
  • 1
    Are you able to group (surround by braces {..}) the ranges or insert something before the first [ on every line? LaTeX gets confused, expecting a length if you follow \\ by [. – Werner Jul 21 '13 at 17:59
  • So simply adding in the code for a space? – Steph Locke Jul 21 '13 at 18:01
  • 2
    @StephLocke: Yes, or you could add {} before each element. – Werner Jul 21 '13 at 18:02
  • Thank you both very much! So simple but {} really did the trick. If you'd like to put it as an answer I'll mark it as accepted – Steph Locke Jul 21 '13 at 18:12
5

Your use of (snippet)

%...
[1, 5) &  2 \\
[5, 8) &  5 \\
%...

is interpreted (visually) as

%...
       &  1 \\[1, 5) 
       &  2 \\[5, 8)
       &  5 \\[5, 8)
%...

since LaTeX expects an optional argument after \\ to contain a length/dimension. The easiest work-around is to insert a non-[ after \\, like {} (an empty group). For example:

\begin{tabular}{D{.}{.}{7}D{.}{.}{7}}
  \toprule
  \multicolumn{1}{c}{x} & \multicolumn{1}{c}{y} \\
  \midrule
  {}[1, 5) &  1 \\
  {}[1, 5) &  2 \\
  {}[5, 8) &  5 \\
  {}[5, 8) &  7 \\
  {}[8,10] &  8 \\
  {}[8,10] & 10 \\
  \bottomrule
\end{tabular}

The first {} is not needed, but I've inserted it for a consistent look in the code.

1

As a follow up to @Werner's excellent explanation, as I was using R I created a sanitise function to use instead of the default xtable::sanitize function that escapes these appropriately.

The package optiRum can be downloaded for the sanitise function to be used.

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.