Latex(Overleaf)表格中文本的垂直对齐

5

我在Overleaf上有以下示例代码来生成一个表格。

\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
    \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
    \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage{stackengine}
    \newcommand\xrowht[2][0]{\addstackgap[0.5\dimexpr#2\relax]{\vphantom{#1}}}
    
\title{table}
\begin{document} 
\maketitle


\begin{table}[ht]
\footnotesize
    \centering
    \begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|}
    \hline\xrowht[()]{15pt}
         \multirow{2}{*}{Test} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{*}{Cell} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\
         \cline{3-5}\cline{7-10}\xrowht[()]{15pt}
         No & & x & y & z & Size & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
         \hline\xrowht[()]{15pt}
         1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
         \hline\xrowht[()]{15pt}
         2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1}  & 0.1 & 0.1 & 0.1 \\
         \hline
         \end{tabular}
         \vspace{0.5cm}
    \caption{Tests}
\end{table}


\end{document}

这给了我:

enter image description here

问题:我无法使表格中的文本垂直居中或位于其他任何位置。例如,“Test No”、“Name”、“Cell Size”、“C1”和“C2”应该是垂直居中对齐的。此外,我发现使用\hline\xrowht[()]{15pt}会给我不同的盒子宽度,请参见“Domain”和“MLR”框。请问有人可以帮助我正确设置这个表格吗?

感谢大家

2个回答

7

不要手动调整表格行高,你可以改变 \arraystretch,所有单元格会自动居中。

其他注意事项:


\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{array}
    \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
    \newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}

\usepackage{caption}
    
\title{table}
\begin{document} 
\maketitle



\begin{table}[ht]
\renewcommand{\arraystretch}{2}
\footnotesize
    \centering
    \begin{tabular}{|P{0.75cm}|P{1.25cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{0.75cm}|P{1cm}|P{1cm}|P{1cm}|P{1cm}|P{0.75cm}|}
    \hline
         \multirow{2}{0.75cm}{\centering Test\\ No} & \multirow{2}{*}{Name} & \multicolumn{3}{c|}{Domain} & \multirow{2}{0.75cm}{\centering Cell\\ Size} & \multicolumn{4}{c|}{MLR} & \multirow{2}{*}{$C_1$} & \multirow{2}{*}{$C_2$}\\
         \cline{3-5}\cline{7-10}
          & & x & y & z & & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
         \hline
         1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
         \hline
         2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \multicolumn{2}{c|}{0.1}  & 0.1 & 0.1 & 0.1 \\
         \hline
         \end{tabular}
%         \vspace{0.5cm}
    \caption{Tests}
\end{table}


\end{document}

enter image description here


如果我仔细看,单元格内容并不完全垂直居中(在第1行和第2行可见),这是\arraystretch的问题 :-( 值得一提的是:https://tex.stackexchange.com/a/166370/32813 - escalator

1

使用tabularray包现在更容易合并单元格并自动居中它们:

\documentclass[hidelinks,a4paper,12pt,oneside]{article}
\usepackage[margin=1in]{geometry}
\usepackage{caption}
\usepackage{tabularray}
\title{table}
\begin{document} 
\maketitle

\begin{table}[ht]
\footnotesize
    \begin{tblr}{
      colspec = {*{12}{X}},
      cells={halign=c},
      vlines,
      hlines,
      stretch=2
    }
    \SetCell[r=2]{} Test No & \SetCell[r=2]{} Name & \SetCell[c=3]{} Domain && & \SetCell[r=2]{} Cell Size & \SetCell[c=4]{} MLR &&&& \SetCell[r=2]{} $C_1$ & \SetCell[r=2]{} $C_2$\\
    & & x & y & z & & $\dot{m_t}$ & $\dot{m}_{in}$ & $\dot{m}_{out}$ & $\dot{m}_{avg}$ & & \\
    1 & Mass1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 \\
    2 & Mass2 & 0.1 & 0.1 & 0.1 & 0.1 & 0.1 & \SetCell[c=2]{} 0.1 &  & 0.1 & 0.1 & 0.1 \\
    \end{tblr}
    \caption{Tests}
\end{table}

\end{document}

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接