11

I have a \graphicspath command at the beginning of a document (the main document):

\graphicspath{{images/}{extras/images/}}

I have sub-documents into which I insert images (actually SVG files from Inkscape):

\def\svgwidth{\linewidth}
\input{/another/path/images/ppn522-graphs.pdf_tex}

These sub-documents may be inserted into different main documents.

But they need some information about images to be included so I need to do this:

\def\svgwidth{\linewidth}
\graphicspath{{/another/path/images/}}
\input{/another/path/images/ppn522-graphs.pdf_tex}

Problem: this overrides my first \graphicspath declaration: my logo images (and extras/images) are not found anymore (generating errors) after I insert the \graphicspath{{/another/path/images/}}

My question: how could I append an entry to the \graphicspath ?

Or is there a variable that contains the \graphicspath value so I can re-use it ?

    \graphicspath{\thegraphicspathvalue{/another/path/images/}}

EDIT: I already tried this, but it does not work :

\graphicspath{\typeout{\Ginput@path}{../images/}}

1 Answer 1

18

The variable containing the paths is \Ginput@path, so you can define

\makeatletter
\newcommand\appendtographicspath[1]{%
  \g@addto@macro\Ginput@path{#1}%
}
\makeatother

and \appendtographicspath{{../images/}} should do. Remember the braces around the path; you can add several paths.

This addition is global. You might want to extend the macro for “prepending”; in this case, use etoolbox:

\usepackage{etoolbox}
\makeatletter
\newcommand\appendtographicspath[1]{%
  \gappto\Ginput@path{#1}%
}
\newcommand\prependtographicspath[1]{%
  \gpreto\Ginput@path{#1}%
}
\makeatother

With \appto and \preto the additions would be local, but I don't think it's good.

Update 2022

Time has come for an update. The macro \Ginput@path behaves like an expl3 token list variable.

We can therefore use expl3 for a more flexible approach (also prompted by a comment).

\ExplSyntaxOn
\NewDocumentCommand{\appendtographicspath}{m}
 {
  \tl_if_exist:cF { Ginput@path } { \tl_new:c { Ginput@path } }
  \tl_gput_right:cn {Ginput@path} { #1 }
 }
\NewDocumentCommand{\prependtographicspath}{m}
 {
  \tl_if_exist:cF { Ginput@path } { \tl_new:c { Ginput@path } }
  \tl_gput_left:cn {Ginput@path} { #1 }
 }
\ExplSyntaxOff

Something like

\appendtographicspath{{a}{b}{c}}

will append all three items.

If one wants to allow specifying the items to append as a comma separated list, then do

\documentclass{article}
\usepackage{graphicx}

\ExplSyntaxOn

\NewDocumentCommand{\appendtographicspath}{m}
 {
  \tl_if_exist:cF { Ginput@path } { \tl_new:c { Ginput@path } }
  \clist_map_function:nN { #1 } \steven_gpath_append:n
 }

\cs_new_protected:Nn \steven_gpath_append:n
 {
  \tl_gput_right:cn { Ginput@path } { {#1} }
 }

% for debugging

\NewDocumentCommand{\showgraphicspath}{}{\tl_show:c { Ginput@path }}

\ExplSyntaxOff

\graphicspath{{a}{b}{c}}

\appendtographicspath{{d},{e}}

\showgraphicspath

\appendtographicspath{f,g}

\showgraphicspath

Braces would be stripped off by \clist_map_inline:nN so we can reinsert them.

At the end of the job, as witnessed by \showgraphicspath, you'd get

\Ginput@path={a}{b}{c}{d}{e}{f}{g}
12
  • I works perfectly. A question: the order is important only if I have several images with the same name, isn't it ?
    – lauhub
    Apr 15, 2014 at 8:43
  • 1
    @lauhub Yes. Note that using \graphicspath slows down the searching process, so more often visited directories should go first.
    – egreg
    Apr 15, 2014 at 9:38
  • Hi @egreg, thanks for this solution! Can I ask you a follow-up question? I'd like to append to the graphics path using a for-loop, looping over a comma-separated list of paths using \@for. This gives me an undefined control sequence. \p -> \@nil error. I suspect that it literally attempts to add \p/Images to the graphics path. I'm stuck from here. Any ideas?
    – Steven
    Nov 9, 2022 at 8:22
  • @Steven Why don't you just do \appendtographicspath{{a}{b}{c}}? Don't use backslashes for paths on Windows; TeX distributions recognize / instead.
    – egreg
    Nov 9, 2022 at 14:02
  • 1
    @PatrickT I added the checks for the post 2022 versions.
    – egreg
    2 days ago

You must log in to answer this question.

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