自定义 \newcolumntype 列的垂直对齐/垂直居中

自定义 \newcolumntype 列的垂直对齐/垂直居中

使用以下代码,我生成了一个包含一行和两行单元格的表格:

\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}

这将产生以下输出:

输出

我想以P一种方式定义列类型,以便它也能自动垂直如果单元格跨越两行但仅包含一行文本(此处,包含position、、text以及text no. two所有lowhigh条目)。

我确信有一个简单的解决方案,但我在这里或在包的文档中都找不到它array

非常感谢您的帮助!

答案1

像这样吗?

在此处输入图片描述

(红线表示页面布局)

  • 您的表格对于标准页面布局来说太宽,因此我建议采取两种措施:

    • 增加\textwidth(通过使用包geometry
    • 减小字体大小至\small
  • 适用于桌面环境tabularx
  • C定义从X列类型派生的新列类型
  • 定义新命令以缩短多列单元格的书写时间

完整的 MWE 是:

\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}

答案2

请看看这是否符合要求

在此处输入图片描述

\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}

相关内容