0

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:

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!

2
  • 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

1

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:

\documentclass[12pt]{article}
\usepackage[a4paper, margin=25mm]{geometry} % set suitable page parameters
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcx{O{2}m}
    {\multicolumn{#1}
                 {>{\hsize=\dimexpr#1\hsize+#1\tabcolsep+#1\arrayrulewidth/2\relax}C|}
                 {#2}}

%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
    \small
    \setlength\tabcolsep{3pt}
    \renewcommand\tabularxcolumn[1]{m{#1}}
\begin{tabularx}{\linewidth}{|>{\bfseries\centering}m{17mm}|| *{8}{C|}}
        \hline
position    
    &   \mcx{text no. one}   
                    &   \mcx[4]{text}                   &   \mcx{text no. two}  \\ \hline
{other}    
    &   \mcx{--}         
                    &   \mcx{towards no. one} 
                            & \mcx{away from no. one}   & \mcx{--}              \\ \hline
two line text
    & low   & high  & low   & high  & low   & high      & low   & high          \\ \hline
\end{tabularx}
    \end{table}
\end{document}
0

Please see if this meets the requirements

enter image description here

\documentclass{article}
\usepackage[a4paper]{geometry} % set suitable page parameters
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{caption} % optional 
\usepackage{siunitx}
    \usepackage{tabulary}
    \usepackage{array}
    \usepackage{multirow}
\usepackage[table]{xcolor} %Used to color the last column
\usepackage{tabularx,ragged2e,booktabs,lipsum}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[export]{adjustbox}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|XXXXXXXXX|}
\hline 
\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}\\%    
\end{tabularx}
\end{document}
5
  • 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 .