daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
131 stars 26 forks source link

Use \includegraphics instead of \pgfimage #205

Open fueralles opened 4 years ago

fueralles commented 4 years ago

I have tried to include a tex file from the tikz output in my latex document. Since I did not put it in my main folder but in a subfolder, I always get confusing error messages. If I add the folder path like \pgfimage{path/tikz.tex} before the file name in the \pgfimage command, I can fix the error messages. So it would be pretty good to add an option to add an optional path for the filenames. If there is any misbehavior on my part, I would ask for instructions to get a runnable latex file . Thanks a lot

M(N)WE: The problem is the subfolder "./images/". The plot_1.tex contains the critical command "\pgfimage{plot_1_ras1}"

latex file: `\documentclass[10pt]{article}

\usepackage[pdftex]{graphicx} \graphicspath{{./images/}} \usepackage{tikz}

\begin{document}

\begin{figure} \input{./images/plot_1.tex} \caption{Sample output from tikzDevice} \label{plot:test} \end{figure} \end{document}`

r file: tikz(file = ".\\images\\plot_1.tex", width = 10, height = 5) ggplot(faithfuld, aes(waiting, eruptions)) + geom_raster(aes(fill = density)) dev.off()

rstub commented 4 years ago

You could include path in the path used by LaTeX to search for images via the \graphicspath command. Untested, since you do not give a reproducible example.

fueralles commented 4 years ago

Could you please explain this workaround more in detail. I have added my latex code in the first post. A workaround with the TEXINPUT variable seems to work but is user-unfriendly as I would have to do it for every graphic.

rstub commented 4 years ago

What do you mean by "as I would have to do it for every graphic"? The TEXINPUTS environment variable would be a good solution. Setting it to ./images:<current-value> should solve the issue, e.g. in a bash like shell

export TEXINPUTS=./images:$TEXINPUTS

BTW, to make you example reproducible it would be nice if you could supply some simple R code that uses \pgfimage when processed with tikzDevice.

fueralles commented 4 years ago

Some graphics I produce have several png files and I want to have them in separate folders. Furthermore, depending on my latex main file the graphics folder is labeled different. Therefore, I prefer a different solution.

Edit: It seems that I can limit my problem to a countable number of cases. Thus, I build a small wrapper for the tikz() function which adds the missing path to the tex file. This is the better solution compared to TEXINPUTS at least for me.

rstub commented 4 years ago

I have now created my own example. R code for producing the image:

library(tikzDevice)

tikz("image.tex", width = 5)
plot(c(100, 150), c(300, 350), type = "n", xlab = "", ylab = "")
image <- as.raster(matrix(rep(c(rep(0:1, 4), rep(1:0, 4)), each = 3), ncol = 6, nrow = 4))
rasterImage(image, 100, 300, 150, 350, interpolate = FALSE)
dev.off()

The resulting files image.tex and image_ras1.png are placed in the sub-directory tikz. Main LaTeX file:

\documentclass{article}
\usepackage{graphicx}
\graphicspath{{./tikz/}}
\usepackage{tikz}
% \let\pgfimage=\includegraphics % un-comment to make it work
\begin{document}
\input{tikz/image.tex}
\end{document}

This reproduces the issue since pgfimage does not use \graphicspath. And there seems to be no analogue to it either. However, the PGF manual actually recommends using \includegraphics instead, as long as you are not using masking (c.f. section 111.1 Overview). Since tikzDevice does not use masking, one can actually replace pgfimage with \includegraphics. If you un-comment the above line the minimal example compiles. I will change the title to make this change within tikzDevice.

For you this requires specifying all the image sub-directories using \graphicspath. That is slightly inconvenient, but currently I do not want to bring additional options into the C code.

fditraglia commented 1 year ago

This would be a very helpful change to tikzDevice. Setting TEXINPUT works but is a real pain if you need to collaborate with others.

x1o commented 1 year ago

For some reason,

\graphicspath{{./tikz/}}

does not work using mactex.

\graphicspath{{tikz/}}

does.