# How to draw multiple colored bars in a table?

For a calendar, I would like to draw multiple lines indicating things like school breaks into a table. A single calendar day is already multiple cells in the table, so I thought using \arrayrulecolor{red}\cline{1-5} for that, but the problem is that \cline does not stack like \hline and therefore there can only be one line on a given day.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[dvipsnames,table]{xcolor}   % »table« optons loads »colortbl« package

\begin{document}
\begin{tabular}{|ccc|ccc|}\hline
& Monday &  &  & Thuesday & \\
\hline\hline
1&2&3&4&5&6 \\
\cline{2-5}\arrayrulecolor{green} \cline{2-3}\arrayrulecolor{black}
&&&&&&
\hline
\end{tabular}
\end{document}


The second (green) \cline{2-3} overwrites the first \cline{2-5} for the first two cells, instead of being positioned below the first like the second \hline below the header.

(I am actually not too attached to the \cline solution, it just seems to be the most obvious way to format the calendar.)

• There's a TikZ calendar library which might work out for your use case. – TH. Mar 19 '18 at 1:34

One option is to use a tikz matrix of nodes. it is possible to use fairly intuitive syntax so that you can produce something like:

using code like:

\documentclass[a4paper]{article}
\usepackage[dvipsnames,table]{xcolor}   % »table« optons loads »colortbl« package
\usepackage{tikz}
\usetikzlibrary{matrix}
\parindent=0pt

\begin{document}

\begin{tikzpicture}[
G/.append style = {fill=ForestGreen!10},
R/.append style = {fill=Red!10, text=Red},
V/.append style = {fill=BlueViolet!10},
]
\matrix (M)[matrix of nodes, nodes in empty cells,
every node/.style = {anchor=base,text depth=.5ex,text height=2ex,
text width=20mm, rectangle, draw=SkyBlue},
row 1/.style = {every node/.append style={fill=SkyBlue!20},
font=\bfseries, Blue, minimum height=2em},
]{%
Monday       & Tuesday & Wednesday & Thursday   & Friday & Saturday & Sunday\\
|[G]|Holiday & Work    & Work      & |[R]| Sick & Work   &  Work& Work \\
|[V]|Meeting & Lunch   & Work      & |[R]| Sick & Work   &  Work& Work \\
};
\end{tikzpicture}

\end{document}


The G, R and V styles are examples that to show you how to define "shorthands" that add particular styling to certain cells. Of course, many things are possible. I don't know the tikz calendar library referred in the comments, so perhaps it gives a nicer way of doing this.

I think that the styling is "better" than what you were asking for, but if instead of shading you prefer using coloured lines this is easy to do using tikz. The M in the \matrux(M)... allows you to refer to the cells in the matrix by their row and column indices so, for example (M-2-1) is the cell in row 2 and column 1. As a slight variation of the above, you can produce

\draw[red, thick](M-2-1.south east)--(M-2-2.south east);
\draw[blue, thick](M-3-2.south east)--(M-3-3.south east);


a the bottom of the tikzpicture environment.

Edit

I quite like this code so I wanted to extend this idea and make it into an environment. Unfortunately, because of the inner working of the \matrix of nodes this means that the & column separators need to be replaced by \&. I also added some trickery for underlining cells that uses the same syntax for changing the background colour of a cell. With these changes, the code

\begin{Table}
Monday       \& Tuesday \& Wednesday \& Thursday   \& Friday  \\
|[G]|Holiday \& Work    \& Work      \& |[R]| Sick \& Work    \\
|[V]|Meeting \& Lunch   \&|[b,V]|Work\& |[R]| Sick \&|[b]|Work\\
\end{Table}


now produces

As the example shows, it is possible to combine different styles.

Here is the revised code:

\documentclass[a4paper]{article}
\usepackage[dvipsnames,table]{xcolor}   % »table« optons loads »colortbl« package
\usepackage{tikz}
\usetikzlibrary{matrix}
\parindent=0pt
\usepackage{environ}
\usepackage{etoolbox}

\tikzset{
G/.append style = {fill=ForestGreen!10},
R/.append style = {fill=Red!10, text=Red},
blue underline/.style={blue, thick},
V/.append style = {fill=BlueViolet!10},
b/.code = {
\def\row{\the\pgfmatrixcurrentrow}
\def\col{\the\pgfmatrixcurrentcolumn}
\xappto\AfterThoughts{\noexpand\draw[blue underline](M-\row-\col.south west)--(M-\row-\col.south east);}
}
}
\NewEnviron{Table}{%
\def\AfterThoughts{}%
\tikzpicture
\matrix (M)[matrix of nodes, nodes in empty cells,
ampersand replacement=\&,
every node/.style = {anchor=base,text depth=.5ex,text height=2ex,
text width=20mm, rectangle, draw=SkyBlue},
row 1/.style = {every node/.append style={fill=SkyBlue!20},
font=\bfseries, Blue, minimum height=2em},
]{\BODY};\AfterThoughts\endtikzpicture}

\begin{document}

\begin{Table}
Monday       \& Tuesday \& Wednesday \& Thursday   \& Friday  \\
|[G]|Holiday \& Work    \& Work      \& |[R]| Sick \& Work    \\
|[V]|Meeting \& Lunch   \&|[b,V]|Work\& |[R]| Sick \&|[b]|Work\\
\end{Table}

\end{document}