12

I was trying to make it on my own, but my skills aren't enough to solve it.

enter image description here

jub0bs
  • 58,916
  • 3
    There's the \cellcolor command of xcolor, why wouldn't you use that and ask for a TikZ solution? TikZ is not an universal saviour! Cf. http://tex.stackexchange.com/q/8891/11002 – yo' Jun 02 '14 at 12:02
  • @tohecz isn't that "the \cellcolor command of colortbl..." ? – Thruston Jun 02 '14 at 13:15
  • @Thruston Yes, it seems so. – yo' Jun 02 '14 at 13:20
  • @tohecz: for the particular case, also TikZ seems to be fine and on purpose as the structure can be treated as two separate matrices. – Claudio Fiandrino Jun 02 '14 at 13:37

3 Answers3

28

TikZ has one advantage here. Once appropiate styles are defined, the syntax of the matrix can be as easy as:

\matrix[options...]
{
Learning unit 1  & & &X&X&X& & \\
Learning unit 2  & & &X&X&X& & \\
Learning unit 3  &X&X&X& &X&X& \\
Learning unit 4  & & &X&X&X&X& \\
Learning unit 5  & & &X&X&X& & \\
Learning unit 6  & & &X&X&X&X&X\\
Learning unit 7  &X&X&X&X&X&X&X\\
Learning unit 8  &X&X& & &X&X& \\
Learning unit 9  &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};

The trick is to use column 2, column 3, etc styles that set the background color and foreground color to the desired "block" color, so that those "X" are not visible.

This is the complete code:

\usetikzlibrary{matrix}
\begin{tikzpicture}

% Define appropiate colors for each block
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}

% This style sets the background and foreground color to #1,
% and fixes the size of the "X" cells
\tikzset{
  block/.style={#1, nodes={fill=#1, minimum height=4.5mm, minimum width=4.5mm}}
}

% Now the matrix
\matrix (M) [
  inner sep=0pt,
  matrix of nodes,
  nodes={inner sep=0pt},
  column 1/.style={nodes={ % Column 1 is different
              text width=4cm, minimum height=4mm, inner sep=2pt,
              font=\sffamily\scriptsize,
           }},
  column 2/.style={block=block1},
  column 3/.style={block=block2},
  column 4/.style={block=block3},
  column 5/.style={block=block4},
  column 6/.style={block=block5},
  column 7/.style={block=block6},
  column 8/.style={block=block7},
] 
{
Learning unit 1  & & &X&X&X& & \\
Learning unit 2  & & &X&X&X& & \\
Learning unit 3  &X&X&X& &X&X& \\
Learning unit 4  & & &X&X&X&X& \\
Learning unit 5  & & &X&X&X& & \\
Learning unit 6  & & &X&X&X&X&X\\
Learning unit 7  &X&X&X&X&X&X&X\\
Learning unit 8  &X&X& & &X&X& \\
Learning unit 9  &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};

% Now draw the row lines
\foreach \i in {1,...,14} 
  \draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
% And the frame around the matrix
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}

Result:

Table

Update

The style for the column 1 can be moved to a new tikz style, which allows us to reuse it for the other "lengend" table.

This is the complete code for both tables:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}

\tikzset{
  block/.style={#1, nodes={fill=#1, minimum height=4.5mm, minimum width=4.5mm}},
  headings/.style= {nodes={
              text width=4cm, minimum height=4mm, inner sep=2pt,
              font=\sffamily\scriptsize}
    },
}

\begin{tikzpicture}
\matrix (M) [          % Legend table
   inner sep = 0pt,
   matrix of nodes,
   nodes={inner sep=0pt},
   column 1/.style=headings,
   row 1 column 2/.style={block=block1},
   row 2 column 2/.style={block=block2},
   row 3 column 2/.style={block=block3},
   row 4 column 2/.style={block=block4},
   row 5 column 2/.style={block=block5},
   row 6 column 2/.style={block=block6},
   row 7 column 2/.style={block=block7},
] {
Block 1. Lorem ipsum  &X\\
Block 2. Lorem ipsum  &X\\
Block 3. Lorem ipsum  &X\\
Block 4. Lorem ipsum  &X\\
Block 5. Lorem ipsum  &X\\
Block 6. Lorem ipsum  &X\\
Block 7. Lorem ipsum  &X\\
};

\foreach \i in {1,...,6} 
  \draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}
%
\hskip 1cm
%
\begin{tikzpicture}  % Second table
\matrix (M) [
  inner sep=0pt,
  matrix of nodes,
  nodes={inner sep=0pt},
  column 1/.style=headings,
  column 2/.style={block=block1},
  column 3/.style={block=block2},
  column 4/.style={block=block3},
  column 5/.style={block=block4},
  column 6/.style={block=block5},
  column 7/.style={block=block6},
  column 8/.style={block=block7},
] 
{
Learning unit 1  & & &X&X&X& & \\
Learning unit 2  & & &X&X&X& & \\
Learning unit 3  &X&X&X& &X&X& \\
Learning unit 4  & & &X&X&X&X& \\
Learning unit 5  & & &X&X&X& & \\
Learning unit 6  & & &X&X&X&X&X\\
Learning unit 7  &X&X&X&X&X&X&X\\
Learning unit 8  &X&X& & &X&X& \\
Learning unit 9  &X&X& & &X&X&X\\
Learning unit 10 &X&X& & &X&X& \\
Learning unit 11 &X& & & &X& & \\
Learning unit 12 & & & &X&X&X&X\\
Learning unit 13 & & & &X& &X&X\\
Learning unit 14 &X& & &X&X& & \\
Learning unit 15 &X& & &X&X& & \\
};

\foreach \i in {1,...,14} 
  \draw[gray] (M-\i-1.south west) -- (M-\i-1.south-|M.east);
\draw[thick] (M.south west) rectangle (M.north east);
\end{tikzpicture}
\end{document}

Result:

enter code here

JLDiaz
  • 55,732
  • Instead of drawing a rectangle after matrix, it's enough to use draw, thick inside matrix options. – Ignasi Jun 02 '14 at 21:56
12

Perhaps not as versatile as the answer from JLDiaz, but just as a non-tikz alternative:

\documentclass[border=5]{standalone}
\renewcommand\familydefault\sfdefault

\usepackage{xcolor}

\usepackage{array}
\setlength\extrarowheight{1ex}

\usepackage{colortbl}
\arrayrulecolor{gray}

\newcount\blockcount

\def\blocks#1{\blockcount=0 \doblocks#1@}
\def\doblocks#1{%
 \ifx#1@%
   \hskip.5\extrarowheight{}% 
 \else%
   \advance\blockcount by1 %
   \ifx#1x%
     \doblock{block\the\blockcount}%
   \else%
     \doblock{white}%
   \fi%
   \expandafter\doblocks%
 \fi}

\def\doblock#1{%
  \textcolor{#1}{\raisebox{-.5\extrarowheight}{\vrule width 3ex height3ex depth0.0ex}}%
}
\colorlet{block1}{green!60!yellow!80!black}
\colorlet{block2}{blue!80!red!70!black}
\colorlet{block3}{orange}
\colorlet{block4}{blue!50!cyan}
\colorlet{block5}{orange!50!red}
\colorlet{block6}{yellow}
\colorlet{block7}{black!30}

\begin{document}
\begin{tabular}{|p{2in}l@{}|}\hline
Learning unit  1 & \blocks{..xxx..} \\\hline
Learning unit  2 & \blocks{..xxx..} \\\hline
Learning unit  3 & \blocks{xxx.xx.} \\\hline
Learning unit  4 & \blocks{..xxxx.} \\\hline
Learning unit  5 & \blocks{..xxx..} \\\hline
Learning unit  6 & \blocks{..xxxxx} \\\hline
Learning unit  7 & \blocks{xxxxxxx} \\\hline
Learning unit  8 & \blocks{xx..xx.} \\\hline
Learning unit  9 & \blocks{xx..xxx} \\\hline
Learning unit 10 & \blocks{xx..xx.} \\\hline
Learning unit 11 & \blocks{x...x..} \\\hline
Learning unit 12 & \blocks{...xxxx} \\\hline
Learning unit 13 & \blocks{...x.xx} \\\hline
Learning unit 14 & \blocks{x..xx..} \\\hline
Learning unit 15 & \blocks{x..xx..} \\\hline
\end{tabular}
\end{document}

enter image description here

Mark Wibrow
  • 70,437
9

can be done with a simple tabular:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[table,dvipsnames,X11names]{xcolor}
\def\myBox{\rule[-1.5ex]{5ex}{5ex}}
\def\W{\phantom{\myBox}}
\def\G{\textcolor{green}{\myBox}}
\def\V{\textcolor{blue!100!red!80}{\myBox}}
\def\B{\textcolor{blue}{\myBox}}
\def\O{\textcolor{orange}{\myBox}}
\def\R{\textcolor{red}{\myBox}}
\def\Y{\textcolor{yellow}{\myBox}}
\def\g{\textcolor{gray}{\myBox}}
\begin{document}
{\fboxsep=0pt\fboxrule=1pt
\fbox{%
\arrayrulecolor{black!20}\arrayrulewidth=0.8pt%
\begin{tabular}{ >{Learning unit }p{0.4\linewidth}l}
1 & \W\W\O\B\R\W\W \\\hline
2 & \W\W\O\B\R\W\W \\\hline
3 & \G\V\O\W\R\Y\W \\\hline
4 & \W\W\O\W\R\Y\W \\\hline
5 & \W\W\O\W\R\W\W \\\hline
6 & \G\V\O\W\R\Y\g \\\hline
7 & ...
\end{tabular}}

\end{document}

enter image description here

If you like to play with active characters you can simplify the tabluar:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{libertine}
\usepackage[table,dvipsnames,X11names]{xcolor}
\def\mybox{\rule[-1.5ex]{5ex}{5ex}}
\begingroup
  \makeatletter
  \catcode`\W=\active \catcode`\G=\active 
  \catcode`\V=\active \catcode`\B=\active 
  \catcode`\O=\active \catcode`\R=\active 
  \catcode`\Y=\active \catcode`\K=\active 
  \@firstofone{\endgroup
    \newenvironment{makeActive}{%
      \catcode`\W=\active \catcode`\G=\active 
      \catcode`\V=\active \catcode`\B=\active 
      \catcode`\O=\active \catcode`\R=\active 
      \catcode`\Y=\active \catcode`\K=\active 
      \def W{\phantom{\mybox}}%   white box
      \def G{\textcolor{green}{\mybox}}%
      \def V{\textcolor{blue!100!red!80}{\mybox}}%
      \def B{\textcolor{blue}{\mybox}}%
      \def O{\textcolor{orange}{\mybox}}%
      \def R{\textcolor{red}{\mybox}}%
      \def Y{\textcolor{yellow}{\mybox}}%
      \def K{\textcolor{gray}{\mybox}}%
    }{}%
  }
\makeatother
\begin{document}

\begin{makeActive}
\fboxsep=0pt\fboxrule=1pt
\fbox{\arrayrulecolor{black!20}\arrayrulewidth=0.8pt%
\begin{tabular}{ >{Learning unit }p{0.4\linewidth}l}
1 & WWOBRWW \\\hline
2 & WWOBRWW \\\hline
3 & GVOWRYW \\\hline
4 & WWOWRYW \\\hline
5 & WWOWRWW \\\hline
6 & GVOWRYK 
%7 & ...
\end{tabular}}
\end{makeActive}

\end{document}