R-从 tikzDevice 的最佳包中转义字符

R-从 tikzDevice 的最佳包中转义字符

我尝试R BEST在软件包的帮助下修改软件包中的一些绘图输出tikzDevice。但是,tikzDevice由于带有百分号(“%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 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

tikzTest("% HDI")我还可以发布相当长的输出。

当百分号直接来自其他包时,我该如何正确转义它?或者我是否可以修改“% HDI”,这样一开始就不会出现百分号?

我不确定这个问题适合哪里(这里或在 stackoverflow 上)。

谢谢。

答案1

绘制 BESTout 生成的图形包括,% HDI因此对的调用tikzDevice::tikz必须包括sanitise=TRUE以转义 %,即它变成{{\%} HDI}BESTout.tex这是输出:

在此处输入图片描述

MWE 位于Rmarkdown

---
output:
  pdf_document: default
header-includes: \usepackage{tikz}
---
```{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}

离题了,但tikzDevice将 μ 1 - μ 2改为m 1 - m 2。我不知道如何解决这个问题。

相关内容