3

It has something to do with the default inline hook, I realize that and I have tried getting at it (the hook) and also read this thread and Yihui's page about hooks, but I haven't been able to solve my issue. I even tried this suggestion from Sacha Epskamp, but it didn't do this trick in my case.

I'm using \Sexpr and doing something along the lines of \Sexpr{load("meta.data.saved"); meta.data[1,7]} to print a keyword in my report, the problem is that people writing these keywords (people I can't control) are using special LaTeX characters ($, &, %, # etc.) and when they are passed to my .tex file without an \ I'm having a bad time.

I have an .Rnw file with this code,

\documentclass{article}
\begin{document}
 Look \Sexpr{foo <- "me&you"; foo} at this.
\end{document}

Thsi creates an .tex file with an illegal LaTeX character. Like this,

<!-- Preamble omitted for this example. -->
\begin{document}
 Look me&you at this.
\end{document}

I'm interested to get an output that looks like this,

<!-- Preamble omitted for this example. -->
\begin{document}
 Look me\&you at this.
\end{document}

Sorry for the simple question, but can someone help me, and maybe others, getting starting on how to modify the default inline hook for \Sexpr?

Community
  • 1
  • 1
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • why do you fail to give us a reproducible example? I really can't see why do you want hook \Sexpr? – agstudy Dec 04 '12 at 00:20
  • @agstudy, tahnk you for your comment and sorry for not providing a reproducible example. If I have an R object that is `(foo <- "me&you")` and pass that you `\Sexpr{foo}` the .tex-file ends up with a & without an `\` and it should have been `\&`. I'll try to make a reproducible example. – Eric Fail Dec 04 '12 at 00:28
  • And edit your question please , better than putting it in the comment. – agstudy Dec 04 '12 at 00:30
  • I'm surprised that Sacha's verbatim idea didn't work. That seems like the right thing to do here, rather than adding backslashes before all the special characters. – Aaron left Stack Overflow Dec 04 '12 at 03:13
  • @Aaron yes it is amazing but \verb command doesn't work in this case. – agstudy Dec 04 '12 at 05:37
  • @agstudy @Aaron I'm also very surprised `\verb|\Sexpr{'me&you'}|` did not work for you, but it works for me with TeXLive 2012 under Ubuntu and also with MikTeX 2.9 under Windows. Perhaps it is a LaTeX problem? Update your LaTeX packages first? – Yihui Xie Dec 04 '12 at 16:15
  • @Yihui,@Aaron +agstudy Thank you for all your help and feedback. It's most likely my LaTeX installation. I'm on a TeX Live 2009/Debian. – Eric Fail Dec 04 '12 at 17:51

2 Answers2

7

The solution provided by @agstudy has shown the basic idea, and here is a more robust version:

hook_inline = knit_hooks$get('inline')
knit_hooks$set(inline = function(x) {
  if (is.character(x)) x = knitr:::escape_latex(x)
  hook_inline(x)
})

It only modifies the default inline hook when the inline result is character (otherwise just use the default hook). I have an internal function escape_latex() which hopefully escapes all special LaTeX characters correctly.

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • This worked when I knit with the RStudio button. When I use a driver rscript to iterate over different data sets, all my figures don't display. Instead I see the their latex label name. I'm using to knit2pdf in the driver script. How would we have the same functionality but running knit2pdf? – Sahir Moosvi Oct 29 '18 at 22:39
  • This allows me to use pipes inline which is awesome. But I get a message saying Error: unexpected input in "\" for every expression with these escaped characters. Is there a way to handle that? – Sahir Moosvi Nov 29 '18 at 17:46
3

Hooking works in this case. I customize it like this :

inline_hook <- function(x) {
  x <- gsub("\\&", "\\\\&", x)
  x <- gsub("\\$", "\\\\$", x)
  ## good luck for all Latex special character  
  ## $ % _  {  }  &  ~   ^  <  >   |  \ 
}
knit_hooks$set(inline = inline_hook)                    

Then

 knit(input='report.Rnw')      

Will reproduce your report.tex.

PS: I think it is better to not to allow users do what they want.

agstudy
  • 119,832
  • 17
  • 199
  • 261