With the following code, I produce a table containing cells with one and two lines:




        \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


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!

  • 1
    Try replacing p with m as in \newcolumntype{P}[1]{>{\centering\arraybackslash}m{#1}}
    – leandriis
    Sep 26, 2019 at 8:08
  • 1
    You might also want to keep in mind that your table is currently wider than the textwidth. Instead of manually calculating the necesarry column widths, I'd recomment the use of tabularx.
    – leandriis
    Sep 26, 2019 at 8:20

2 Answers 2


Something like this?

enter image description here

(red lines indicate page layout)

  • your table is to wide for standard page layout, consequently I suggest to take two measures:

    • increase \textwidth (by use of the geometry package)
    • reduce font size to \small
  • for table environment use tabularx
  • define new column type C derived from X column type
  • define new command for shorter writing of multi column cells

Complete MWE is:

\usepackage[a4paper, margin=25mm]{geometry} % set suitable page parameters

%---------------- show page layout. don't use in a real document!

\begin{tabularx}{\linewidth}{|>{\bfseries\centering}m{17mm}|| *{8}{C|}}
    &   \mcx{text no. one}   
                    &   \mcx[4]{text}                   &   \mcx{text no. two}  \\ \hline
    &   \mcx{--}         
                    &   \mcx{towards no. one} 
                            & \mcx{away from no. one}   & \mcx{--}              \\ \hline
two line text
    & low   & high  & low   & high  & low   & high      & low   & high          \\ \hline

Please see if this meets the requirements

enter image description here

\usepackage[a4paper]{geometry} % set suitable page parameters
\usepackage{caption} % optional 
\usepackage[table]{xcolor} %Used to color the last column
\small\centering{position} &     
\multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+2\arrayrulewidth\small\centering\relax}X|}{text no. one} &     
\multicolumn{4}{|>{\hsize=\dimexpr4\hsize+6\tabcolsep+3\arrayrulewidth\small\centering\relax}X|}{text}                         &     
\multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+2\arrayrulewidth\small\centering\relax}X|}{text no. two} \\ \hline
        \small\centering{other} & 
        \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\small\centering\relax}X|}{-} & 
        \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\small\centering\relax}X|}{towards no. one} & 
        \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\small\centering\relax}X|}{away from no. one} & 
        \multicolumn{2}{|>{\hsize=\dimexpr2\hsize+2\tabcolsep+1\arrayrulewidth\small\centering\relax}X|}{-} \\ \hline
        \footnotesize\centering{two line text} & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}          & \footnotesize\centering{two line text}\\%    
  • Hi, thank you for your code! Unfortunately, the text is not vertically centered so I am still looking for a way to do that.
    – Ivo
    Dec 5, 2019 at 10:22
  • sorry--I have updated code for centering--could not get the bottom line of the table keeps throwing an error
    – js bibra
    Dec 5, 2019 at 12:06
  • It is sufficient to load each package only once.
    – Zarko
    Dec 5, 2019 at 12:51
  • @Zarko why does hline throw an error in the last line of the table
    – js bibra
    Dec 5, 2019 at 13:31
  • @jsbibra, cause of your problem is using \centering in cells. Its use in cells' contents formatting is wrong. You should define column specification accordingly.
    – Zarko
    Dec 5, 2019 at 20:31

You must log in to answer this question.

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