# R - Escaping a character from the BEST package for tikzDevice

I try to modify some plot output from the R BEST package with the help of the package tikzDevice. However, apparently tikzDevice can not proceed due to a parameter with a percent sign ("% HDI").

library(BEST)
library(tikzDevice)

y1 <- c(5.77, 5.33, 4.59, 4.33, 3.66, 4.48)
y2 <- c(3.88, 3.55, 3.29, 2.59, 2.33, 3.59)

BESTout <- BESTmcmc(y1, y2, priors=NULL, parallel=FALSE)

tikz('Bestout.tex', width=3.5, height=3.5)
plot(BESTout)
dev.off



Error message:

Error in getMetricsFromLatex(TeXMetrics, verbose = verbose) :
TeX was unable to calculate metrics for:

% HDI

Run the following commands for diagnosis:

tikzTest()
tikzTest("% HDI")

Common reasons for failure include:
* The string contains a character which is special to LaTeX unless
escaped properly, such as % or \$.
* The string makes use of LaTeX commands provided by a package and
the tikzDevice was not told to load the package.

The TeX and log files used for the calculation can help diagnose the
problem. If these files are missing, rerun the plot and make sure to
keep the R session open.
TeX file: tikzStringWidthCalc.tex
Log file: tikzStringWidthCalc.log
Calls: <Anonymous> ... text -> text.default -> <Anonymous> -> getMetricsFromLatex

Execution halted

tikzTest()

Active compiler:
/usr/bin/pdflatex
pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian)
kpathsea version 6.3.1

Measuring dimensions of: A
Running command: '/usr/bin/pdflatex' -interaction=batchmode -halt-on-error -output-directory '/tmp/RtmpX75QBi/tikzDevice152e46ef599eb' 'tikzStringWidthCalc.tex'
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019/Debian) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
[1] 7.49817



I could also post the output of tikzTest("% HDI") which is rather long.

How can I escape the percent sign properly when it comes directly from the other package? Or is it possible that I could modify "% HDI" so there will not be a percent sign in the first place?

I am not sure where this question fits (here or on stackoverflow).

Thank you.

• Try tikz('Bestout.tex', sanitise=TRUE,...)
– Ross
Apr 2 at 18:15
• Thank you so much! Would you like to create an answer with sanitize=TRUE, so I could mark your answer? Apr 2 at 18:19
• @Ross Feel like writing up an answer?17757970 wait a few more days and if not make the answer yourself so this will be out of the unanswered queue. Apr 15 at 15:03
• @scottkosty Done.
– Ross
Apr 15 at 15:37

The graph produced by plotting BESTout includes % HDI so the call to tikzDevice::tikz must include sanitise=TRUE to escape the % i.e. it becomes {{\%} HDI} in BESTout.tex. This is the output:

The MWE in Rmarkdown:

---
output:
pdf_document: default
---
{r}
library(BEST)
library(tikzDevice)

y1 <- c(5.77, 5.33, 4.59, 4.33, 3.66, 4.48)
y2 <- c(3.88, 3.55, 3.29, 2.59, 2.33, 3.59)

BESTout <- BESTmcmc(y1, y2, priors=NULL, parallel=FALSE)
BESTout

tikzDevice::tikz('Bestout.tex',sanitize = TRUE, width=3.5, height=3.5)
plot(BESTout)
dev.off()

\begin{figure}[!h]
\centering
\input{Bestout.tex}
\caption{Simple Example}
\end{figure}


Off topic, but tikzDevice changes μ1 - μ2 to m1 - m2. I could not work out how to fix that.