我无法运行R包tikzDevice。我安装了MiKTex,通过TeXworks生成像this one这样的文档没有问题。
遗憾的是,通过tikzDevice导出绘图不起作用,例如,以下代码from here会产生一条错误消息:
library(tikzDevice)
library(ggplot2)
#For some reason, Rstudio needs to know the time zone...
options(tz="CA")
#Dummy data for the plot
y <- exp(seq(1,10,.1))
x <- 1:length(y)
data <- data.frame(x = x, y = y)
#Create a .tex file that will contain your plot as vectors
#You need to set the size of your plot here, if you do it in LaTeX,
#font consistency with the rest of the document will be lost
tikz(file = "plot_test.tex", width = 5, height = 5)
#Simple plot of the dummy data using LaTeX elements
plot <- ggplot(data, aes(x = x, y = y)) +
geom_line() +
#Space does not appear after Latex
ggtitle( paste("Fancy \\LaTeX ", "\\hspace{0.01cm} title")) +
labs( x = "$x$ = Time", y = "$\\Phi$ = Innovation output") +
theme_bw()
#This line is only necessary if you want to preview the plot right after compiling
print(plot)
#Necessary to close or the tikxDevice .tex file will not be written
dev.off()将生成以下错误消息:
Measuring dimensions of: \char77
Error in get_latex_cmd(TeXMetrics$engine) :
Cannot find LaTeX! Please check your system configuration or manually provide a value for options(tikzLatex)我在Google上或这里找不到关于这个问题的讨论,所以我希望能得到一些帮助。
发布于 2018-06-25 20:49:35
您的库中未设置LaTex文件。在我的例子中,它是pdflatex文件。您可以添加pdftex、xetex或luatex文件。
轻松尝试:
如果可能的话,尝试重新安装库或latex,这应该是一种简单的干净安装方法。
手动设置您的变量
Linux上的:
使用
getOption("tikzLatex")我生成输出
"/usr/bin/pdflatex"这是我的latex文件的路径,这是您的例子中缺少的内容。所以我们需要添加它。
您可以在终端中使用以下命令检查Latex文件的位置:
whereis pdflatex因此,如果您找出了文件的路径,则可以使用以下命令进行设置:
options("tikzLatex"='/usr/bin/pdflatex')Windows
我不是windows用户,所以我猜也是一样的。在Windows上查找文件的类似命令是where命令where command on windows
变量的设置应该是相同的。如果有人能证实这一点就太好了。
编辑: Mason Malone在问题的评论中提供了一个windows解决方案。
打开Windows命令提示符(开始>键入"Command Prompt“> enter)。
键入以下内容:where pdflatex复制它提供的文件路径,例如:C:\Users\user1\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\pdflatex.exe
在R中,键入以下内容:options("tikzLatex"='C:/Users/user1/AppData/Local/Programs/MiKTeX 2.9/miktex/bin/x64/pdflatex.exe')
请注意,对于通过命令提示符给出的文件路径,它有反斜杠, but in R we have to type forward slashes /。“
发布于 2019-05-07 11:22:38
这可能太晚了,但这里有一个更持久的系统范围的解决方案。
这是针对Linux的,但是一般的想法也应该适用于Windows。
错误的原因是R找不到pdflatex的位置。您可以通过将它的目录添加到PATH环境变量来告诉R它的位置。你必须以一种R可以看到的方式来做。
首先,找到pdflatex的安装目录。如果您不知道它在哪里,下面的命令将告诉您它在哪里。
which pdflatex在我的例子中,上面的命令提供了/usr/local/texlive/2018/bin/x86_64-linux/pdflatex。
现在,我们必须将它添加到PATH环境变量中,以便任何想要执行pdflatex的程序(不仅仅是R)都可以找到它。我们可以通过更新PATH,让所有程序/用户都能看到。对此执行以下命令。
echo "export PATH=\"\$PATH:<pdflatex directory>\"" | sudo tee /etc/profile.d/latex_path.sh在我的例子中,我必须执行:
echo "export PATH=\"\$PATH:/usr/local/texlive/2018/bin/x86_64-linux\"" | sudo tee /etc/profile.d/latex_path.sh要使更改可用而无需注销并重新登录,请执行:
source /etc/profile.d/latex_path.sh现在,任何程序或用户都可以使用pdflatex命令。
您可能需要在R中重新安装tikzDevice,以更新其属性。
https://stackoverflow.com/questions/51023447
复制相似问题