\Sexpr{} Special Latex Characters ($, &, %, # etc.) in .Rnw-File

\Sexpr{} special LaTeX characters ($, &, %, # etc.) in .Rnw-file

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.

knitr + LaTeX (Rnw file) compiles in TeXShop but fails with UTF-8 error in RStudio

The problem seems to be the × in the intermediate .tex file.

You can avoid the problem by choosing an unicode aware engine, like lualatex or xelatex, to compile your document:

Sample Image

Knitr - Define Latex commands that call R functions using Sexpr

No, you can't do that. The problem is the way knitr works:

R runs the knit() function (or some other knitr function). That function looks through the source for code chunks and \Sexpr calls, executes them, and replaces them with the requested output, producing a .tex file.

Then LaTeX processes that .tex file. R is no longer involved.

Since \newcommand is a LaTeX command, it is only handled in the final stage, after all R evaluation is done.

There may be a way in knitr to specify another "macro" that works the way \Sexpr works, but I don't think there's a way to have several of them.

So what you should do is write multiple functions in R, and call those to do what you want, as \Sexpr{fn1(...)}, \Sexpr{fn2(...)}, etc.

I suppose if you were really determined, you could add an extra preprocessor stage at the beginning, that went through your Rnw file and replaced all strings that looked like \SweetLatexCommand{blah} with \Sexpr{SomeRFunction(get("blah"))} and then called knit(), but that seems like way too much work.

R, Sweave, LaTeX - escape variables to be printed in LaTeX?

You can either use \verb or you can insert a quadruple backslash, which is needed to get one in the final tex file because it goes through two printing operations which convert a double "\" to a single "\".

\documentclass{article}
\begin{document}
<<echo=FALSE, results=hide>>=
sanitize <- function(str) {
result <- str
result <- gsub("&", "\\\\&", result, fixed = TRUE)
result <- gsub("_", "\\\\_", result, fixed = TRUE)
result
}
@

<<>>=
(foo <- "test & _")
sanitize(foo)
@

My string is ``\verb+\Sexpr{foo}+''. When sanitized, it's ``\Sexpr{sanitize(foo)}''.

\end{document}

Coming together with ``\Sexpr{}'' from knitr package, Chinese character garbled

Copyed from another question

Type

system("defaults write org.R-project.R force.LANG en_US.UTF-8")

and press Enter

Restart R

How to avoid using round() in every \Sexpr{}?

If you have read the motivation of the knitr package, you probably know I'm the person who hates round() inside \Sexpr{} most. I suggested this to R core long time ago for Sweave but it was ignored, so I started my own package.

Although the answer by Josh O'Brien is absolutely correct, I should say you really only need options(digits = 2) with knitr, and there is no need to reset the default inline hook because the default hook respects this option.

R - Handling cut intervals of form [x,y] in tables when converting to LaTeX

Courtesy of folks on the tex SE, a [ directly after a line break(\\) is considered an entry into math-mode. It is very simple to prevent this behaviour by adding {} into the output just before a [. My function looks like:

escapedLatex<-function (df = NULL) 
{
require("memisc")
gsub(gsub(x = toLatex(df, show.xvar = TRUE), pattern = "%",
replacement = "\\%", fixed = TRUE), pattern = "[", replacement = "{}[",
fixed = TRUE)
}

I'd be very happy to see any alternative, more elegant solutions around and will leave it open for a few days.

\sexpr returning wrong value of r object

I decided to clear the cache and remove all objects (including hidden objects) from the workspace once again. But this time I also deleted several hidden files that R creates (history and data). When I ran the code chunks again and compiled the document, everything worked. I believe the only new step was deleting the hidden documents.



Related Topics



Leave a reply



Submit