![对示例图像进行着色,但保留默认的暗色](https://linux22.com/image/445202/%E5%AF%B9%E7%A4%BA%E4%BE%8B%E5%9B%BE%E5%83%8F%E8%BF%9B%E8%A1%8C%E7%9D%80%E8%89%B2%EF%BC%8C%E4%BD%86%E4%BF%9D%E7%95%99%E9%BB%98%E8%AE%A4%E7%9A%84%E6%9A%97%E8%89%B2.png)
这篇文章要求扩展 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}