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.

I need to build a table of this type:

STUFF | COLOR | PICTURE | COLOR DEFINITION

To retrieve the color definition, I tried to use \extractcolorspecs expecting that the third argument { color-cmd } would allowed me to print what I needed. The problem is that I defined colors in HTML, while the command gives me RGB format in output. Here is a MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{booktabs}
\usepackage{xcolor}

\definecolor{mapblue}{HTML}{0000FF}%
\definecolor{maproyalblue}{HTML}{0088FF}%
\definecolor{mapskyblue}{HTML}{00CCFF}%
\definecolor{mapgreen}{HTML}{00CC00}%
\definecolor{mapdandelion}{HTML}{FFCC00}%
\definecolor{maporange}{HTML}{FF8800}%
\definecolor{mapred}{HTML}{FF0000}%

\newcommand{\colpic}[1]{\tikz \draw[#1,fill=#1,draw](0,0)circle(0.1cm);}
\newcommand{\colrow}[1]{#1 & \colpic{#1} & \extractcolorspecs{#1}{\model}{\mycolor}\mycolor}

\begin{document}

\begin{tabular}{cccc}
\toprule
\multicolumn{1}{c|}{\textsc{Relevance Interval}} &
\multicolumn{1}{c|}{\textsc{Color}} & 
\multicolumn{1}{c|}{\textsc{Picture}} & 
\multicolumn{1}{c}{\textsc{ Color Definition}}\\
\midrule 
0 - 5 & \colrow{mapblue}\\
5 - 10 & \colrow{maproyalblue}\\
10 - 25 & \colrow{mapskyblue}\\
25 - 50 & \colrow{mapgreen}\\
50 - 100 & \colrow{mapdandelion}\\
100 - 200 & \colrow{maporange}\\
>200 & \colrow{mapred}\\
\bottomrule
\end{tabular}

\end{document}

and this is the output: enter image description here

How is it possible to obtain the color definition in HTML?

share|improve this question
3  
There is also a command \convertcolorspec in the xcolor manual for conversion. –  percusse Apr 9 '12 at 11:24
    
Thanks! Now it works. Should I write as answer the solution? –  Claudio Fiandrino Apr 9 '12 at 11:31
2  
@percusse Please, expand your comment into an answer –  egreg Apr 9 '12 at 12:46
    
@egreg Thanks, done... –  percusse Apr 9 '12 at 13:53
add comment

1 Answer

up vote 16 down vote accepted

We can convert the stored version of the color definition (which is expressed in rgb) to HTML with

\newcommand{\colrow}[1]{%
  #1 & \colpic{#1} & \extractcolorspecs{#1}{\model}{\mycolor}%
    \convertcolorspec{\model}{\mycolor}{HTML}\tmp\tmp
}

Also we can squeeze in the definitions of the commands like \somecolorhtml with some extra coding.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz,booktabs,xcolor}
\newcommand{\definemycolor}[2]{%
        \definecolor{#1}{HTML}{#2}%
        \expandafter\def\csname#1html\endcsname{#2\relax} %Which defines \colornamehtml
        }
\newcommand{\colpic}[1]{\tikz \draw[#1,fill=#1,draw](0,0)circle(0.1cm);}
\newcommand{\colrow}[1]{#1 & \colpic{#1} &\csname#1html\endcsname}
\definemycolor{mapblue}{0000FF}%
\definemycolor{maproyalblue}{0088FF}%
\definemycolor{mapskyblue}{00CCFF}%
\definemycolor{mapgreen}{00CC00}%
\definemycolor{mapdandelion}{FFCC00}%
\definemycolor{maporange}{FF8800}%
\definemycolor{mapred}{FF0000}%
\begin{document}
\begin{tabular}{cccc}
\toprule
\multicolumn{1}{c|}{\textsc{Relevance Interval}} &
\multicolumn{1}{c|}{\textsc{Color}} & 
\multicolumn{1}{c|}{\textsc{Picture}} & 
\multicolumn{1}{c}{\textsc{ Color Definition}}\\
\midrule 
0 - 5 & \colrow{mapblue}\\
5 - 10 & \colrow{maproyalblue}\\
10 - 25 & \colrow{mapskyblue}\\
25 - 50 & \colrow{mapgreen}\\
50 - 100 & \colrow{mapdandelion}\\
100 - 200 & \colrow{maporange}\\
>200 & \colrow{mapred}\\
\bottomrule
\end{tabular}
Since we defined the \verb|\colornamehtml| command versions of the colors,
\verb|\mapbluehtml| gives \textcolor{mapblue}{\mapbluehtml}  and 
\verb|\mapdandelionhtml| gives \textcolor{mapdandelion}{\mapdandelionhtml}.
\end{document}

enter image description here

Also, I think a monospaced font would please more in the last column so replacing the colrow command with the following

\newcommand{\colrow}[1]{#1 & \colpic{#1} &\expandafter\texttt{\csname#1html\endcsname}}

would lead to

enter image description here

share|improve this answer
add comment

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.