# R tikzdevice relative directories

I asked this on SO, but perhaps it is more relevant here:

This is a problem which I face with many many plots.

In R, Lets say I create a ggplot2 plot, with a colourbar:

R> sample = ggplot(data=data.frame(x=runif(10),y=runif(10)),aes(x,y)) +
geom_point(aes(color=x))


Now when I export this to a tikz object via the following:

tikz("./figures/sample.tikz",width = 6,height=6/sqrt(2))
print(sample)
dev.off()


This can then be included in the document like thus:

\begin{figure}[htbp]
\centering
\input{figures/sample.tikz}
\end{figure}


Inside sample.tikz, At around line 193 it contains the following:

\pgfimage[width= 14.45pt,height= 72.27pt,interpolate=true]{sample_ras1}};


which is referring to a raster image of the colourbar. It was obviously created and written in the same directory as the main .tikz file.

Now the problem is, this .tikz file is written to an images folder, which is a subdirectory to the root document, and therefore, for this to be plotted correctly in a latex document, the above pgfimage line should be as follows:

\pgfimage[width= 14.45pt,height= 72.27pt,interpolate=true]{figures/sample_ras1}};


How can I ensure (is it even possible) that the image is being picked up relative to the tikz file, NOT the root .tex file?

I could go through and manually add the small change, but like I said, I have many many images and this would be tedious. One option is to simply write the tikz files to the root folder, however, I don't particularly want to pollute the root folder with countless images.

UPDATE: One solution that I have come up with, is to create a new command for inputting tikz files:

\newcommand\inputTikz[2][figures]{
\let\pgfimageOld\pgfimage%
\renewcommand{\pgfimage}[2][]{\pgfimageOld[##1]{#1/##2}}%
\input{#1/#2}%
\let\pgfimage\pgfimageOld%
}


And this seems to do the job, but its a very specific solution, locally redefining the pgfimage command. It will fail if other commands emerge which suffer the same fate.

It can be used like this:

\begin{figure}[htbp]
\centering
\inputTikz{sample.tikz}
\end{figure}

• At least give a link if cross-posting, both here to there and there to here. Right now you're just saying you don't care if people needlessly duplicate effort, so long as you get what you want. However, you are at least being honest about it. – cfr Feb 20 '17 at 2:12
• Yep. Sorry about that. – Nicholas Hamilton Feb 20 '17 at 2:54
• I've experienced this issue before. No good solution yet as far as I know. I think this github issue is closely related. A poor solution that I've used when necessary is to set fig.path='fig-' (or similar) to at least have all generated TikZ files in some order. – chepec Feb 20 '17 at 3:57
• @chepec Thanks. I have updated my question with a solution that seems to work for me, but its far from ideal. – Nicholas Hamilton Feb 20 '17 at 10:09