R tikzDevice:找不到LaTeX

时间:2018-06-25 12:25:01

标签: r latex tikzdevice

我无法运行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或此处找到有关该问题的讨论,因此,我希望获得一些帮助。

2 个答案:

答案 0 :(得分:2)

您的LaTex文件未在您的媒体库中设置。就我而言,它是pdflatex文件。您可以添加pdftex,xetex或luatex文件。

简单尝试:

如果可能的话,尝试重新安装您的库或乳胶,这应该是一种干净的安装方式。

手动设置变量

在Linux上:

使用

getOption("tikzLatex")

我生成输出

"/usr/bin/pdflatex"

这是我通往乳胶文件的路径,而这正是您所缺少的。所以我们需要添加它。

您可以使用终端中的命令检查您的Latex文件在哪里:

whereis pdflatex

因此,如果您想出了文件的路径,请使用以下命令进行设置:

options("tikzLatex"='/usr/bin/pdflatex')

Windows

我不是Windows用户,所以我只能猜测是相同的方式。 Windows上用于查找文件的类似命令是where命令where command on windows

设置变量应相同。如果有人可以确认这一点,那就太好了。

编辑:Mason Malone在问题的评论中提供了Windows解决方案

答案 1 :(得分:0)

这可能为时已晚,但是这是一个更永久且系统范围内的解决方案。

这是针对Linux的,但总体思路也应适用于Windows。

该错误的原因是R无法找到pdflatex在哪里。通过将pdflatex的目录添加到PATH环境变量中,可以告诉R pdflatex在哪里。并且您必须以一种R可以看到它的方式进行操作。

首先,找到pdflatex的安装目录。如果您不知道它,但是如果您可以编译乳胶文件,则遵循以下命令的通常方法将告诉您它的位置。

which pdflatex

就我而言,以上命令给出了/usr/local/texlive/2018/bin/x86_64-linux/pdflatex。这意味着pdflatex和其他乳胶二进制文件位于 / usr / local / texlive / 2018 / bin / x86_64-linux 目录中。

现在,我们必须将其添加到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,以更新其属性。