# why is the directory trimmed from my path argument in includegraphics?

Hi community I have a weird problem. It seems that includegraphics won't accept the entire argument, it always trims off the directory part!

The argument was manipulated and concatenated in another command before passing to the concerned command. But \typeout the argument shows that the argument contains correct string. What happened!?

A MWE:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\usepackage{calc}
\usepackage{xstring}

\newcommand{\setInset}[6]{%
\expandafter\gdef\csname useInset#1\endcsname{1}%
\expandafter\gdef\csname inset#1Color\endcsname{#2}%
\expandafter\gdef\csname crop#1X\endcsname{#3}%
\expandafter\gdef\csname crop#1Y\endcsname{#4}%
\expandafter\gdef\csname crop#1W\endcsname{#5}%
\expandafter\gdef\csname crop#1H\endcsname{#6}%
}

\color{#2}%
\typeout{OMG #1}
\fbox{\includegraphics[width=1\linewidth]{{#1}}}%
}

\makeatletter
\filename@parse{#2}
\StrSubstitute{\filename@base.\filename@ext}{.}{-}[\tmpName]
\def\baseFileName{\filename@area\tmpName}
% setting \cropfile manually works! e.g.
% \def\cropfile{figures/lr_img_053-png-19.25x19.25+176.5+123.75}
\def\cropfile{\baseFileName-19.25x19.25+176.5+123.75}

}
\makeatother

\begin{document}
\begin{figure}\centering
\setInset{A}{red}{176.5}{123.75}{19.25}{19.25}
\end{figure}
\end{document}


Calling \mycommand{red}{figures/figure.png} always gives me error Not found.

• Can you make a minimal working example (MWE)? – user36296 Feb 19 '18 at 19:37
• @samcarter please have a look now :) – yfi Feb 19 '18 at 20:16
• Are all your images in the folder figures? If yes, I'd simply suggest to add it to the graphic path. – user36296 Feb 19 '18 at 20:24
• What's the purpose of inserting . and then to change it into -? Wouldn't it be simpler to directly do \edef\baseFileName{\filename@area\filename@base-\filename@ext}? – egreg Feb 20 '18 at 0:57
• yeah except there might be other . i wanna change - in the base and ext. – yfi Feb 20 '18 at 13:07

\includegraphics uses \filename@parse internally. Because you used \def\cropfile instead of \edef\cropfile, \baseFileName isn't expanded and depends on the value of \filename@area. \filename@parse inside \includegraphics overwrites the value of \filename@area before expanding the argument, so it sees the value of \filename@area it already reset instead of the value assigned by your \filename@parse in \addInsets.

So you can fix this like @Ian Thompson suggested by writing

\edef\cropfile{\baseFileName-19.25x19.25+176.5+123.75}


\def\cropfile{\baseFileName-19.25x19.25+176.5+123.75}


or by renaming your version of \filename@area:

\newcommand{\addInsets}[2][1]{%
\filename@parse{#2}
\let\myFileArea\filename@area % \edef could be used here too
\StrSubstitute{\filename@base.\filename@ext}{.}{-}[\tmpName]
\def\baseFileName{\myFileArea\tmpName}
...
}


I'm too tired to work out what's going on here, but the problem seems to go away if you change \def to \edef in

\def\cropfile{\baseFileName-19.25x19.25+176.5+123.75}