如何让表格单元格中的所有文本居中?

如何让表格单元格中的所有文本居中?

以下编译后的代码输出不符合预期。我希望所有文本都居中显示在单元格中,因此我需要第一列和最后一列的文本看起来与其他列一样。我该如何实现?

\documentclass[a4paper,12pt]{article}
\usepackage{diagbox}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}


\begin{document} 

\begin{table}[ht]
\caption{Conditions for Neutral Stability} 
\centering
   \begin{tabular}{|l||*{3}{M{4cm}|}}\hline
\backslashbox{Density}{Velocity}
&\makebox[3em]{$U_{1} < U_{2}$}&\makebox[3em]{$U_{1} = U_{2}$}&\makebox[3em]{$U_{1} > U_{2}$}
\\ [.5cm]\hline\hline
$\rho_{1}<\rho_{2}$ & Neutrally stable if \newline $|U_{1}-U_{2}|<\sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}$ &Unstable& Neutrally stable if $|U_{1}-U_{2}|<\sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}$\\ [1cm]\hline
$\rho_{1}=\rho_{2}$ &Unstable&Neutrally Stable&Unstable\\ [1cm]\hline
$\rho_{1}>\rho_{2}$ &Unstable&Unstable&Unstable\\ [1cm]\hline
   \end{tabular}
\end{table}
 \end{document}

输出:

编译输出

答案1

编辑: 由于使用diagbox是强制性的,因此我在第一个版本的答案中更改了 MWE。请注意,现在表格更宽了,可能不适合文本宽度。可以获得一些空间(8mm)以文本样式写入公式(将命令拖放\displaystyle到完整的 MWE 下方)并将列宽更改为:

   \begin{tabular}{|l||>{\centering\arraybackslash}m{4cm}|
                        >{\centering\arraybackslash}m{3cm}|
                        >{\centering\arraybackslash}m{4cm}|}

在此处输入图片描述

我将所有选项放在[3em]\makebox,并替换\\[1cm]\\。相反,我添加了\renewcommand\arraystretch{1.5},第二行和第三行的方程式写入\displaystyle,其中我还在方程式的上方和下方添加了一些垂直空间。

在此处输入图片描述

\documentclass{article}
\usepackage{diagbox}
\usepackage{array}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}
    \begin{table}[ht]
\caption{Conditions for Neutral Stability}
\centering
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{3pt}
\begin{tabular}{|l||>{\centering\arraybackslash}m{4.4cm}|
                    >{\centering\arraybackslash}m{3.0cm}|
                    >{\centering\arraybackslash}m{4.4cm}|}
\hline
\backslashbox{Density}{Velocity}
    &   \makebox{$U_{1} < U_{2}$} 
        &   \makebox{$U_{1} = U_{2}$}
            &   \makebox{$U_{1} > U_{2}$}           \\
    \hline\hline

\rho_{1}<\rho_{2}   
    &   Neutrally stable if\smallskip\newline
        $\displaystyle
        |U_{1}-U_{2}|<
        \sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
        $\smallskip
        &   Unstable    &   Neutrally stable if\smallskip\newline 
                            $\displaystyle
                            |U_{1}-U_{2}|<
                            \sqrt{\frac{g(\rho_{1}^{2}-
                                        \rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
                            $\smallskip                              \\
    \hline
\rho_{1}=\rho_{2}
    &   Unstable        &   Neutrally Stable    &   Unstable                 \\
    \hline
\rho_{1}>\rho_{2}
    &   Unstable        &   Unstable            &   Unstable                 \\
    \hline
   \end{tabular}
    \end{table}
\end{document}

附录:hhline通过使用 (i)包、(ii) 而不是\backslashbox{Density}{Velocity}使用\diagbox[innerwidth=2.4cm]{Density}{Velocity}},以及将第三列缩小到 1.8 厘米, 可以实现更好的外观和较小的表格宽度。此外,插入\diagbox花括号后所有内容\makebox{...}都变为多余的内容:

\documentclass[a4paper,12pt]{article}
\usepackage{diagbox}
\usepackage{array}
\usepackage{hhline}
\usepackage[font=small,labelfont=bf]{caption}

\begin{document}
    \begin{table}[ht]
\caption{Conditions for Neutral Stability}
\centering
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{4pt}
    \begin{tabular}{|c||>{\centering\arraybackslash}m{4.4cm}|
                        >{\centering\arraybackslash}m{1.8cm}|
                        >{\centering\arraybackslash}m{4.4cm}|}
    \hhline{|-||-|-|-|}
{\diagbox[innerwidth=2.4cm]{Density}{Velocity}}
    &   $U_{1} < U_{2}$ 
        &   $U_{1} = U_{2}$
            &   $U_{1} > U_{2}$            \\
    \hhline{:=::=:=:=:}
$\rho_{1}<\rho_{2}$   
    &   Neutrally stable if\smallskip\newline
        $\displaystyle
        |U_{1}-U_{2}|<
        \sqrt{\frac{g(\rho_{1}^{2}-\rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
        $\smallskip
        &   Unstable    &   Neutrally stable if\smallskip\newline 
                            $\displaystyle
                            |U_{1}-U_{2}|<
                            \sqrt{\frac{g(\rho_{1}^{2}-
                                        \rho_{2}^{2})}{k\rho_{1}\rho_{2}}}
                            $\smallskip                              \\
    \hhline{|-||-|-|-|}
$\rho_{1}=\rho_{2}$
    &   Unstable        &   Neutrally Stable    &   Unstable                 \\
    \hhline{|-||-|-|-|}
$\rho_{1}>\rho_{2}$
    &   Unstable        &   Unstable            &   Unstable                 \\
    \hhline{|-||-|-|-|}
   \end{tabular}
    \end{table}
\end{document}

在此处输入图片描述

相关内容