Function to sanitize strings for LaTeX compilation? - r

While xtable() has a sanitize.text.function argument which allows to sanitize strings with special charaters to stop LaTeX compilation from breaking in Sweave/knitr documents, the package does not export the function to the userspace.
How can I sanitize strings like asdf_text outside of the xtable context, so as to have it transformed to something like asdf\_text? (If possible I would prefer a small, self-contained solution.)

Unless I misunderstand your question, I think you've overlooked latexTranslate, which is also in the Hmisc package (and documented on the same help page as ?latex):
‘latexTranslate’ translates particular items in character strings
to LaTeX format, e.g., makes ‘a^2 = a\$^2\$’ for superscript
within variable labels. LaTeX names of greek letters (e.g.,
‘"alpha"’) will have backslashes added if ‘greek==TRUE’. Math
mode is inserted as needed. ‘latexTranslate’ assumes that input
text always has matches, e.g. ‘[) [] (] ()’, and that surrounding
by ‘\$\$’ is OK.
library("Hmisc")
latexTranslate("asdf_text")
## [1] "asdf\\_text"
latexTranslate("a^2")
## [1] "a$^{2}$"

Thus far I found package reportRx which provides sanitizestr():
Sanitizes strings to not break LaTeX
Strings with special charaters will break LaTeX if returned 'asis' by knitr. This happens every time we use one of the main reportRx functions. We first sanitize our strings with this function to stop LaTeX from breaking.
require(reportRx)
sanitizestr("asdf_text")
## [1] "asdf\\_text"
My gripe however is that it comes with quite a number of dependencies...
Another solution is tikzDevice which provides sanitizeTexString(), and has many fewer mandatory dependencies:
Replace LaTeX Special Characters in a String
This function is used by tikzDevice when sanitize=TRUE to replace special LaTeX characters [..]
require(tikzDevice)
sanitizeTexString("asdf_text")
## [1] "asdf{\\_{}}text"

Related

How to get roxygen2 to interpret backticks as code formatting?

The standard way of writing documentation with code formatting is by using \code{}.
That is, #' #param foo value passed on to \code{bar()} becomes
Arguments
foo    value passed on to bar()
However, I've seen some packages (i.e. dplyr) using backticks instead of \code{} to the same effect. This is much better, since it's less clunky and allows for very nice syntax highlighting.
However, if I try that on my own package, the backticks get interpreted as... just backticks, like any other character.
The documentation for dplyr::across(), for example, starts with:
#' #description
#' `across()` makes it easy to apply the same transformation to multiple [...]
which gets compiled and displayed in the man page as:
Description
across() makes it easy to apply the same transformation to multiple [...]
But if I try something similar on my package, I get:
Description
`across()` makes it easy to apply the same transformation to multiple [...]
Weirdly, I've forked the package glue (which also manages to use backticks for code formatting) for a simple PR, and if I build the package locally, the backticks work (I get code formatting). Can't for the life of me figure out why it works there but not for my package.
So, is there some setting I need to modify to get this to work? I checked the dplyr.Rproj but found nothing relevant. I also glanced at the Doxyfile, but didn't know what it did or what I'd even be looking for there.
All credit goes to #rawr's comment to the question, just formalizing it with an answer:
The secret is in the roxygen2 documentation: just add the following to the end of the package DESCRIPTION file:
# DESCRIPTION file
# [... rest of file ...]
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2 # actually works since 6.0.0
As that code would imply, this sets roxygen2 to interpret the commands as good ol' Markdown like we're used to using here on SO and elsewhere. This also implies all the other standard Markdown commands such as **bold** and *italics*, as well as [text](http://www.url.com), code blocks defined by ```, itemized and enumerated lists, etc. It's a huge improvement across the board.
Though be careful and take a look at the documentation, since there are a few gotchas. For instance, an empty line isn't necessary to start lists, so don't start any lines with #' 1. [...] or #' * [...] or you'll accidentally create a list!). There's also a few things which don't work yet, but they're pretty minor.

Knitr - Define Latex commands that call R functions using Sexpr

I'm basically wondering how to define a new Latex command such that it allows the nesting of Sexpr and some other R function, where the Latex argument is an R object.
As fortunute happenstance, the idea somewhat is transmitted by the new command structure given below:
\newcommand{\SomeLatexCommand}[1]{\Sexpr{"#1"}}
Where fortunately the argument is indeed shown, albeit in string. With this in mind, I was hoping upon the following command:
\newcommand{\SweetLatexCommand}[1]{\Sexpr{SomeRFunction(get("#1"))}}
However, once inside nested inside an R function, #1 is not read as a placeholder for the Latex argument, but instead as an existing R variable.
Is there a way to make the last comand work? Or else, are there also other neat ways to define Latex commands which in turn can call on any R function through R objects?
Good day,
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.

knitr - change code indentation

I am working on a two-column (Rnw, latex) document where the width is at a premium. By default knitr indents the code blocks by 4 spaces. How can I reduce this default indentation?
Many thanks
David
Either do not reformat the code (use the chunk option tidy=FALSE) and manually indent by two spaces,
<<tidy=FALSE>>=
if (TRUE) {
# code here
}
#
or set the R option reindent.spaces to a smaller value, e.g.
options(reindent.spaces = 2)
This option is passed to the formatR package to reindent your code, and knitr uses formatR to reformat your R code by default.

Using Sexpr{} within \SweaveInput{}

I have this 2 lines of code which I run with R Sweave function.
\SweaveInput{samples.rnw}
\SweaveInput{\Sexpr{args$samples}}
The first line leads to the inclusion of the content of the corresponding file, while the second just causes evaluation of the Sexpr{} term but nothing else.
What I want is both: first let evaluate the Sexpr{} term and afterwards do the inclusion of the respective file content.
How do solve this ?
Thanks
If you use the knitr package, the solution would be simply
<<child='samples.rnw'>>=
<<child=args$samples>>=
#
Sweave is much weaker than knitr in terms of programmability. For example, knitr allows the chunk options to be any valid R expressions, which is the reason why we can write child=args$samples here; knitr will evaluate the chunk options just like function arguments.
BTW, the child option is equivalent to \SweaveInput{}, but I strongly discourage the use of the pseudo LaTeX command. For more about Sweave vs knitr, see http://yihui.name/knitr/demo/sweave/

How to convert R expressions to LaTeX/TeX without using a CAS?

I would like to be able to convert R expressions (ex. 1/2*x^2) or formulas into a character string that contains the LaTeX (ex. "\frac{1}{2} \times x^{2}" - or something similar). I know that expression() can be used for something similar, generating mathematical notation in figures, but I'm not sure how to convert a similar type of expression into LaTeX. I want to be able to include this into a Sweave document.
Please give an example (like with the 1/2*x^2), instead of just saying "use the tikzDevice package". It sounds like the Hmisc package might be able to do what I want, but I haven't figured out how yet.
I'm wanting to avoid using a CAS (computer algebra system), because I want it not have to rely on a complex external application, like that Ryacas uses, and want it to be able to run quickly.
latexTranslate provides valid LaTeX output from string input but only partial expansion. The translation of x/y will not be converted to the prefix version, \frac {1} {2}.
require(Hmisc)
latexTranslate("1/2*x^2")
#[1] "1/2*x$^{2}$"

Resources