1

I would like to know how to go about coloring the boxes that are equal 1 in blue.

Code:

\documentclass[11pt]{book}

\begin{document}

\begin{center}
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|}
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline
\end{tabular}
\end{center}

\end{document} 
Joe
  • 9,080
  • have a look at http://tex.stackexchange.com/questions/174234/latex-tables-cell-value-color-based-on-its-sign-conditional-cell-color#174359 – Runar Sep 15 '16 at 13:10

2 Answers2

5

The same as here: LaTeX Tables: Cell value color based on its sign / conditional cell Color. You can create new columns which verify the cell's Content and fill it if the condition is met, in this case it's looking for only the first token, so if you have a cell with 11 on it it will be highlighted.

\documentclass[11pt]{book}

\usepackage{array}
\usepackage{colortbl}

\makeatletter
\newcommand*{\minuscellcolor}{}
\def\minuscellcolor\ignorespaces{%
  % \ignorespaces not really needed, because \@ifnextchar gobbles spaces
  \@ifnextchar1{\cellcolor{red}}{}%
}
\newcolumntype{L}{>{\minuscellcolor}l}
\newcolumntype{C}{>{\minuscellcolor}c}
\newcolumntype{R}{>{\minuscellcolor}r}
\makeatother


\begin{document}

\begin{center}
\begin{tabular}{|C|C|C|C|C|C|C|C|C|C|}
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 0 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 0 \\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
\hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
\hline
\end{tabular}
\end{center}

\end{document}

The result is:

enter image description here

  • thank you for your solution. How do I go about making it local to just one table? I have multiple tabaular, but need this color on only one table? Thanks. – Joe Sep 15 '16 at 16:13
  • 1
    This solution implements the column specifier C, which is different than the column specifier c, so only the tables using the former will be formatted as such. Therefore, just use the normal c and everything should be fine. – Guilherme Zanotelli Sep 15 '16 at 17:27
2

Here is a luatex based solution. I use ConTeXt, but it could be easily translated to LaTeX:

\startluacode
    userdata = userdata or {}
    local function setcolor(s)
        return ("backgroundcolor=" .. s)
    end
    function userdata.coloredtable( tab, colors)
      table.print(tab)
      context.bTABLE{"setups=coloredtable"}
        for y = 1, #tab do
          context.bTR()
            local row = tab[y]
            table.print(row)
            for x = 1, #row do
              local content = row[x]
              local settings = nil
              for match,color in pairs(colors) do
                if content == match then
                  settings  = setcolor(color)
                end
              end
              context.bTD{settings}
              context(content)
              context.eTD()
            end
          context.eTR()
        end
      context.eTABLE()
    end
\stopluacode

\startsetups coloredtable
  \setupTABLE[each][each][frame=off, background=color]
\stopsetups

\define[2]\coloredtable
  {\ctxlua{userdata.coloredtable({#2}, {#1})}}

\starttext
\coloredtable{[1] = "red", [2] = "blue"}{ {1,2,3},{3,2,1} }

\stoptext

It creates a macro \coloredtable that takes two arguments: the first is the list of colors to give to matches (if the content is 1, color red; if it is 2, color blue, etc.); the second is the content of the table which is input as a lua table.

The result is

enter image description here

Aditya
  • 62,301