2

I have a data frame in R which unfortunately has strings in it containing dollar signs. When the function latex() from the package Hmisc converts this data frame to a LaTeX table, the dollar signs are not escaped. This renders the LaTeX uncompilable. Is there a way within Hmisc to escape dollar signs when formatting values from a data frame?

I can't run search and replace over all dollar signs afterwards either, because the Hmisc itself adds dollar signs for the empty cells.

Minimal example:

> latex("test$test",file="")
# returns:
\begin{table}[!tbp]
% [...]
test$test\tabularnewline
% [...]
\end{table}

# should return:
\begin{table}[!tbp]
% [...]
test\$test\tabularnewline
% [...]
\end{table}
roelandvanbeek
  • 659
  • 8
  • 20

1 Answers1

5

You can look at the latexTranslate function, from Hmisc :

R> latexTranslate("3%")
[1] "3\\%"
juba
  • 47,631
  • 14
  • 113
  • 118
  • Looks useful, thanks. Can this be integrated with `latex` though? Or only via `sapply` beforehand? – roelandvanbeek Feb 08 '13 at 14:26
  • You can use `latex(latexTranslate("test$test"))` for example. Or maybe I didn't understand your question ? – juba Feb 08 '13 at 14:28
  • Sorry, I meant to apply it to the cells of a data frame, when calling `latex(df)`. But I guess `latex(as.data.frame(sapply(df,latexTranslate)))` will do. – roelandvanbeek Feb 10 '13 at 00:26