如何使表格中的文本居中

如何使表格中的文本居中

我有以下 LaTeX 代码,它几乎可以完成我想要的工作:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}
\begin{table}
  \begin{tabular}{ | p{1.5cm} | p{1cm} | c | c | }
    \hline
     & Fully Translated & Linearized & Stabilized \\
    \hline
    MulDA Partial &  &  &  \\
    \hline
    MulDA Full & \multirow{2}{*}{X} &  &  \\
    \hline
    Stabilized Partial &  &  & \multirow{2}{*}{X} \\
    \hline
    Stabilized Full & \multirow{2}{*}{X} &  & \multirow{2}{*}{X} \\
    \hline
    Linearized Partial &  & \multirow{2}{*}{X} &  \\
    \hline
    Linearized Full & \multirow{2}{*}{X} & \multirow{2}{*}{X} &  \\
    \hline
  \end{tabular}
\end{table}

\end{document}

但是,X第二列中的 s 稍微偏左了。

Xs 稍微向左偏移

我该如何解决这个问题?我尝试过使用mc代替p,使用 效果c很接近,但是它会使列太大而溢出。

编辑:ChatGPT 建议使用parbox几乎可行的方法,但会使框中的文本过于拥挤:

文字太拥挤

下面是该代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multirow}

\begin{document}
\begin{tabular}{ | p{1.5cm} | c | c | c | }
    \hline
     & \parbox{1.5cm}{Fully\newline Trans-\newline lated} & Linearized & Stabilized \\
    \hline
    MulDA Partial &  &  &  \\
    \hline
    MulDA Full & \multirow{2}{*}{X} &  &  \\
    \hline
    Stabilized Partial &  &  & \multirow{2}{*}{X} \\
    \hline
    Stabilized Full & \multirow{2}{*}{X} &  & \multirow{2}{*}{X} \\
    \hline
    Linearized Partial &  & \multirow{2}{*}{X} &  \\
    \hline
    Linearized Full & \multirow{2}{*}{X} & \multirow{2}{*}{X} &  \\
    \hline
  \end{tabular}
\end{document}

答案1

使用包tblrtabularray表格代码很简单,结果(至少在我看来)更好:

在此处输入图片描述

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{table}
\begin{tblr}{hlines, vlines,
            colspec = {Q[l,m,wd=1.6cm] Q[c,m,wd=1cm] Q[c,m] Q[c,m]},
            %rowsep=1pt
            }
                    & Fully Translated
                        & Linearized
                            & Stabilized    \\
MulDA Partial       &   &   &               \\
MulDA Full          & X &   &               \\
Stabilized Partial  &   &   & X             \\
Stabilized Full     & X &   & X             \\
Linearized Partial  &   & X &               \\
Linearized Full     & X & X &               \\
\end{tblr}
    \end{table}
\end{document}

答案2

\multirow。使用\Centeringfromragged2em列类型 from array

\documentclass{article}
\usepackage{ragged2e,array}

\begin{document}
\begin{table}

\begin{tabular}{
  |
  >{\Centering}m{1.6cm}
  |
  >{\Centering}m{1cm} | c | c | }
\hline
& Fully Translated & Linearized & Stabilized \\
\hline
MulDA Partial &  &  &  \\
\hline
MulDA Full & X &  &  \\
\hline
Stabilized Partial &  &  & X \\
\hline
Stabilized Full & X &  & X \\
\hline
Linearized Partial &  & X &  \\
\hline
Linearized Full & X & X &  \\
\hline
\end{tabular}

\end{table}

\end{document}

在此处输入图片描述

不同的风格

\documentclass{article}
\usepackage{booktabs}

\newcommand{\splitcell}[1]{\begin{tabular}{@{}c@{}}#1\end{tabular}}

\begin{document}
\begin{table}

\begin{tabular}{@{}lccc@{}}
\toprule
& \splitcell{Fully \\ Translated} & Linearized & Stabilized \\
\midrule
\textit{MulDA} \\
\quad Partial &  &  &  \\
\quad Full & X &  &  \\
\midrule
\textit{Stabilized} \\
\quad Partial &  &  & X \\
\quad Full & X &  & X \\
\midrule
\textit{Linearized} \\
\quad Partial &  & X &  \\
\quad Full & X & X &  \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

在此处输入图片描述

答案3

ChatGPT 经过了几次迭代(我最终根据 ChatGPT 的建议自己编写了这个最终版本),但我最终得到了我想要的:

正确居中图像

\begin{tabular}{ | p{1.5cm} | c | c | c | c | }
    \hline
     & \parbox{1cm}{\vspace{0.1cm}\centering Fully\newline Trans-\newline lated\vspace{0.1cm}} & Linearized & Stabilized \\
    \hline
    MulDA Partial &  &  &  \\
    \hline
    MulDA Full & \multirow{2}{*}{X} &  &  \\
    \hline
    Stabilized Partial &  &  & \multirow{2}{*}{X} \\
    \hline
    Stabilized Full & \multirow{2}{*}{X} &  & \multirow{2}{*}{X} \\
    \hline
    Linearized Partial &  & \multirow{2}{*}{X} &  \\
    \hline
    Linearized Full & \multirow{2}{*}{X} & \multirow{2}{*}{X} &  \\
    \hline
\end{tabular}

相关内容