Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use \graphicspath to search for images #565

Open
pgf-tikz-bot opened this issue Aug 25, 2008 · 2 comments · May be fixed by #980
Open

Use \graphicspath to search for images #565

pgf-tikz-bot opened this issue Aug 25, 2008 · 2 comments · May be fixed by #980

Comments

@pgf-tikz-bot
Copy link

Migrated from SourceForge
Author: *anonymous
Timestamp: 2008-08-25 17:38:22

It is quite common to use \graphicspath{{../images/}} or similar with \includegraphics. It would be nice to have pgf search in these directories for \pgfdeclareimage just like \includegraphics does.

@pgf-tikz-bot
Copy link
Author

Migrated from SourceForge
Author: tantau
Timestamp: 2013-09-30 10:24:41.444000

  • status: open --> open-accepted
  • Group: --> Future

@hmenke hmenke added this to the 3.1.6 milestone Dec 17, 2019
@hmenke hmenke modified the milestones: 3.1.6, 3.1.7 Sep 3, 2020
@sergiud
Copy link

sergiud commented Jan 9, 2021

I have a working implementation for this feature. The changes are already used in a complex project which also makes heavy use of TikZ externalization.

Currently, the changes are injected into \pgf@declareimage by replacing all \pgf@findfile instances by a modified version of this macro that loops over the \Ginput@path content. I explicitly avoided patching \pgf@findfile because the macro is also used by the external library which is pretty fragile. As such, the search path support is limited to \pgfdeclareimage only.

Please advise how to integrate the changes into TikZ directly.

The changes are currently loaded through a package:

% inputpath.sty
\RequirePackage{etoolbox}

\def\inputpath@pgf@findfile#1:#2+#3{%
  \ifx\Ginput@path\@empty
    % There's nothing we can loop over: pass the arguments to the original
    % command as is.
    \inputpath@old@pgf@findfile{#1}:{#2}+{#3}%
  \else
    \let\inputpath@cur\@secondoftwo
    \expandafter\@tfor\expandafter
      \inputpath@cur\expandafter:\expandafter=\Ginput@path\do{%
      \def\inputpath@filename{\inputpath@cur/#3#1}%
      \pgfutil@IfFileExists{\inputpath@filename}%
      {\xdef\pgf@filename{\inputpath@filename}%
        % Found the file. We're done.
        \@break@tfor
      }%
      {\def\pgf@mightbeempty{#2}%
        \ifx\pgf@mightbeempty\pgfutil@empty\else%
          \inputpath@pgf@findfile#2+{#3}%
        \fi
      }%
    }%
  \fi
}%

\newcommand\inputpath@pgf@declareimage@fail{%
  \PackageWarning{inputpath}{\string\pgf@declareimage\space could not be
    patched\MessageBreak
    to enable graphicx search path support.\MessageBreak
    Using \string\graphicspath\space may not extend the\MessageBreak
    image search path\@gobble}%
}

\AtEndPreamble{%
  \ifx\pgf@findfile\@undefined\else
    % Add search path support to \pgfdeclareimage
    \let\inputpath@old@pgf@findfile=\pgf@findfile
    % Uncomment the following line for debugging purposes:
    % \tracingpatches
    \PackageInfo{inputpath}%
      {Patching two occurrences of \string\pgf@findfile\space in\MessageBreak
      \string\pgf@declareimage\space for enabling\MessageBreak
      graphicx search path support\@gobble}%
    % As of TikZ/PGF 3.1.8b, \pgf@declareimage contains two calls to
    % \pgf@findfile. Both references to the original macro must be replaced.
    % Check pgfcoreimage.code.tex if patching succeeds but \pgfdeclareimage
    % still cannot locate images in the graphicx search path.
    \patchcmd\pgf@declareimage
      {\pgf@findfile}%
      {\inputpath@pgf@findfile}%
      {%
        \PackageInfo{inputpath}{Patched first occurrence of
          \string\pgf@findfile\space in\MessageBreak
          \string\pgf@declareimage\@gobble}%
        \PackageInfo{inputpath}{Patching second occurrence of
          \string\pgf@findfile\space in\MessageBreak
          \string\pgf@declareimage\@gobble}%
        \patchcmd\pgf@declareimage
          {\pgf@findfile}%
          {\inputpath@pgf@findfile}%
          {\PackageInfo{inputpath}{Successfully patched
              \string\pgf@declareimage.\MessageBreak
               It is now possible to use \string\graphicspath\space\MessageBreak
               to extend the image search path\@gobble}}%
          {\inputpath@pgf@declareimage@fail}%
      }%
      {\inputpath@pgf@declareimage@fail}%
  \fi
}

Afterwards, one can simply use

\graphicspath{{./images/ch00/}}

to extend the search path for image declarations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

3 participants