I've got a document that will be both printed and displayed on screen. In first case pictures should be B&W, on the other hand they can be colored.
With a \newif I can handle this easily.
In a dedicated folder I store the two versions of the pictures, with a _c suffix for the colored ones.
I now wish to patch the \includegraphics[opt]{filename} command so that
- it first checks for
\filename@base _c\filename@extwhatever is provided asfilename(with or without extension) - if file doesn't exist, then checks for
\filename@base\filename@ext - includes relevant picture or raises error if no file found.
I tried to tweak the \Ginclude@graphics command as explained here with no success…
For the MWE below to compile, just place two pictures Image.… and Image_c.… in the cwd.
% !TeX TS-encoding = utf8
% !TeX TS-spellcheck = fr_FR
% !BIB TS-program = biber
% !TeX TS-program = lualatex
\documentclass[12pt,a4paper]{memoir}
\usepackage{graphicx}
\makeatletter
\def\Ginclude@graphics#1{%
\begingroup
\let\input@path\Ginput@path
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase _c\Gin@temp
\fi
}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1_c' not found}%
\def\Gin@base{\filename@area\filename@base _c}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
%%% add new test here?
\ifx\Gin@ext\relax
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase\Gin@temp
\fi
}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1' not found}%
\def\Gin@base{\filename@area\filename@base}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
\fi
%%%
\ifx\Gin@ext\relax
\@latex@error{File `#1' not found}
{I could not locate the file with any of these extensions:^^J
\Gin@extensions^^J\@ehc}
\else
\@ifundefined{Gin@rule@\Gin@ext}%
{\ifx\Gin@rule@*\@undefined
\@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
\else
\expandafter\Gin@setfile\Gin@rule@*{\Gin@base\Gin@ext}%
\fi}%
{\expandafter\expandafter\expandafter\Gin@setfile
\csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%
\fi
\endgroup}
\makeatother
\begin{document}
\includegraphics[width=.7\linewidth]{Image}
\end{document}
\newif\ifvcol \newcommand{\NBCincludegraphics}[2][]{% \ifvcol \includegraphics[#1]{#2_c} \else \includegraphics[#1]{#2} \fi}and use it\NBCincludegraphics[option]{filename}filenamewithout_cand without.ext– touhami Apr 03 '18 at 10:13#2_cin the color version ;) – NBur Apr 03 '18 at 10:16\iffileexiststest – touhami Apr 03 '18 at 10:30\IfFileExists{#2_c.png}{\includegraphics{#2_c}}{\IfFileExists{#2_c.jpeg}{\includegraphics{#2_c}}{\IfFileExists{#2_c.jpg}{\includegraphics{#2_c}}{\IfFileExists{#2_c.pdf}{\includegraphics{#2_c}}{\includegraphics{#2}}}}}but I found it clumsy since I check few possible extensions lowercase only… So instead of checking every combination I wondered if there wasn't a better approach directly based on\includegraphics– NBur Apr 03 '18 at 10:47