Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The following is an image of a sub-section of the documentation of the package xcolor.

Inline color swatch

How to insert such a color swatch within text? I need it to explain the functionality of different parts of a schematic.

share|improve this question
    
I tried searching strings like "latex color swatch". Obviously (in hindsight) it took me to links related to colored rubber materials. –  Shashank Sawant Oct 22 '12 at 21:42
1  
Searching instead for tex latex color swatch gives you some more relevant pages. –  Matthew Leingang Oct 23 '12 at 0:17

2 Answers 2

up vote 12 down vote accepted

You could inspect the original source code for the xcolor manual.

It uses a \colorbox that has a tight (\fboxsep is set to \z@ = 0pt) \fbox around it.
The dimension of the colored box is taken from the string “XX”. The macro \phantom expands to a box with the dimension of its argument.

Code

\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}
share|improve this answer

I think the other answer is the way to go, but just in case you'd like to do it with tikz, here's an option

screenshot

This gives you access to all of the fun things that tikz can do such as rounded corners and decorated paths etc.

\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}
share|improve this answer
    
Thanks! I didn't really pay attention to this in a long time. I use tikz almost always and this works like a charm. –  Shashank Sawant Nov 13 '12 at 4:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.