4

Is it possible to draw an \fcolorbox with multiple colors inside it?

I want to draw sort of a "legend" in my beamer plot, which in the end would look something like this:

colors

(Created with Windows Paint and MS Word)

I am familiar with fcolorbox, but can't figure out how to paint more than one color inside it.

\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}

Any advise would be helpful!

7

enter image description here

\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}
4

By coincidence, @egreg's answer yesterday ( How to construct a macro with key=value by \pgfkeys) dovetails with this one.

Adapting that answer very slightly:

custom colorbox

MWE

\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}
2

Pursuing David's idea, but letting TeX do the calculations…

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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