2

This post asks for an extension of Werner's answer given in Color an example-image.

Consider the following code:

\documentclass{article}
\usepackage{xcolor,graphicx}
\usepackage{caption}

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % \makeatletter \define@key{Gin}{color}{\def\Gin@color{#1}}

\setlength{\fboxsep}{0pt} \let\oldincludegraphics\includegraphics \renewcommand{\includegraphics}[2][]{% \ifnum\pdfstrcmp{#2}{example-image}=0% \begingroup \setkeys{Gin}{color=red,#1} % Sets default color to be red \colorbox{\Gin@color}{\phantom{\oldincludegraphics[#1]{#2}}}% \endgroup \else \oldincludegraphics[#1]{#2}% \fi } \makeatother % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

\begin{document} \begin{figure}[!htb] \centering \includegraphics[width=20em,height=30em]{example-image} \captionsetup{labelformat=empty} \vskip 8pt \caption{\textbf{\scshape{\large (Default Red) Figure}}} \end{figure}

\begin{figure}[!htb] \centering \includegraphics[width=20em,height=30em,color=blue]{example-image} \captionsetup{labelformat=empty} \vskip 8pt \caption{\textbf{\scshape{\large (Specified) Blue Figure}}} \end{figure} \end{document}

which produces the two figures:

enter image description here

enter image description here

QUESTION: Is it possible to modify the macro so that the original example-image appears if no color is specified to produce the figure? If so, how? The macro currently defaults the color of the example-image to be "red" if no specific color is indicated in producing the example-image.

Thank you.

DDS
  • 8,816
  • 4
  • 9
  • 36
  • @Simon Dispa The original (default) example-image resized according to the \begin{figure} \end{figure} specifications, but the original "drab" color of the default graphicx image when no specific color is specified. – DDS Jan 24 '22 at 19:22
  • Not sure I fully understand the question, but to use the default color you can specify color=.. – Peter Grill Jan 24 '22 at 20:32
  • @Peter Grill I thought so too; but when I ran the code with \includegraphics[width=20em,height=30em,color=]{example-image} for the first figure, it produced an error. – DDS Jan 24 '22 at 21:51
  • Not color=, but color=. (note the "."). – Peter Grill Jan 24 '22 at 22:52
  • @Peter Grill It runs (with Pdflatex) without an error using \includegraphics[width=20em,height=30em,color=.]{example-image}; thank you. However, it produces a solid black image. – DDS Jan 24 '22 at 23:12

1 Answers1

1

You can set the default value to for example none and add an extra check for that:

\documentclass{article}
\usepackage{xcolor,graphicx}
\usepackage{caption}

% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % \makeatletter \define@key{Gin}{color}{\def\Gin@color{#1}}

\setlength{\fboxsep}{0pt} \let\oldincludegraphics\includegraphics \renewcommand{\includegraphics}[2][]{% \ifnum\pdfstrcmp{#2}{example-image}=0% \begingroup \setkeys{Gin}{color=none,#1} % Sets default color to be red \ifnum\pdfstrcmp{\Gin@color}{none}=0% \oldincludegraphics[#1]{#2}% \else% \colorbox{\Gin@color}{\phantom{\oldincludegraphics[#1]{#2}}}% \fi% \endgroup \else \oldincludegraphics[#1]{#2}% \fi } \makeatother % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %

\begin{document} \begin{figure}[!htb] \centering \includegraphics[width=2cm,height=2cm]{example-image} \captionsetup{labelformat=empty} \vskip 8pt \caption{\textbf{\scshape{\large (Default) Figure}}} \end{figure}

\begin{figure}[!htb] \centering \includegraphics[width=2cm,height=2cm,color=blue]{example-image} \captionsetup{labelformat=empty} \vskip 8pt \caption{\textbf{\scshape{\large (Specified) Blue Figure}}} \end{figure} \end{document}

enter image description here

Marijn
  • 37,699