对示例图像进行着色,但保留默认的暗色

对示例图像进行着色,但保留默认的暗色

这篇文章要求扩展 Werner 的回答对示例图像进行着色

考虑以下代码:

\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}

得出两个数字:

在此处输入图片描述

在此处输入图片描述

问题:是否可以修改宏,以便在未指定颜色来生成图形时显示原始示例图像?如果可以,该怎么做?如果在生成示例图像时未指定特定颜色,则宏当前默认示例图像的颜色为“红色”。

谢谢。

答案1

例如,您可以将默认值设置为none并添加额外的检查:

\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}

在此处输入图片描述

相关内容