
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}%
}
\newcommand{\addInset}[2]{%
\color{#2}%
\typeout{OMG #1}
\fbox{\includegraphics[width=1\linewidth]{{#1}}}%
}
\makeatletter
\newcommand{\addInsets}[2][1]{%
\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}
\addInset{\cropfile.png}{\insetAColor} % works if I hardcode \addInset{figures/\cropfile.png}
}
\makeatother
\begin{document}
\begin{figure}\centering
\setInset{A}{red}{176.5}{123.75}{19.25}{19.25}
\addInsets{figures/lr_img_053.png}
\end{figure}
\end{document}
Calling \mycommand{red}{figures/figure.png}
always gives me error Not found
.
답변1
\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}
instead of
\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}
...
}
답변2
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}