With the following code, I produce a table containing cells with one and two lines:
\documentclass[a4paper,12pt]{article}
\usepackage{float}
\usepackage{lscape}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}[H]
\begin{tabular}{|P{2cm}||P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}|P{0.8cm}||}
\hline
\textbf{position} & \multicolumn{2}{P{2cm}|}{text no. one} & \multicolumn{4}{c|}{text} & \multicolumn{2}{c|}{text no. two} \\ \hline
\textbf{other} & \multicolumn{2}{c|}{-} & \multicolumn{2}{c|}{towards no. one} & \multicolumn{2}{c|}{away from no. one} & \multicolumn{2}{c|}{-} \\ \hline
\textbf{two line text} & low & high & low & high & low & high & low & high \\ \hline
\end{tabular}
\end{table}
\end{document}
This produces the following output:
I would like to define the columntype P
in a way that it automatically also vertically centers text automatically if the cell spans two lines but contains only one line of text (here, the cells containing position
, text
, text no. two
and all low
and high
entries).
I'm sure there is a straightforward solution but I couldn't find it either here or in the documentation of the array
package.
Many thanks for your help!
p
withm
as in\newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}}
tabularx
.