R tikzDevice: Cannot find LaTeX

2.4k Views Asked by At

I can't get the R package tikzDevice to running. I installed MiKTex and I don't have problems to produce documents like this one via TeXworks.

Exporting plots via tikzDevice unfortunately doesn't work, for example the following code from here produces an error message:

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()

The following error message is produced:

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)

I couldn't find a discussion about that problem on Google or here so I would appreciate some help.

2

There are 2 best solutions below

1
On

Your LaTex file is not set in your library. In my case it's the pdflatex file. You can add an pdftex, xetex or luatex file.

easy attempt:

Try to reinstall your library or your latex if possible, that should be an easy way to clean install it.

Set your Variable manually

On Linux:

With

getOption("tikzLatex")

i generate the output

"/usr/bin/pdflatex"

so this is my path to the latex file and this is what's missing in your case. So we need to add it.

You can check where your Latex file is with the command in the Terminal:

whereis pdflatex

so if you figured out the path to your file you set it with:

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

Windows

i'm not a windows user, so i just can guess it's the same way. The similar command on Windows for finding your file is the where command where command on windows

Setting your Variable should be the same. Would be nice if someone could confirm this.

Edit: a windows solution is provided by Mason Malone in the commentary of the question

"For Windows User's it does work the same way for Windows users. Open the Windows command prompt (Start > type "Command Prompt" > enter).

Type the following: where pdflatex Copy the file path that it gives you, e.g.: C:\Users\user1\AppData\Local\Programs\MiKTeX 2.9\miktex\bin\x64\pdflatex.exe

In R, type the following: options("tikzLatex"='C:/Users/user1/AppData/Local/Programs/MiKTeX 2.9/miktex/bin/x64/pdflatex.exe')

Note that for the file path we are given via the command prompt, it has backslashes , but in R we have to type forward slashes /. "

0
On

This is probably too late, but here is a more permanent and a system wide solution.

This is for Linux, but general idea should work for Windows as well.

The reason for the error is R not being able to find where pdflatex is. You can tell R where pdflatex is by adding it's directory to PATH environment variable. and you have to do it in a way that R can see it.

First, locate the installation directory of pdflatex. If you don't know where it is, the following command will tell you where it is.

which pdflatex

In my case, above command gives /usr/local/texlive/2018/bin/x86_64-linux/pdflatex.

Now we have to add that to PATH environment variable so that any program (not just R) that wants to execute pdflatex can find it. We can do this by updating PATH for all programs/users to see. Execute following command to that.

echo "export PATH=\"\$PATH:<pdflatex directory>\"" | sudo tee /etc/profile.d/latex_path.sh

In my case I had to execute:

echo "export PATH=\"\$PATH:/usr/local/texlive/2018/bin/x86_64-linux\"" | sudo tee /etc/profile.d/latex_path.sh

To make the changes available without needing to log out and log in again, execute:

source /etc/profile.d/latex_path.sh

Now any program or user can use pdflatex command.

You may have to reinstall tikzDevice within R, to update its properties.