是否可以绘制一个\fcolorbox
包含多种颜色的图形?
我想在我的投影仪图中绘制一种“图例”,最终看起来像这样:
(使用 Windows Paint 和 MS Word 创建)
我熟悉fcolorbox
,但不知道如何在其中绘制多种颜色。
\documentclass[]{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}
%\fcolorbox{black}{ForestGreen,Magenta,red}{\vspace{0em}\hspace{1em}} --- Multiple Channels % <<< gives error
\fcolorbox{black}{red}{\vspace{0em}\hspace{1em}} --- Driver
\end{document}
任何建议都会有帮助!
答案1
\documentclass[]{article}
\usepackage[dvipsnames]{xcolor}
\begin{document}
%\fcolorbox{black}{ForestGreen,Magenta,red}{\vspace{0em}\hspace{1em}} --- Multiple Channels % <<< gives error
{\setlength\fboxsep{0pt}%
\fbox{%
\textcolor{red}{\rule{5pt}{20pt}}%
\textcolor{blue}{\rule{5pt}{20pt}}%
\textcolor{green}{\rule{5pt}{20pt}}%
\textcolor{yellow}{\rule{5pt}{20pt}}%
}}
\end{document}
答案2
巧的是,@egreg 昨天的回答(如何通过 \pgfkeys 构造 key=value 的宏) 与此相吻合。
稍微调整一下这个答案:
平均能量损失
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\ExplSyntaxOn
\keys_define:nn { lyl/mycolorbox }
{
color .tl_set:N = \l__lyl_mycolorbox_color_tl,
text .tl_set:N = \l__lyl_mycolorbox_text_tl,
color .initial:n = red!40,
text .initial:n = some~text,
}
\NewDocumentCommand{\mycolorbox}{O{}}
{
\group_begin:
\keys_set:nn { lyl/mycolorbox } { #1 }
\colorbox{\l__lyl_mycolorbox_color_tl}{\l__lyl_mycolorbox_text_tl}
\group_end:
}
\ExplSyntaxOff
\begin{document}
\mycolorbox[color=blue!20]
\mycolorbox[text=This is a practice. Default colour.]
\mycolorbox[color=blue!20, text=This is a practice.]
\mycolorbox[text=\strut]
: with strut
\mycolorbox[text=\strut\ ]
: strut with space
\mycolorbox[text=\strut\ ,color=red]
\mycolorbox[text=\strut\ ,color=blue]
\mycolorbox[text=\strut\ ,color=green]
\mycolorbox[text=\strut\ ,color=yellow]
: set of boxes
\setlength{\fboxsep}{5pt}
\mycolorbox[text=\strut\ ,color=red]
\mycolorbox[text=\strut\ ,color=blue]
\mycolorbox[text=\strut\ ,color=green]
\mycolorbox[text=\strut\ ,color=yellow]
: fboxsep=5pt
\mycolorbox[text=\strut R ,color=red]%
\mycolorbox[text=\strut\textcolor{white}{B} ,color=blue]%
\mycolorbox[text=\strut G ,color=green]%
\mycolorbox[text=\strut Y ,color=yellow]
: no spaces
\fbox{%
\mycolorbox[text=\strut R ,color=red]%
\mycolorbox[text=\strut\textcolor{white}{B} ,color=blue]%
\mycolorbox[text=\strut G ,color=green]%
\mycolorbox[text=\strut Y ,color=yellow]%
}
: inside fbox
\fcolorbox{violet}{blue!20}{%
\mycolorbox[text=\strut R ,color=red]%
\mycolorbox[text=\strut\textcolor{white}{B} ,color=blue]%
\mycolorbox[text=\strut G ,color=green]%
\mycolorbox[text=\strut Y ,color=yellow]%
}
: inside fcolorbox
\setlength{\fboxrule}{2pt}
\fcolorbox{violet}{blue!20}{%
\mycolorbox[text=\strut R ,color=red]%
\mycolorbox[text=\strut\textcolor{white}{B} ,color=blue]%
\mycolorbox[text=\strut G ,color=green]%
\mycolorbox[text=\strut Y ,color=yellow]%
}
: fboxrule = 2pt
\fcolorbox{violet}{blue!20}{%
\mycolorbox[text=\strut\sffamily red ,color=red]%
\mycolorbox[text=\strut\sffamily\textcolor{white}{blue} ,color=blue]%
\mycolorbox[text=\strut\sffamily green ,color=green]%
\mycolorbox[text=\strut\sffamily yellow ,color=yellow]%
}
\end{document}
答案3
遵循 David 的想法,但让 TeX 进行计算……
\documentclass{article}
\usepackage[dvipsnames]{xcolor}
\ExplSyntaxOn
\NewDocumentCommand{\colorlegend}{O{2em}m}
{% #1 = total width, #2 = list of color names
\group_begin:
\setlength{\fboxsep}{0pt}
\fbox { \tush_colorlegend:nn { #1 } { #2 } }
\group_end:
}
\seq_new:N \l__tush_colorlegend_colors_seq
\dim_new:N \l__tush_colorlegend_wd_dim
\cs_new_protected:Nn \tush_colorlegend:nn
{
\seq_set_from_clist:Nn \l__tush_colorlegend_colors_seq { #2 }
\dim_set:Nn \l__tush_colorlegend_wd_dim { #1 / \seq_count:N \l__tush_colorlegend_colors_seq }
\seq_map_inline:Nn \l__tush_colorlegend_colors_seq
{
\textcolor{##1}{\rule{\l__tush_colorlegend_wd_dim}{0.8\ht\strutbox}}
}
}
\ExplSyntaxOff
\begin{document}
\colorlegend{ForestGreen,Apricot,Magenta,TealBlue,YellowOrange,green!20} -- Multiple channels
\colorlegend{red} -- Driver
\colorlegend[40pt]{ForestGreen,Apricot,Magenta,TealBlue,YellowOrange,green!20} -- Multiple channels
\colorlegend[30pt]{red} -- Driver
\end{document}