[Tex/LaTex] Image File Not Found in LaTeX

graphicsinputpaths

I am trying to use the includegraphics command alongside a custom command to include an image in my PDF file, however I'm running into an issue where LaTeX can't seem to find a file that clearly exists on my system.

Here's the line that has the issue:

\includegraphics[width=0.6\textwidth]{\matlabgraph{rplot}}

where these are defined in the class file:

\newcommand{\matlabvalue}[1]{\input{\datadir{value/#1.dat}}}
\newcommand{\matlabgraph}[1]{\datadir{graph/#1.png}}

and \datadir is defined in the command line:

pdflatex "\newcommand\datadir[1]{\detokenize{C:/Users/<user>/AppData/...}/#1}\input{D:/Users/<user>/path/to/tex}"

The odd thing is that the \matlabvalue command works perfectly, but the \matlabgraph command does not. It gives me the following error:

LaTeX Error: File `C:/Users/<user>/AppData/.../graph/rplot.png` not found.

When I navigate to the directory listed in the error, I see the rplot.png file and can open it without any issues. The only issue I can think of that could cause this is that in the path to the file (in <user>) there's a tilde (~). However, I would expect that the \detokenize in the command line would've taken care of that…as it works with the \matlabvalue command.

Does anyone have any idea of what's going on here? Do I have to cleanup the path before passing it into \includegraphics?

UPDATE: I have discovered that the problems lies with the \matlabgraph command.

I put these two lines into my tex file:

\includegraphics{\datadir{graph/rplot.png}}
\includegraphics{\matlabgraph{rplot}}

The first line works, but the second line causes an error. Considering that \matlabgraph{rplot} is effectively a shorthand for \datadir{graph/rplot.png} why would \includegraphics error out with \matlabgraph?

Best Answer

This code will not work:

\usepackage{graphicx}

\begin{document}
  \newcommand\myimage[1]{#1}
  \newcommand\mymyimage[1]{\myimage{#1}}
  \includegraphics[scale=.5]{\myimage{/usr/local/texlive/2014/texmf-dist/tex/latex/mwe/example-image-a.png}}
  \includegraphics[scale=.5]{\mymyimage{/usr/local/texlive/2014/texmf-dist/tex/latex/mwe/example-image-b.png}}
\end{document}

This is because the commands try to include the file {/usr/local/texlive/2014/texmf-dist/tex/latex/mwe/example-image-b.png} i.e. it is treated as a single thing.

Making the following change allows the code to work:

  \newcommand\mymyimage[1]{\myimage#1}

This is because the additional set of curly brackets, which causes the grouping effect is removed.

At least, I think so. At any rate, it does work.