我是新手,正在自学 LaTeX,但我发现了很多格式化表格的方法。我不喜欢下面的代码,希望有更好的选择,以及我还没有见过的东西。
Overleaf 有一个表格示例,看起来非常棒,但与我的四列模型不兼容。我很可能做错了什么,但我需要帮助找到正确的方向。
我怎么知道哪个版本的表格可以满足我的需要?
我需要:
- 自动间距,因此较长的文本会自行调整大小以适应单元格并转到新行。第三列将不断更改信息,因此我无法手动添加需要硬回车的单元格。此文档将非常长,创建如下所示的 \makecells 将非常繁琐。
- 前两列需要居中,后两列则居左。
- 我需要在第一列中对步骤进行编号,所以我需要某种自动枚举?也许?
- 我需要表格适合页面的大小(如
\textwidth
)
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{multicol}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\begin{document}
\begin{center}
\begin{tabular}{|c|c|p{9.5cm}|p{6cm}|}
\hline
\thead{Step} & \thead{System} & \thead{Detail} & \thead{Expected Results} \\
\hline
\makecell[c]{1} & \makecell[c]{DSU} & \makecell[l]{Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\ \\ \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
\makecell[c]{1} & \makecell[c]{DSU} & \makecell[l]{Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\ \\ \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
\makecell[c]{1} & \makecell[c]{DSU} & \makecell[l]{Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\ \\ \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
\multicolumn{4}{|c|}{\textbf{End section procedure.}}\\
\hline
\end{tabular}
\end{center}
答案1
\documentclass[12pt]{article}
\usepackage{tabularray}
\begin{document}
\begin{center}
\begin{tblr}{width=\textwidth, colspec={|c|c|X[3,l,m]|X[2,l,m]|},
row{1}={c,font=\bfseries}}
\hline
{Step} & {System} & {Detail} & {Expected Results} \\
\hline
{1} & {DSU} & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex] \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
{1} & {DSU} & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex] \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
{1} & {DSU} & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex] \textbf{Note}: This may take several minutes.} & Command window opens.\\
\hline
\SetCell[c=4]{c}{\textbf{End section procedure.}}&&&\\
\hline
\end{tblr}
\end{center}
\end{document}
{ }
可以移除几对。
答案2
这是表格的代码。由于其中一个要求是让表格自动调整至页面宽度,因此我加载了tabularx
其X
列类型,它会自动调整宽度以适应剩余的可用空间。
重新定义m{}
内部X
(1) 如果文本太长则添加换行符,(2) 允许添加\par
(或空行)以形成多个段落,最后 (3) 应用中间对齐。此外,由于非零,\parskip
段落之间添加了额外的垂直空间。
还有一个计数器,它通过“\stepcounter”自动对每一行进行迭代。
桌子:
\documentclass[12pt]{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage[nopar]{kantlipsum}
\newcommand\thead[1]{\normalfont\bfseries #1}
\renewcommand{\tabularxcolumn}[1]{>{\parskip=9pt\RaggedRight}m{#1}}
\newcounter{nrow}
\begin{document}
\begin{center}
\renewcommand*\arraystretch{1.45}
\begin{tabularx}{\textwidth}{|
>{\stepcounter{nrow}}c|c|X|>{\parskip=9pt\RaggedRight}m{3.5cm}|}
\hline
\thead{Step} & \thead{System} & \thead{Detail} & \thead{Expected Results} \\
\hline
\thenrow & DSU &
Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.
\par
\textbf{Note}: This may take several minutes.
& Command window opens. \\
\hline
\thenrow & DSU &
Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.
\textbf{Note}: This may take several minutes.
& Command window opens.\\
\hline
\thenrow & DSU &
\kant[1][1]
\kant[1][2]
& Command window opens.\\
\hline
\multicolumn{4}{|c|}{\textbf{End section procedure.}} \\
\hline
\end{tabularx}
\end{center}
\end{document}
答案3
@Celdor 和 @Pieter van Oostrum 的组合答案:自动编号步骤,使用tabularray
具有更简洁表格代码的包。
\documentclass[12pt]{article}
\usepackage{tabularray}
\UseTblrLibrary{counter}
\newlength\cw % column width
\newcounter{rn} % row number
\begin{document}
\begin{center}
\settowidth\cw{\textbf{Expected Results}}
\begin{tblr}{hlines, vlines,
colspec = {c c X[l,m] Q[l, wd=\cw] },
cell{2-Y}{1} = {cmd=\stepcounter{rn}\thern},
row{1} = {c, font=\bfseries}
}
Step & System & Detail & Expected Results \\
& DSU & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex]
\textbf{Note}: This may take several minutes.}
& Command window opens. \\
& DSU & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex]
\textbf{Note}: This may take several minutes.}
& Command window opens. \\
& DSU & {Click \textbf{Start} and type: \textit{cmd} and press \textbf{Enter}.\\[2ex]
\textbf{Note}: This may take several minutes.}
& Command window opens. \\
\SetCell[c=4]{c} \textbf{End section procedure} & & & \\
\end{tblr}
\end{center}
\end{document}