1

The following code produces the kind of table I want, but it does attribute almost all the space to the first cell. I want them split equally without providing an exact cell width that I have to recalculate each time I have a different amount of columns.

enter image description here

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{tabularx}  % for 'tabularx' environment and 'X' column type
\usepackage{ragged2e}  % for '\RaggedRight' macro (allows hyphenation)
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} 
\usepackage{booktabs}  % for \toprule, \midrule, and \bottomrule macros 

\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{} Y c c c @{}} % use 'Y' for first column
\toprule
1 & 2 & 3 \\
\midrule
1 & \begin{tabular}{l} 2  \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2  \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2  \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
1 & \begin{tabular}{l} 2  \\ 3\end{tabular} & \begin{tabular}{l} 1 \\ 2\end{tabular} \\
\bottomrule
\end{tabularx}
\caption{Nonlinear Model Results} 
\label{table:nonlin}
\end{table}
\end{document}

2 Answers 2

1

You have to use X type columns on each column you want to be as wide as possible. The available space is equally distributed among those.

So for centred X type columns you could define \newcolumntype{Z}{>{\centering\arraybackslash}X}. I also defined a shortcut macro for the inner \begin{tabular}...\end{tabular} constructs.

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman,english]{babel}
\usepackage{tabularx}  % for 'tabularx' environment and 'X' column type
\usepackage{ragged2e}  % for '\RaggedRight' macro (allows hyphenation)
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} 
\newcolumntype{Z}{>{\centering\arraybackslash}X} 
\usepackage{booktabs}  % for \toprule, \midrule, and \bottomrule macros 
\newcommand\CELL[2][l]{\begin{tabular}{@{}#1@{}}#2\end{tabular}}

\begin{document}
\begin{table}
\begin{tabularx}{\textwidth}{@{} Y Z Z Z @{}} % use 'Y' for first column
\toprule
1 & 2 & 3 \\
\midrule
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
1 & \CELL{2\\3} & \CELL{1\\2} \\
\bottomrule
\end{tabularx}
\caption{Nonlinear Model Results} 
\label{table:nonlin}
\end{table}
\end{document}

To produce the image I've added vertical rules to show that they all have the same width. enter image description here

1
  • Works perfectly, and the \CELL is so great. You are awesome! Thanks!
    – Jannis
    Commented May 17, 2018 at 15:20
1
\documentclass[a4paper]{article}
\usepackage{caption}
\usepackage{tabularx}
\usepackage{ragged2e}
\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{Y}{>{\RaggedRight}X} 
\newcolumntype{Z}{>{\Centering}X} 
\usepackage{booktabs}  
\let\CELL\shortstack

\begin{document}
\begin{table}
\caption{Nonlinear Model Results} \label{table:nonlin}
\begin{tabularx}{\textwidth}{@{} Y Z Z @{}}\toprule
    1 & 2 & 3 \\\midrule
    1 & \CELL{2\\3} & \CELL{1\\2} \\
    1 & \CELL{2\\3} & \CELL{1\\2} \\
    1 & \CELL{2\\3} & \CELL{1\\2} \\
    1 & \CELL{2\\3} & \CELL{1\\2} \\\bottomrule
\end{tabularx}
\end{table}

\end{document}

A tabular should have it's caption above the table:

enter image description here

You must log in to answer this question.

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