表格环境中单元格内容的垂直定位(顶部对齐)

表格环境中单元格内容的垂直定位(顶部对齐)

背景

我正在使用 Overleaf(编译器 = XeLaTeX;TexLive 版本 2021)编写文档。在其中,我将从 Excel 传输的表格放入TablesGenerator.com然后我将其放入我的 Overleaf 文档中。

下面是我想要制作的一个表格的可行示例(忽略内容,它是故意弄乱的):

\begin{landscape}
\begin{table}[]
\small
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{@{}llll@{}}
\toprule
\multicolumn{1}{c}{Name} & \multicolumn{1}{c}{Label} & \multicolumn{1}{c}{Description}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                & \multicolumn{1}{c}{Code} \\ \midrule
Cars                     & Manufacturers             & Audi, Kia,Volkswagen                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           & 23579                    \\
Providence               & Rhode Island              & \begin{tabular}[c]{@{}l@{}}Immigrant labor powered one of the nation's largest industrial manufacturing centers. Providence was a major manufacturer of industrial products, \\ from steam engines to precision tools to silverware, screws, and textiles. Giant companies were based in or near Providence, such as Brown \& Sharpe,\\  the Corliss Steam Engine Company, Babcock \& Wilcox, the Grinnell Corporation, the Gorham Manufacturing Company, Nicholson File, and the Fruit \\ of the Loom textile company. From 1975 until 1982, \$606 million of local and national community development funds were invested throughout the city. \\ In the 1990s, the city pushed for revitalization, realigning the north–south railroad tracks, removing the huge rail viaduct that separated Downtown from\\  the capitol building, uncovering and moving the rivers (which had been covered by paved bridges) to create Waterplace Park and river walks along the\\  rivers' banks, and constructing the Fleet Skating Rink (now the Alex and Ani City Center) and the Providence Place Mall.\end{tabular} & 2346                     \\ \bottomrule
\end{tabular}
}
\end{table}
\end{landscape}

问题

我希望第二行和第三行的内容在每个单元格内顶部对齐,而不是像现在这样垂直居中。

换句话说,我想要一个如下所示的表格:

在此处输入图片描述

有没有一种简单的方法可以做到这一点tabular,就像我现在这样?

我尝试过的方法

我在 Tex.SE 上搜索了这个问题的解决方案,有几十篇关于这个问题的帖子。不过,我读到的大多数帖子似乎都建议使用不同的表格环境,例如tabularx,我试图将其调整到我的示例,但没有成功。我更喜欢坚持使用tabular,尽管我并不完全理解,但我通常可以让它工作。

我似乎关闭了浏览器选项卡,但另一篇文章建议在代码中使用[t][p]某处tabular,但我记不清具体是什么了。这有点没意义,因为我试过了,但没有用。

有没有一种直接的方法可以做到这一点tabular

答案1

当同一行中有多个单元格时,其中一个单元格有多行,其他单元格只有一条线,则单行单元格相对于多行单元格的垂直位置取决于多行单元格中材料相对于行基线的垂直位置。

在您的例子中,您的“大”单元格由使用该选项的内部表格环境组成[c]。这意味着垂直中心其内容与行基线对齐,其他单元格也将在该基线处对齐。

要使其他行与内部表格的顶部对齐,您需要将其垂直位置设置为[t]而不是[c],以便其顶线与行的基线对齐,其他单元格将与其顶部对齐,而不是与中间对齐。(或者您可以直接删除[c],因为[t]这是表格的默认设置。)

\begin{landscape}
\begin{table}[]
\small
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{@{}llll@{}}
\toprule
\multicolumn{1}{c}{Name} & \multicolumn{1}{c}{Label} & \multicolumn{1}{c}{Description} & \multicolumn{1}{c}{Code} \\ \midrule
Cars & Manufacturers & Audi, Kia,Volkswagen & 23579 \\
Providence & Rhode Island & \begin{tabular}{@{}l@{}}Immigrant labor powered one of the nation's largest industrial manufacturing centers. Providence was a major manufacturer of industrial products, \\ from steam engines to precision tools to silverware, screws, and textiles. Giant companies were based in or near Providence, such as Brown \& Sharpe,\\ the Corliss Steam Engine Company, Babcock \& Wilcox, the Grinnell Corporation, the Gorham Manufacturing Company, Nicholson File, and the Fruit \\ of the Loom textile company. From 1975 until 1982, \$606 million of local and national community development funds were invested throughout the city. \\ In the 1990s, the city pushed for revitalization, realigning the north–south railroad tracks, removing the huge rail viaduct that separated Downtown from\\ the capitol building, uncovering and moving the rivers (which had been covered by paved bridges) to create Waterplace Park and river walks along the\\ rivers' banks, and constructing the Fleet Skating Rink (now the Alex and Ani City Center) and the Providence Place Mall.\end{tabular} & 2346 \\ \bottomrule
\end{tabular}
}
\end{table}
\end{landscape}

但是,由于“大单元格”的内容基本上是一段文本,而不是真正的表格,因此您使用内部表格环境很奇怪。除了使用l其列说明符外,您还可以使用或类似方法在给定宽度的 parbox 中排版其内容,p{5in}而不是l使用第三列。

这与使用内部表格具有类似的效果[t],但您不必在段落中手动添加换行符。

更好的是,您可以使用 tabularx 包并使用X列说明符。这样,列将扩展以填充可用空间,并且可能不需要调整大小框。

您说您在使用这个包时遇到了问题,但是不清楚为什么会遇到问题。

这是我建议的代码:

\documentclass{article}
\usepackage{lscape}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}

\begin{landscape}
\begin{table}[htb]
\small
\centering
\begin{tabularx}{\columnwidth}{@{}llp{5in}l@{}}
\toprule
\multicolumn{1}{c}{Name} & \multicolumn{1}{c}{Label} & \multicolumn{1}{c}{Description} & \multicolumn{1}{c}{Code} \\ \midrule
Cars & Manufacturers & Audi, Kia,Volkswagen & 23579 \\
Providence & Rhode Island & \raggedright Immigrant labor powered one of the nation's largest industrial manufacturing centers. Providence was a major manufacturer of industrial products, from steam engines to precision tools to silverware, screws, and textiles. Giant companies were based in or near Providence, such as Brown \& Sharpe, the Corliss Steam Engine Company, Babcock \& Wilcox, the Grinnell Corporation, the Gorham Manufacturing Company, Nicholson File, and the Fruit of the Loom textile company. From 1975 until 1982, \$606 million of local and national community development funds were invested throughout the city. In the 1990s, the city pushed for revitalization, realigning the north–south railroad tracks, removing the huge rail viaduct that separated Downtown from the capitol building, uncovering and moving the rivers (which had been covered by paved bridges) to create Waterplace Park and river walks along the rivers' banks, and constructing the Fleet Skating Rink (now the Alex and Ani City Center) and the Providence Place Mall.& 2346 \\ \bottomrule
\end{tabularx}
\end{table}
\end{landscape}
\end{document}

tabularx 输出

答案2

我会使用tabularray包而不是嵌套tabular在单元格中:

\documentclass{article}
\usepackage{microtype}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\usepackage{pdflscape}

\begin{document}
\begin{landscape}
    \begin{table}[]
    \small
\begin{tblr}{colspec={@{} ll X[j] c @{}},
             row{1}={font=\bfseries, c}
             }
    \toprule
Name    &   Label       &   Description         &   Code    \\ 
    \midrule
Cars    & Manufacturers & Audi, Kia,Volkswagen  & 23579     \\
Providence               
        & Rhode Island  & Immigrant labor powered one of the nation's largest industrial manufacturing centers. Providence was a major manufacturer of industrial products, from steam engines to precision tools to silverware, screws, and textiles. Giant companies were based in or near Providence, such as Brown \& Sharpe, the Corliss Steam Engine Company, Babcock \& Wilcox, the Grinnell Corporation, the Gorham Manufacturing Company, Nicholson File, and the Fruit of the Loom textile company. From 1975 until 1982, \$606 million of local and national community development funds were invested throughout the city.  In the 1990s, the city pushed for revitalization, realigning the north–south railroad tracks, removing the huge rail viaduct that separated Downtown from the capitol building, uncovering and moving the rivers (which had been covered by paved bridges) to create Waterplace Park and river walks along the rivers' banks, and constructing the Fleet Skating Rink (now the Alex and Ani City Center) and the Providence Place Mall.
                                                & 2346      \\ 
    \bottomrule
\end{tblr}
    \end{table}
\end{landscape}
\end{document}

在此处输入图片描述

相关内容