将表格中的一行居中

将表格中的一行居中

以下是一个例子:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\begin{document}
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace}                                                                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
\multicolumn{2}{c}{\enspace}                                                                       \\
\multicolumn{2}{l}{\hspace{20pt} short line (left-aligned centered block)}                         \\
\multicolumn{2}{l}{\hspace{20pt} now a longer (line in the same left-aligned centered block)}      \\
\multicolumn{2}{c}{\enspace}                                                                       \\
\multicolumn{2}{c}{short line (centered, not left-aligned)}                                        \\
\multicolumn{2}{c}{this is a longer line (centered, not left-aligned)}                             \\
\multicolumn{2}{c}{\enspace}                                                                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
\end{tabular}
\end{center}

\end{document}

我有一张两列表格,我需要将跨越两列的文本居中并保持左对齐。

在上面的代码中,我使用左对齐多列和 \hspace 将块任意缩进 20pt。

有什么方法可以让编译器计算缩进的宽度/长度?

块居中的合适缩进应该是这样的:(表格宽度 - 平均行长)/ 2。

正确的做法是什么?

更新:

我选择了嵌套表格并获得了与原始表格几乎相同的布局:

在此处输入图片描述

答案1

使用 stackengine和它的\Centerstack命令,它可以采用可选的对齐参数(默认为c),以及\addlinespace(从booktabs)来简化这些块周围的垂直间距:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, hmargin=0.2cm]{geometry}
\usepackage{booktabs}
\usepackage[usestackEOL]{stackengine}

\begin{document}

\begin{center}
\scriptsize\setlength{\defaultaddspace}{2.7ex}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace} \\
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\addlinespace
\multicolumn{2}{c}{\Centerstack[l]{short line (left-aligned centered block) \\
now a longer (line in the same left-aligned centered block)}} \\
\addlinespace
\multicolumn{2}{c}{\Centerstack{short line (centered, not left-aligned) \\
this is a longer line (centered, not left-aligned)}} \\
\addlinespace
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\end{tabular}
\end{center}

\end{document}

在此处输入图片描述

编辑:加载eqparbox,您可以使用以下代码获取此对齐:

\begin{center}
\scriptsize\setlength{\defaultaddspace}{2.7ex}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace} \\
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\addlinespace
\multicolumn{2}{c}{\eqparbox{LC}{\Centerstack[l]{short line (left-aligned centered block) \\
now a longer (line in the same left-aligned centered block)}}} \\
\addlinespace
\multicolumn{2}{c}{\eqparbox{LC}{\Centerstack[l]{short line (centered, not left-aligned) \\
this is a longer line (centered, not left-aligned)}}} \\
\addlinespace
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\end{tabular}
\end{center}

在此处输入图片描述

答案2

通过以下两个小技巧,您可以获得两条彼此左对齐但在表格内居中的行。

在第一个例子中,我对两行文本都使用了单一居中p类型的列,而第二个例子则基于居中多列内的嵌套左对齐表格。

在此处输入图片描述

\documentclass{article}
\usepackage{array}
\usepackage{makecell}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\begin{document}
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace}                                                                             \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\multicolumn{2}{c}{}                                                                                     \\
\multicolumn{2}{>{\centering\arraybackslash}p{8cm}}{short line (centered, not left-aligned) 
                                                    \newline 
                                                    this is a longer line (centered, not left-aligned)}  \\
\multicolumn{2}{c}{}                                                                                     \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\multicolumn{2}{c}{}                                                                                     \\
\multicolumn{2}{c}{\begin{tabular}{@{}l@{}} 
                     short line (centered, not left-aligned) \\ 
                     this is a longer line (centered, not left-aligned)
                   \end{tabular}}                                                                        \\
\multicolumn{2}{c}{}                                                                                     \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\end{tabular}
\end{center}

\end{document}

答案3

通过使用\makecell\mbox您的表格也可以按以下方式编写:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, 
            top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\usepackage{array, makecell}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{2}m}
    {\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\mcc{}                                                          \\
\mcc{\mbox{%
     \makecell[l]{short line (centered, not left-aligned)\\
                  this is a longer line 
                  (centered, not left-aligned)}}
     }                                                          \\
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\mcc{}                                                          \\
\mcc{short line (centered, not left-aligned)}                   \\
\mcc{this is a longer line (centered, not left-aligned)}        \\
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\end{tabular}
    \end{center}
\end{document}

在此处输入图片描述

相关内容