4

I would like to make partially colored cells as is shown in the answers here Partially coloring cell background with histograms.

TikZ example does not work for me and I would rather use the answer without TikZ.

The problem I have is that I have column headers and then the coloring does not start at the left border of the cell any more.

Here is an minimal working example I have made from the answer above:

\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\newlength{\maxlen}
\newcommand{\databar}[3][green!50]{%
  \settowidth{\maxlen}{#2}%
  \addtolength{\maxlen}{\dimexpr2\tabcolsep-\arrayrulewidth}%
  \FPeval\result{round(#3/#2:4)}%
  \rlap{\color{#1}\hspace*{\dimexpr-\tabcolsep+.5\arrayrulewidth}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}%
  \makebox[\dimexpr\maxlen-2\tabcolsep+\arrayrulewidth][r]{#3}%
}
\begin{document}

\begin{tabular}{ | l | r |}
  \hline
  Name & Value column which is much wider \\
  \hline
  name1 & \databar{10.0}{1} \\
  name2 & \databar{10.0}{2} \\
  name3 & \databar{10.0}{3} \\
  name4 & \databar{10.0}{4} \\
  name5 & \databar{10.0}{5} \\
  name6 & \databar{10.0}{6} \\
  name7 & \databar{10.0}{8} \\
  name8 & \databar{10.0}{8} \\
  name9 & \databar{10.0}{9} \\
  name10 & \databar{10.0}{10} \\
  \hline
\end{tabular}
\end{document}

Wide column

I would like the bars to extend over the whole column and not just on the right side of it. How can I do that? Thanks!

  • For one thing, change the column alignment of the second column from r to l. The other thing is that the column width is driven by the column title width, not the data. – Steven B. Segletes Dec 29 '16 at 14:13
4
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage[nomessages]{fp}
\newlength\maxlen
\newcommand\databar[3][green!50]{%
  \FPeval\result{round(#3/#2:4)}%
  \rlap{\textcolor{#1}{\hspace*{\dimexpr-\tabcolsep+.5\arrayrulewidth}%
        \rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}}%
  \makebox[\dimexpr\maxlen-2\tabcolsep+\arrayrulewidth][r]{#3}}
\def\header{Value column which is much wider}
\settowidth\maxlen{\header}
\addtolength\maxlen{\dimexpr2\tabcolsep-\arrayrulewidth}
\begin{document}

    \begin{tabular}{ | l | l |}
        \hline
        Name & \header \\
        \hline
        name1 & \databar{10.0}{1} \\
        name2 & \databar{10.0}{2} \\
        name3 & \databar{10.0}{3} \\
        name4 & \databar{10.0}{4} \\
        name5 & \databar{10.0}{5} \\
        name6 & \databar{10.0}{6} \\
        name7 & \databar{10.0}{8} \\
        name8 & \databar{10.0}{8} \\
        name9 & \databar{10.0}{9} \\
        name10 & \databar{10.0}{10} \\
        \hline
    \end{tabular}
\end{document}

enter image description here

  • Now I see that the width of actual text is taken to get the width of the cells. What if I want to use two or more columns of different widths to display the databars? – nobody Dec 29 '16 at 16:32
  • eg.: \def\header{Name\hspace{2\tabcolsep}Value column which is much wider}. For a p-column you can set it to the width of the p column – user2478 Dec 29 '16 at 16:54

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.