1

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

  1. it first checks for \filename@base _c\filename@ext whatever is provided as filename (with or without extension)
  2. if file doesn't exist, then checks for \filename@base\filename@ext
  3. 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}
NBur
  • 4,326
  • 10
  • 27
  • With similar issues I have found it a lot easier to simply store versions of the graphics in different folders, and then change the graphics path. – Aubrey Blumsohn Apr 03 '18 at 09:38
  • That could be an option, despite the fact I'd like to minimise the storage footprint and avoid double pictures if there is no color version. – NBur Apr 03 '18 at 10:03
  • you can try do define a new command \newif\ifvcol \newcommand{\NBCincludegraphics}[2][]{% \ifvcol \includegraphics[#1]{#2_c} \else \includegraphics[#1]{#2} \fi} and use it \NBCincludegraphics[option]{filename} filename without _c and without .ext – touhami Apr 03 '18 at 10:13
  • Sure, but with this solution I have an error if there is no #2_c in the color version ;) – NBur Apr 03 '18 at 10:16
  • you can use \iffileexists test – touhami Apr 03 '18 at 10:30
  • In a very first version, I wrote \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

2 Answers2

0

Here is a solution.

\documentclass{article}
\usepackage{graphicx}

\newif\ifvcol 
\vcoltrue

\let\WBCfile\relax
\makeatletter  
\newcommand{\WBCincludegraphics}[2][]{%
\ifvcol
\@for\Gin@temp:=\Gin@extensions\do{%
\IfFileExists{#2_c\Gin@temp}{%
\def\WBCfile{#2_c\Gin@temp}}{}}%
\ifx\WBCfile\relax
\includegraphics[#1]{#2}%
\else
\includegraphics[#1]{#2_c}%
\fi
\else
\includegraphics[#1]{#2}%
\fi
\let\WBCfile\relax}
\makeatother

\begin{document}
\WBCincludegraphics[scale=.5]{example-image}
\end{document}
touhami
  • 19,520
0

@touhami solved my OP.

However, in order to keep the original \includegraphics command, I'm going to use the snippet below (according to few tests it seems to work). Thus I can provide filename with or without extension.

\documentclass[12pt,a4paper]{memoir}
\newif\ifvcol 
\vcolfalse

\usepackage{graphicx}
\makeatletter
\def\Ginclude@graphics#1{%
    \begingroup
    \let\input@path\Ginput@path
    \filename@parse{#1}%
    \ifvcol
    \filename@parse{\filename@area\filename@base _c}%
    \@for\Gin@temp:=\Gin@extensions\do{%
        \ifx\Gin@ext\relax
            \Gin@getbase{\Gin@temp}
        \fi}%
    \fi
    \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}% <----------------------------- MODIFIED
            {I could not locate the file with any of these extensions:^^J% <-- MODIFIED
                \Gin@extensions^^J\@ehc}% <-------------------------------------- MODIFIED
    \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


\newcommand{\fig}[2][.26]{{\par\centering\includegraphics[width=#1\linewidth]{#2}\par}}
\usepackage{lipsum}
\begin{document}
    \lipsum[1]
    \fig{Image}
    \lipsum[2]
    \includegraphics[height=2cm]{Image.png}
\end{document}
NBur
  • 4,326
  • 10
  • 27