有没有简单的方法来设置(空白)示例图像的颜色?
简单的方法不起作用:
\begin{figure}
\color{red}
\includegraphics[width=\textwidth]{example-image}
\color{black}
\end{figure}
我当然不想创建和存储大量彩色示例图像并逐个插入。这也使得被迫为不同的文档环境存储多个副本或处理图像文件路径变得很尴尬。
作为背景:我将使用它对稍后要插入的图形进行分类和优先排序(例如,红色=风景照片,绿色=肖像画,蓝色=计算机图表,橙色=要计算的图形,黄色=要购买的图形......)。
答案1
如果参数\includegraphics
是声明的颜色,则这将生成彩色矩形。在部分
\prop_gset_from_keyval:Nn \g_stork_ig_ratios_prop
{
red=3/4, green=16/9, blue=3/4, orange=3/4, yellow=3/4
}
您可以为相应类型的图像设置已知颜色和纵横比(高度/宽度)。
使用示例。复杂的代码可以保留在一个单独的文件中,\input
当所有图像都填满后,您可以删除该\input
行。
\documentclass{article}
\usepackage{graphicx,xcolor}
% to be removed when the document has all the pictures
\NewCommandCopy{\originalincludegraphics}{\includegraphics}
\ExplSyntaxOn
\RenewDocumentCommand{\includegraphics}{O{}m}
{
\stork_ig:nn { #1 } { #2 }
}
\prg_generate_conditional_variant:Nnn \clist_if_in:Nn { Nx } { T,F,TF }
\prop_new:N \g_stork_ig_ratios_prop
\clist_new:N \g_stork_ig_colors_clist
\keys_define:nn { stork/ig }
{
width .dim_set:N = \l_stork_ig_wd_dim,
width .initial:n = 8cm,
unknown .code:n = {},
}
\cs_new_protected:Nn \stork_ig:nn
{
\group_begin:
\clist_if_in:NxTF \g_stork_ig_colors_clist { \tl_to_str:n { #2 } }
{
\keys_set:nn { stork/ig } { #1 }
\stork_ig_fake:n { #2 }
}
{
\originalincludegraphics[#1]{#2}
}
\group_end:
}
\cs_new_protected:Nn \stork_ig_fake:n
{
\textcolor{#1}
{
\rule
{% width
\l_stork_ig_wd_dim
}
{% height
\dim_eval:n { \l_stork_ig_wd_dim * \prop_item:Nn \g_stork_ig_ratios_prop { #1 } }
}
}
}
% set the known colors
\prop_gset_from_keyval:Nn \g_stork_ig_ratios_prop
{
red=3/4, green=16/9, blue=3/4, orange=3/4, yellow=3/4
}
\prop_map_inline:Nn \g_stork_ig_ratios_prop
{
\clist_gput_right:Nn \g_stork_ig_colors_clist { #1 }
}
\ExplSyntaxOff
% end of temporary code
\begin{document}
\includegraphics[width=0.4\textwidth]{red}
\includegraphics[width=0.2\textwidth]{green}
\includegraphics[width=0.2\textwidth]{orange}
\includegraphics[width=0.4\textwidth]{example-image}
\end{document}
如果你不想让各种颜色有固定的长宽比,你可以修改代码来调用如下代码
\includegraphics[width=0.3\textwidth]{red:3/4}
其中纵横比始终是高度/宽度,但允许使用单个整数(隐含/1
)。
\documentclass{article}
\usepackage{graphicx,xcolor}
% to be removed when the document has all the pictures
\NewCommandCopy{\originalincludegraphics}{\includegraphics}
\ExplSyntaxOn
\RenewDocumentCommand{\includegraphics}{O{}m}
{
\stork_ig:nn { #1 } { #2 }
}
\prg_generate_conditional_variant:Nnn \clist_if_in:Nn { Nx } { T,F,TF }
\cs_generate_variant:Nn \seq_set_split:Nnn { NV }
\seq_new:N \l_stork_ig_name_seq
\clist_new:N \g_stork_ig_colors_clist
\keys_define:nn { stork/ig }
{
width .dim_set:N = \l_stork_ig_wd_dim,
width .initial:n = 8cm,
unknown .code:n = {},
}
\cs_new_protected:Nn \stork_ig:nn
{
\group_begin:
\seq_set_split:NVn \l_stork_ig_name_seq \c_colon_str { #2 }
\clist_if_in:NxTF \g_stork_ig_colors_clist { \seq_item:Nn \l_stork_ig_name_seq { 1 } }
{
\keys_set:nn { stork/ig } { #1 }
\stork_ig_fake:xx
{ \seq_item:Nn \l_stork_ig_name_seq { 1 } }
{ \seq_item:Nn \l_stork_ig_name_seq { 2 } }
}
{
\originalincludegraphics[#1]{#2}
}
\group_end:
}
\cs_new_protected:Nn \stork_ig_fake:nn
{
\textcolor{ #1 }
{
\rule
{% width
\l_stork_ig_wd_dim
}
{% height
\__stork_ig_height:n { #2 }
}
}
}
\cs_generate_variant:Nn \stork_ig_fake:nn { xx }
\cs_new:Nn \__stork_ig_height:n
{
\dim_eval:n
{
\l_stork_ig_wd_dim
*
\tl_if_blank:nTF { #1 } { 3/4 } { #1 }
}
}
% set the known colors
\clist_gset:Nn \g_stork_ig_colors_clist { red, green, blue, orange, yellow }
\ExplSyntaxOff
% end of temporary code
\begin{document}
\includegraphics[width=0.4\textwidth]{red:3/4}
\includegraphics[width=0.2\textwidth]{green:16/11}
\includegraphics[width=0.2\textwidth]{orange:1}
\includegraphics[width=0.4\textwidth]{example-image}
\end{document}
答案2
以下方法
将选项添加
color
到键列表中\includegraphics
(默认为red
);假设您的“替代”图像由给出
example-image
。
如果您提供,则放置一个与 尺寸设置相匹配的example-image
颜色盒。除 之外的任何其他颜色都根据需要设置(即使有规格)。color
\includegraphics
example-image
color
\documentclass{article}
\usepackage{xcolor,graphicx}
\makeatletter
\define@key{Gin}{color}{\def\Gin@color{#1}}
\setlength{\fboxsep}{0pt}% Remove \fbox separation
\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\ifnum\pdfstrcmp{#2}{example-image}=0%
\begingroup
\setkeys{Gin}{color=red,#1}% Default color is red
\colorbox{\Gin@color}{\phantom{\oldincludegraphics[#1]{#2}}}%
\endgroup
\else
\oldincludegraphics[#1]{#2}%
\fi
}
\makeatother
\begin{document}
\includegraphics[width=3em,height=4em,color=red]{example-image}
\includegraphics[width=3em,height=4em,color=red]{example-image-a}
\end{document}