I need to use variable (ie. an argument of a custom command) from latex into a R inline code chunk. See, for instance :
\newcommand{\textvar}[1]{
\textbf{
\Sexpr{
# Here, I would like to use the one argument of my \textvar command to do somthing in R
}
}
}
Of course, I can't just add #1 like in pure Tex, it would just comment the line in R.
You can't do that because of the order in which things are run. First Sweave()
processes the .Rnw
file to produce a .tex
file, then LaTeX processes the .tex
file to produce output. Sweave (and hence R) knows nothing about LaTeX macro processing.
What you could do is write an R function that outputs the LaTeX you want, e.g. something like
textvar <- function(x) {
paste0("\\textbf{", x, "}")
}
and then insert this as an inline code chunk
\Sexpr{textvar(x)}
which will expand to the LaTeX code you want your \textvar
macro to insert.