4

I run into a simple problem but i cant finde a solution to it.. I want to create a matrix where all elements in a row have a equal width. Like this:

Aim of the table

My approach was 12 columns (+1 initial column) and then to work with multicolumns. The problem I get is that the rows are not scaled to the same width as in this picture.

enter image description here

I tried tabularx as in this question but it did not work as desired. (Using 12 times X as header) Table spacing, multi column

Can someone explain to me where the error is that tabularx doesn't work either?

Code for the simple tabel environment

\documentclass{article}
\begin{document}
\begin{table}[h]
\begin{tabular}{|c|cllcclclccll|}
\hline
N & \multicolumn{12}{c|}{Chance}                                                                                  \\ \hline
1 & \multicolumn{12}{c|}{100}                                                                                     \\ \hline
2 & \multicolumn{6}{c|}{50}                               & \multicolumn{6}{c|}{50}                               \\ \hline
3 & \multicolumn{4}{c|}{22}             & \multicolumn{4}{c|}{56}            & \multicolumn{4}{c|}{22}            \\ \hline
4 & \multicolumn{3}{c|}{12.5} & \multicolumn{3}{c|}{37.5} & \multicolumn{3}{c|}{37.5} & \multicolumn{3}{c|}{12.5} \\ \hline
\end{tabular}
\end{table}
\end{document}
2
  • Welcome to TeX.SE! Please make your code snippet be compilable, then we do not have to guess what you are doing ...
    – Mensch
    Commented Feb 7 at 16:27
  • Edited to be compilable
    – JonasM
    Commented Feb 7 at 17:25

3 Answers 3

4

With fixed width columns:

\documentclass{article}
\usepackage{array}

\newlength{\blockwidth}

\begin{document}

\begin{table}[htp]
\centering

% local settings
\setlength{\blockwidth}{0.5cm}
\newcommand{\block}[2]{% #1 = number of blocks to span, #2 = text
  \multicolumn{#1}{w{c}{\dimeval{#1\blockwidth+10\arrayrulewidth*\f{#1}/#1}}|}{#2}%
}
\newcommand{\f}[1]{\inteval{4*(#1-3)}}
%%%

\begin{tabular}{|>{$\vphantom{\bigg|}}c<{$}|*{12}{c}}
\hline
N & \block{12}{Chance} \\ \hline
1 & \block{12}{100} \\ \hline
2 & \block{6}{50} & \block{6}{50} \\ \hline
3 & \block{4}{22} & \block{4}{56} & \block{4}{22} \\ \hline
4 & \block{3}{12.5} & \block{3}{37.5} & \block{3}{37.5} & \block{3}{12.5} \\ \hline
\end{tabular}

\caption{A caption}

\end{table}

\end{document}

enter image description here

Probably a TikZ based approach is better, for this case. Or maybe even a tree rather than a table.

1
  • Thanks, that was what i was looking for. TikZ would have been a good idear i didn't think of. You got any idear why tabularx with somthing on the lines of "\begin{tabularx}{5cm}{|X|X|...|X|X|}" didnt work?
    – JonasM
    Commented Feb 7 at 18:31
3

Just for fun:

\multicolumn puts all the "extra" space into the last column used, and a lot of these columns are never used individually.

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}[h]
\begin{tabularx}{\textwidth}{|c|XXXXXXXXXXXX|}
\hline
 &&&&&&&&&&&& \\[-\normalbaselineskip]% hidden
N & \multicolumn{12}{c|}{Chance}                                                                                  \\ \hline
1 & \multicolumn{12}{c|}{100}                                                                                     \\ \hline
2 & \multicolumn{6}{c|}{50}                               & \multicolumn{6}{c|}{50}                               \\ \hline
3 & \multicolumn{4}{c|}{22}             & \multicolumn{4}{c|}{56}            & \multicolumn{4}{c|}{22}            \\ \hline
4 & \multicolumn{3}{c|}{12.5} & \multicolumn{3}{c|}{37.5} & \multicolumn{3}{c|}{37.5} & \multicolumn{3}{c|}{12.5} \\ \hline
\end{tabularx}
\end{table}
\end{document}
0

I'm not sure if this is what you mean, but if you want the boxes to be proportional in size to the numbers, you could use TikZ chains. Basically, the first column is a chain called n going below, and each numbered row is a chain going right. There is no error checking, so your rows could be shorter or longer than 100.

You can change the value of \widthfactor to adjust the width of the table.

enter image description here

There is also an optional argument that can set TikZ options for the entire table, for example:

\chancetable[thick, orange, text=blue]{{100},{50,50},{22,56,22},{12.5,37.5,37.5,12.5}}

would work.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{chains}
\newcommand{\widthfactor}{1}

\tikzset{rect/.style={draw, minimum width=#1, minimum height=5mm, inner sep=0pt}}

\newcommand{\chancetable}[2][]{\tikz[node distance=-\pgflinewidth, #1]{
    \node[rect=\widthfactor*5mm, start chain=n going below](N0){N}; \node[rect=\widthfactor*100mm, right=of N0]{Chance};
    \foreach \r[count=\n, evaluate=\n as \k using {int(\n-1)}] in {#2} {
        \node[rect=\widthfactor*5mm, below=of N\k, start chain=\n, on chain=\n](N\n){\n};
        \foreach \p in \r {\node[on chain=\n, rect=\widthfactor*\p mm]{\p};} 
    }
}}

\begin{document}

\chancetable{{100},{50,50},{22,56,22},{12.5,37.5,37.5,12.5}}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .