6

I would like to learn how can I create on LaTeX a table like this: enter image description here

Please consider that the content of cells 9,17,25 and 13,21,29 are left aligned, not centered. Also, the width of border lines doesn't matter.

Until now, I have managed to do only this:

\documentclass[11pt,a4paper]{book}
  \usepackage[table]{xcolor}
  \begin{document}
  \begin{center}
  \begin{tabular}{c|ccc|}
   \arrayrulecolor{orange}\hline
   \cline{2-4}
   \multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3 & \cellcolor{green} 4 \\ \hline
   \multicolumn{1}{|c|}{\cellcolor{red} 9} & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
   \multicolumn{1}{|c|}{\cellcolor{red} 17} & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
   \multicolumn{1}{|c|}{\cellcolor{red} 25} & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
\end{tabular}
\end{center}
\end{document}

which has this result:

enter image description here

I would appreciate very much if you could explain a little bit your code, because I need to create more tables like this (thankfully, simpler ones).

Thank you in advance!

bstex
  • 95
  • 6
  • 1
    Welcome! OK, I've no idea why you'd want to produce that but, given that you do, what have you got so far? Please provide a Minimal Working Example in the form of compilable code for the table you've got at the moment. – cfr Jun 18 '16 at 23:14
  • @cfr Thank you for your reply. I edited my post with the information you asked. – bstex Jun 19 '16 at 06:32
  • Thanks for that. Could you make it compilable by adding a minimal preamble and the document environment? That makes it easier for people to help as they don't have to start by completing your code and guessing which packages etc. it needs. – cfr Jun 19 '16 at 12:27
  • Use l instead of c to align cells left. Remove the first \hline to avoid the line being drawn above the top left cell. – cfr Jun 19 '16 at 12:31
  • I added the preamble as you said, sorry for that. Thank you both cfr and Bernard for your help. I'm going to dive into your code and create my tables. Thank you again! – bstex Jun 19 '16 at 16:41

4 Answers4

3

Here is a solution, playing with the parameters of booktabs:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} \usepackage{gillius2} \usepackage{geometry}
\usepackage[table, x11names]{xcolor}
\usepackage{booktabs, hhline} \usepackage{etoolbox}
\newcommand\thickvrule[1][DarkOrange2]{\ifblank{#1}{}{\color{#1}}\vrule width 2pt}


\begin{document}
\begin{table}
\sffamily\bfseries%
\centering\arrayrulecolor{DarkOrange2}
\setlength\aboverulesep{0pt}\setlength\belowrulesep{0pt}
\setlength\cmidrulekern{2pt}\setlength\cmidrulewidth{2pt}
\renewcommand\arraystretch{1.6}\setlength\tabcolsep{10pt}
\begin{tabular}{!{\thickvrule}>{\columncolor{Firebrick3!75}\hskip-3pt}p{5mm}!{\thickvrule}cc!{\thickvrule[Plum4]}c!{\thickvrule[Plum4]}% left table
p{2em}% emptycolumn
!{\thickvrule}>{\columncolor{Firebrick3!75}\hskip-3pt}p{5mm}!{\thickvrule} cc!{\thickvrule[Plum4]}c!{\thickvrule[Plum4]}}% right table
\arrayrulecolor{Plum4}
\cmidrule(l{-2pt}){4-9}
\multicolumn{3}{c!{\thickvrule[Plum4]}}{} & \multicolumn{6}{c!{\thickvrule[Plum4]}}{} \\[-3ex]
\cmidrule(l{-2pt}){5-8}
\arrayrulecolor{DarkOrange2}
\multicolumn{3}{c!{\thickvrule[Plum4]}}{} & \multicolumn{1}{c!{\thickvrule[Plum4]}}{} & \multicolumn{4}{c!{\thickvrule[Plum4]}}{} & \multicolumn{1}{c!{\thickvrule[Plum4]}}{} \\[-2ex]
\cmidrule(l{-2pt}r){2-3}\cmidrule(r){4-4}
\cmidrule(l{-2pt}r){7-8}
\cmidrule(r){9-9}
\rowcolor{OliveDrab4!60}\multicolumn{1}{c!{\thickvrule}}{\cellcolor{white}1} & 2 & 3 & 4 &\multicolumn{1}{c}{\cellcolor{white}} & \multicolumn{1}{c!{\thickvrule} }{\cellcolor{white}5} & 6 & 7 & 8 \\[-2.1pt]
\cmidrule(r){1-3} \cmidrule(r){4-4}\cmidrule(l{-2pt}r){6-8}\cmidrule(r){9-9}%
\arrayrulecolor{LightSkyBlue1}9 & 10 & 11 & 12 & & 13 & 14& 15 & 16 \\
\cline{2-4}\cline{7-9}
17 & 18 & 19 & 20 & & 21 & 22 & 23 & 24 \\
\cline{2-4}\cline{7-9}
\arrayrulecolor{DarkOrange2}
25 & 26 & 27 & 28 & & 29 & 30 & 31 & 32\\
\addlinespace[-2pt]
\cmidrule(r){1-3}\cmidrule(r){6-8}\addlinespace[-2pt]
\arrayrulecolor{Plum4}\cmidrule(r){4-4}\cmidrule(r){9-9}
\end{tabular}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
2

You can create something like the following

blue loop

using the following code. The code is annotated to explain it a bit, but you'll need to look at the manuals for tikzmark and/or TikZ for further details. The latter is very long, but you obviously don't need most of it and the bits used here are simple.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\begin{center}
  \begin{tabular}[b]{|l|ccc|} % |l| to left align the column with lines on both sides; we then override this for the exceptional top left cell rather than all the others
    \arrayrulecolor{orange}% remove \hline to avoid the line above the top left cell
    \cline{2-4}
    % \tikzmark{} is marking coordinates for later reference: a2, a1 etc.
    \multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3\tikzmark{a2} & \tikzmark{a1}\cellcolor{green} 4 \\ \hline
    \cellcolor{red} 9 & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
    \cellcolor{red} 17 & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
    \cellcolor{red} 25 & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
  \end{tabular}%
  \tikzmark{c}
  \hskip 1.5em
  \begin{tabular}[b]{|l|ccc|}
    \arrayrulecolor{orange}
    \cline{2-4}
    \multicolumn{1}{c|}{1} & \cellcolor{green} 2 & \cellcolor{green} 3\tikzmark{b2} & \tikzmark{b1}\cellcolor{green} 4 \\ \hline
    \cellcolor{red} 9 & 10 & 11 & 12 \\ \arrayrulecolor{blue}\hline
    \cellcolor{red} 17 & 18 & 19 & 20 \\ \arrayrulecolor{blue}\hline
    \cellcolor{red} 25 & 26 & 27 & 28 \\ \arrayrulecolor{orange}\hline
  \end{tabular}%
  \tikzmark{d}
\end{center}
% Now we use the marked coordinates to draw the blue shape over the top of the tables using a TikZ picture which is overlaid
\begin{tikzpicture}[remember picture,overlay]
  % the \tikzmark uses the `pic` coordinate system so we start by making some regular coordinates e.g. turning ({pic cs:c}) into (c), but this gets tedious so we use loops for extensibility
  % the a/b cases are special because we need to use a1, a2, b1 and b2 to get these positions using the `calc` library; we set a and b halfway between a1/a2 and b1/b2 respectively
  \foreach \i in {a,b} \coordinate (\i) at ($({pic cs:\i1})!1/2!({pic cs:\i2})$);
  % the c/d cases are straightforward
  \foreach \i in {c,...,d} \coordinate (\i) at ({pic cs:\i});
  % now we draw the loop using the coordinates we just defined: -- for straight; |- for up/down then across; -| for across then up/down
  \draw [draw=blue, thick] ([yshift=20pt]a -| c) coordinate (e) -- (c |- d) -| ([yshift=30pt]a) -| (d) -| (b |- e) -- cycle;
\end{tikzpicture}
\end{document}

Note that I wouldn't do this because I think the diagram is rather ugly and confusing. It looks like something you might make in Word if you'd just discovered how to use colour and were rather over enthusiastic about it.

For example, what do the different colours represent? The diagram uses red, orange, blue, green and none. What do each of this symbolise? I can see the green and red probably represent the column and row labels, although this is hardly necessary given that the location already represents this information far more clearly.

cfr
  • 198,882
0

Here is my approach, not even as elegant as other answers, but using TiKZ Matrix.

complicated matrix in tikz

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc} 
\usepackage{gillius2} % Stolen from Bernard answer
\renewcommand\familydefault{\sfdefault} 
\usepackage[dvipsnames]{xcolor} %always declare the xcolor package before tikz
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\bfseries
\begin{tikzpicture}
\tikzset{square matrix/.style={
    matrix of nodes,
    column sep=-\pgflinewidth, row sep=-\pgflinewidth,
    nodes={draw=none,
      minimum height=12pt,
      anchor=center,
      text width=#1,
    },
  },
  square matrix/.default=1.2cm
}

\matrix (first) [square matrix,
      column 1/.style={nodes={fill=BrickRed,draw=none}},
      column 2/.style={nodes={align=center}},
      column 3/.style={nodes={align=center}},
      column 4/.style={nodes={align=center}},     
      row 1/.style={nodes={draw=none,fill=white}},
      row 2/.style={nodes={draw=none,fill=white}},
      row 3/.style={nodes={fill=LimeGreen,draw=none}},
]
{
{} & {}  & {} & {} \\
{} & {}  & {} & {} \\
|[draw=none,fill=white,align=center]| 1 & 2 & 3 & 4 \\
9 & 10 & 11 & 12 \\
17 & 18 & 19 & 20 \\
25 & 26 & 27 & 28 \\
};

\matrix (second) [right=of first,square matrix,
      column 1/.style={nodes={fill=BrickRed,draw=none}},
      column 2/.style={nodes={align=center}},
      column 3/.style={nodes={align=center}},
      column 4/.style={nodes={align=center}},
      row 1/.style={nodes={draw=none,fill=white}},
      row 2/.style={nodes={draw=none,fill=white}},
      row 3/.style={nodes={fill=LimeGreen,draw=none}},
]
{
{} & {}  & {} & {} \\
{} & {}  & {} & {} \\
|[draw=none,fill=white,align=center]| 5 & 6 & 7 & 8 \\
13 & 14 & 15 & 16 \\
21 & 22 & 23 & 24 \\
29 & 30 & 31 & 32 \\
};
\draw[blue] (first-4-2.south west) -- (first-4-4.south east);
\draw[blue] (first-5-2.south west) -- (first-5-4.south east);
\draw[blue] (second-4-2.south west) -- (second-4-4.south east);
\draw[blue] (second-5-2.south west) -- (second-5-4.south east);
%
\draw[YellowOrange, ultra thick]  (second-4-2.north west) -- (second-6-2.south west) -- (second-6-1.south west) -- (second-6-1.south west) -- (second-3-1.south west) -- (second-3-2.south west) ;
\draw[YellowOrange, ultra thick] (second-4-2.north west) -- (second-3-2.north west) -- (second-3-4.north east) -- (second-4-4.north east) -- (second-3-2.south west) ;
\draw[YellowOrange, ultra thick] (second-6-2.south west) -- (second-6-4.south west);
%
\draw[YellowOrange, ultra thick]  (first-4-2.north west) -- (first-6-2.south west) -- (first-6-1.south west) -- (first-6-1.south west) -- (first-3-1.south west) -- (first-3-2.south west) ;
\draw[YellowOrange, ultra thick] (first-4-2.north west) -- (first-3-2.north west) -- (first-3-4.north east) -- (first-4-4.north east) -- (first-3-2.south west) ;
\draw[YellowOrange, ultra thick] (first-6-2.south west) -- (first-6-4.south west);
%
\draw[Violet,ultra thick] (first-6-4.south east) -- (first-6-4.south west) -- (first-1-4.north west) -- (second-1-4.north east) -- (second-6-4.south east) --  (second-6-4.south west) -- (second-1-4.south west) -- (first-1-4.south east) -- (first-6-4.south east) -- (first-6-4.south west);
\end{tikzpicture}
\end{document}

My code was partially adapted from the following question/answers : Color of square cell in array, TikZ matrix as a replacement for tabular, Big block matrix with tikz.

G. Bay
  • 2,047
0

Here is a solution with {NiceTabular} of nicematrix and TikZ.

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\renewcommand{\arraystretch}{1.3} \newcommand{\BlueLine}{\Hline[tikz={blue,line width=0.4pt}]} \newcommand{\MyBlock}{\Block[draw=orange,line-width=1pt]}

\begin{NiceTabular}{w{l}{6mm}ccc}[name=A] \Block[c]{}{1} & \MyBlock[fill=green!20]{1-3}{} 2 & 3 & 4\ \MyBlock[fill=red!30]{3-1}{} 9 & \MyBlock[transparent]{3-3}{} 10 & 11 & 12 \ \BlueLine 17 & 18 & 19 & 20 \ \BlueLine 25 & 26 & 27 & 18
\end{NiceTabular} \qquad \begin{NiceTabular}{w{l}{6mm}ccc}[name=B] \Block[c]{}{5} & \MyBlock[fill=green!20]{1-3}{} 6 & 7 & 8\ \MyBlock[fill=red!30]{3-1}{} 13 & \MyBlock[transparent]{3-3}{} 14 & 15 & 16 \ \BlueLine 21 & 22 & 23 & 24 \ \BlueLine 29 & 30 & 31 & 32 \end{NiceTabular} % \begin{tikzpicture}[remember picture, overlay] \draw [line width=1.2 pt, purple] (A-5-|A-4) -| ([yshift=2mm]A-1-|A-5) -| (B-5-|B-4) -| ([yshift=3mm]B-1-|B-5) -| cycle ; \end{tikzpicture}

\end{document}

Output of the above code

F. Pantigny
  • 40,250