代码

代码

下面是该包的文档的子部分的图像xcolor

内联颜色样本

如何在文本中插入这样的颜色样本?我需要它来解释原理图不同部分的功能。

答案1

您可以检查原始源代码xcolor手动的

它使用一个带有\colorbox紧密(\fboxsep设置为\z@= 0pt)的框\fbox
彩色框的尺寸取自字符串“XX”。该宏\phantom扩展为具有其参数尺寸的框。

代码

\documentclass{article}
\usepackage{xcolor}
\makeatletter
\def\testclr#1#{\@testclr{#1}}
\def\@testclr#1#2{{\fboxsep\z@\fbox{\colorbox#1{#2}{\phantom{XX}}}}}
\newcommand*\Color[1]{\textsl{#1}}
\makeatother
\begin{document}
\begin{itemize}
\item How does a mixture of 40\% \Color{green} and 60\% \Color{yellow} look like?\\
(Answer: 40\% \testclr{green} $+$ 60\% \testclr{yellow} $=$ \testclr{green!40!yellow}, e.g., \verb|\color{green!40!yellow}|)
\item And how does its complementary color look like?\\
(Answer: \testclr{-green!40!yellow}, accessible via \verb|\color{-green!40!yellow}|)
\item Now I want to mix three parts of the last color with two parts of its complement and one part of \Color{red}.
How does that look?\\
(Answer: $3\times$ \testclr{-green!40!yellow} $+ 2\times$ \testclr{green!40!yellow} $+ 1\times$ \testclr{red} $=$ \testclr{rgb:-green!40!yellow,3;green!40!yellow,2;red,1}, the last color being accessible via \verb|\color{rgb:-green!40!yellow,3;green!40!yellow,2;red,1}|)
\end{itemize}
\end{document}

答案2

我认为另一个答案是正确的,但如果你想这样做tikz,这里有一个选择

截屏

这使您可以访问所有tikz可以做的有趣的事情,例如rounded cornersdecorated paths

\documentclass{article}

\usepackage{tikz}
\newcommand{\swatch}[1]{\tikz[baseline=-0.6ex] \node[fill=#1,shape=rectangle,draw=black,thick,minimum width=5mm,rounded corners=2pt](){};}

\begin{document}
Answer: 40\% \swatch{green}+60\% \swatch{yellow}=\swatch{green!40!yellow}
\end{document}

相关内容