0

Objective

I wanted to create a command that enables me to add a collection of paths in a comma-separated list to my graphics path, next to a bunch of other instructions for each path that I will not include. To append the graphics path, I use the code written by @egreg, see link. The result is \setDependentPaths, see the MWE below.

Issue

In the MWE, I placed an image image.png in the relative folder mypath/Resources/Images. So, I want to add mypath as a 'dependent path'. If I compile the document with the \includegraphics command commented out, it does not produce an error. When I don't comment it out, it produces the following error:

! Undefined control sequence.
\p ->\@nil 

I suspect that the loop variable \p is used literally, such that \p/Resources/Images is used as the graphics path instead of mypath/Resources/Images. However, I'm not sure, and even if that's the case, I don't know how to solve it.

Anyone any clues?

MWE

\documentclass{book}

% Load packages
\usepackage{graphicx}

% Set graphics path
\graphicspath{
    {Resources/Images/}
}

% Enable appending to graphics path
\makeatletter
\newcommand{\appendGraphicsPath}[1]{%
    \g@addto@macro\Ginput@path{#1}%
}
\makeatother

% This command loops over a comma-separated list to add graphics paths
\makeatletter
\newcommand{\setDependentPaths}[1]{%
    \@for\p:=#1\do{%
        \appendGraphicsPath{{\p/Resources/Images/}}%
    }%
}
\makeatother

% Set the paths that my document depends on
\setDependentPaths{mypath,anotherpath}

\begin{document}

A title

\includegraphics{image.png}

\end{document}

1 Answer 1

0

You need to expand \p so

\expandafter\appendGraphicsPath\expandafter{\expandafter{\p
3
  • I knew it, I need more knowledge on expansion! Would you be so kind to add an explanation on why you placed \expandafter in those locations?
    – Steven
    Nov 9, 2022 at 9:56
  • @Steven the first \expandafter expands the second, the second expands the third, and the third expands \p after which the input buffer looks like \appendGraphicsPath{{mypath Nov 9, 2022 at 10:31
  • by any chance, do you know how to do in case I do \providecommand{\main}{.} and call \appendGraphicsPath{{\main/\p/Resources/Images/}} in the for loop?
    – Steven
    Nov 16, 2022 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .