水平对齐表格中的多行单元格

水平对齐表格中的多行单元格

恐怕关于表格中单元格对齐的大量问题都无法解决我的问题。问题是我想要居中的单元格是一个multirow单元格。使用\multicolumn{1}{|c|}{text}(如建议的那样这里) 里面multirow似乎不起作用。

我的 MWE:

\documentclass{article}
\usepackage{multirow,multicol}

\begin{document}  

\begin{table}
\begin{tabular}{c l c c c}
\hline 
\multirow{2}{*}{Text}  & \multirow{2}{*}{Text to center} & 1234 & 345 & 543 \\
    & & (Four-digit) & (Three-digit) & (Four-digit) \\
\hline \hline
\multirow{3}{*}{Yes} & Managers, and senior officials & 1 & 2 & 1 \\
 & Professional occupations & 2 & 2 & 2 \\
 & Associate professional occupations & 3 & 3 & 3 \\
\hline
\multirow{3}{*}{No} & Personal service occupations & 5 & 6 & 8 \\   
 & Agricultural, fishery and related & 9 & 9 & 9 \\
 & Health service occupations & 8 & 6 & 6 \\
\hline
\end{tabular} 
\end{table}

\end{document}

enter image description here

笔记:我想将其居中,因为我希望该列的其余部分左对齐。

答案1

使用该makecell包。它允许在单元格中换行,并允许使用通用格式。默认情况下,\makecell无论列中的对齐方式如何,命令的内容都会垂直和水平居中(但可以使用可选参数进行更改)。此外,您还少了一行。

我借此机会使用了的规则booktabs,它在它们周围添加了一些垂直填充。

\documentclass{article}
\usepackage{multirow,multicol, makecell, booktabs}

\begin{document}

\begin{table}
  \begin{tabular}{c l c c c}
    \toprule
    {Text} & \makecell{Text to center} & \makecell{1234 & & \\ Four-digit} & \makecell{345\\Three-digit }& \makecell{543\\Four-digit }\\
    \cmidrule{1-5}\morecmidrules \cmidrule{1-5}
    \multirow{3}{*}{Yes} & Managers, and senior officials & 1 & 2 & 1 \\
                         & Professional occupations & 2 & 2 & 2 \\
                         & Associate professional occupations & 3 & 3 & 3 \\
    \cmidrule(lr){1-5}
    \multirow{3}{*}{No} & Personal service occupations & 5 & 6 & 8 \\
                         & Agricultural, fishery and related & 9 & 9 & 9 \\
                         & Health service occupations & 8 & 6 & 6 \\
    \bottomrule
  \end{tabular}
\end{table}

\end{document}

enter image description here

答案2

只需添加有关相关项目的“\hfil和”即可。\hfill

\documentclass{article}
\usepackage{multirow,multicol}

\begin{document}  

\begin{table}
\begin{tabular}{c l c c c}
\hline 
\multirow{2}{*}{Text}  & \hfil\multirow{2}{*}{Text to center}\hfill & 1234 & 345 & 543 \\
    & & (Four-digit) & (Three-digit) & (Four-digit) \\
\hline \hline
\multirow{3}{*}{Yes} & Managers, and senior officials & 1 & 2 & 1 \\
 & Professional occupations & 2 & 2 & 2 \\
 & Associate professional occupations & 3 & 3 & 3 \\
\hline
\multirow{3}{*}{No} & Personal service occupations & 5 & 6 & 8 \\   
 & Agricultural, fishery and related & 9 & 9 & 9 \\
 & Health service occupations & 8 & 6 & 6 \\
\hline
\end{tabular} 
\end{table}

\end{document}

enter image description here

答案3

只需multicolumn在你的multirow

\documentclass{article}
\usepackage{multirow,multicol}

\begin{document}  

\begin{table}
\begin{tabular}{c l c c c}
\hline 
\multirow{2}{*}{Text}  & \multicolumn{1}{c}{\multirow{2}{*}{ Text to center}} & 1234 & 345 & 543 \\
    & & (Four-digit) & (Three-digit) & (Four-digit) \\
\hline \hline
\multirow{3}{*}{Yes} & Managers, and senior officials & 1 & 2 & 1 \\
 & Professional occupations & 2 & 2 & 2 \\
 & Associate professional occupations & 3 & 3 & 3 \\
\hline
\multirow{3}{*}{No} & Personal service occupations & 5 & 6 & 8 \\   
 & Agricultural, fishery and related & 9 & 9 & 9 \\
 & Health service occupations & 8 & 6 & 6 \\
\hline
\end{tabular} 
\end{table}

\end{document}

答案4

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}  

\begin{table}
\begin{NiceTabular}{c l c c c}
\hline 
\Block{2-1}{Text}  & \Block[c]{2-1}{Text to center} & 1234 & 345 & 543 \\
    & & (Four-digit) & (Three-digit) & (Four-digit) \\
\hline \hline
\Block{3-1}{Yes} & Managers, and senior officials & 1 & 2 & 1 \\
 & Professional occupations & 2 & 2 & 2 \\
 & Associate professional occupations & 3 & 3 & 3 \\
\hline
\Block{3-1}{No} & Personal service occupations & 5 & 6 & 8 \\   
 & Agricultural, fishery and related & 9 & 9 & 9 \\
 & Health service occupations & 8 & 6 & 6 \\
\hline
\end{NiceTabular} 
\end{table}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

Output of the above code

相关内容