Graphics, Figures & TablesImage can't be included with twice \newcommand expansion

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Image can't be included with twice \newcommand expansion

Post by EmilioLazo »

Hi.

In the following code:

Code: Select all

\documentclass{article}
\newcommand{\test}{example-grid-100x100bp.jpg}
\newcommand{\testx}{\test}
\let\testxx\test
\usepackage{graphicx}
\begin{document}
test \includegraphics{\test} 
testx \includegraphics{\testx}
testxx \includegraphics{\testxx}
\end{document}
Both first and third image appears but second one doesn't:
! LaTeX Error: File `example-grid-100x100bp.jpg' not found.
If '.jpg' extension is removed from \test command definition, all three images appears.

Why?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Image can't be included with twice \newcommand expansion

Post by localghost »

Then just drop the suffix. In the most cases it is superfluous anyway.


Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
EmilioLazo
Posts: 15
Joined: Sat Jul 16, 2011 1:59 am

Re: Image can't be included with twice \newcommand expansion

Post by EmilioLazo »

But what is happening anyway?

Why the difference in the behaviour between first/third image inclusion and second?

(pdf)latex says that it can't find 'example-grid-100x100bp.jpg', the same file of the other two cases.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Image can't be included with twice \newcommand expansion

Post by cgnieder »

EmilioLazo wrote:But what is happening anyway?
The error message is actually misleading. When you do

Code: Select all

\def\test{picture.jpg}
\def\testx{\test}
...
\includegraphics{\testx}
the error message means to say »File `\test' not found«. The actual inclusion is done by the command \Ginclude@graphics which calls

Code: Select all

\filename@parse{#1}
The \filename@parse macro contains the code

Code: Select all

\expandafter\filename@path#1/\\
which means that the argument of \includegraphics is expanded once. This is why the first test succeeds. In the second case it finds the file »\test«.

The above mentioned \filename@parse now calls \filename@path which in this case finds no directory specified and thus assumes the current path and calls \filename@simple#1.\\ which checks for an extension. It finds none and lets \filename@ext to \relax. It also defines \edef\filename@base{#1}. This is an \edef which means it's contents get fully expanded. That means that \filename@base is now picture.jpg.

These macros now are checked by \Ginclude@graphics. If \filename@ext is equal to \relax it looks for a match of \filename@base plus one of the allowed extensions. This would be picture.jpg.png, for example. Such a file does not exist so you get the error message. Without an extension this doesn't matter since \filename@base would be picture and it would find picture.jpg.

Regards
site moderator & package author
Post Reply