daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.

Home Page:https://daqana.github.io/tikzDevice

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tikz plot fails when using includegraphics{} with local file

bbolker opened this issue · comments

3eb8ad8 made tikz do its processing in a temporary directory. Unfortunately, this breaks my workflow, which used \includegraphics{} to include a local file. I'm trying to think of a workaround (I don't want to give an absolute path name ...) (Proximal problem is in getMetricsFromLatex: maybe you would want to consider expurgating this comment?)

To run this example, save the attached PNG as crown.png in the working directory ...

library(tikzDevice)
tikz("crown.tex")
plot(0:1,0:1,type="n")
text(0.5,0.5,"\\includegraphics[width=15pt]{crown.png}")
dev.off()

crown

This works in tikzDevice 0.12, but not in 0.12.3 ...

Thanks for a the report. As a workaround you can put the image into a directory where TeX will look for input. Which TeX distribution do you use? I am using TeX Live and putting crown.png into ~/texmf/tex/latex/local made the error go away. I will try to think of a better way to make it work on Windows with long user names.

thanks for considering this. This isn't really a workable workaround for me, as the code I'm fixing is used in a workflow that's being run by people with very different setups - they won't all have the same TeX setup. I'm wondering if there's some way that's not terribly unwieldy to allow an optional argument specifying external resources (files etc.) that will need to be copied to the TeX working directory ... ??

Such an optional argument should be possible. Downside is that it has to go through the C code for the graphics device. I can think of another TeX based solution, though: Temporarily set TEXINPUTS to include the current working directory:

library(tikzDevice)
tex_inputs <- Sys.getenv("TEXINPUTS")
Sys.setenv(TEXINPUTS = paste(getwd(), tex_inputs, sep = .Platform$path.sep))
tikz("crown.tex")
plot(0:1,0:1,type="n")
text(0.5,0.5,"\\includegraphics[width=15pt]{crown.png}")
dev.off()
Sys.setenv(TEXINPUTS = tex_inputs)

I have tested this on TeX Live and expect it to work on derived distributions (MacTeX, ProTeXt, TinyTeX). And MikTeX seems to support this, too: https://docs.miktex.org/manual/localadditions.html

I will have to do some further testing, but something like this could accompany the wd-changing code in getMetricsFromLatex.

thanks for the workaround, working for me!