![调整 Longtable 以适应文本宽度](https://linux22.com/image/311850/%E8%B0%83%E6%95%B4%20Longtable%20%E4%BB%A5%E9%80%82%E5%BA%94%E6%96%87%E6%9C%AC%E5%AE%BD%E5%BA%A6.png)
我在将 longtables 调整为 textwidth 时遇到了问题。我使用的表格的列数不同,但 LaTeX 显然无法将它们调整为相同的宽度。所有列数相同的 Longtables 都以相同的宽度显示。我该如何解决这个问题?提前致谢!
\documentclass[a4paper,12pt,headsepline,american]{scrreprt}
\usepackage{array}
\usepackage[english]{babel}
\usepackage[labelfont=bf]{caption}
\captionsetup{format=plain,font=footnotesize}
\usepackage{float}
\usepackage{geometry}
\usepackage{layouts}
\usepackage{longtable}
\usepackage{ragged2e}
\begin{document}
...
\begin{longtable}{|>{\RaggedRight}p{0.4\textwidth}|>{\RaggedRight}p{0.2\textwidth}|>{\RaggedRight}p{0.4\textwidth}|}
\caption[...]{\textbf{...}}\\
\hline
... & ... & ...\\
\hline
\hline
\endfirsthead
\hline
... & ... & ...\\
\hline
\hline
\endhead
... & ...& ...\\
\hline
\end{longtable}
...
\begin{longtable}{|>{\RaggedRight}p{0.3\textwidth}|>{\RaggedRight}p{0.7\textwidth}|}
\caption[...]{\textbf{...}}\\
\hline
... & ...\\
\hline
\hline
\endfirsthead
\hline
... & ...\\
\hline
\hline
\endhead
... & ...\\
\hline
\end{longtable}
...
\end{document}
答案1
您需要考虑规则的宽度(\arrayrulewidth
)和列填充(\tabcolsep
在列的每一侧),因此:
\documentclass[a4paper,12pt,headsepline,american]{scrreprt}
\usepackage{array}
\usepackage[english]{babel}
\usepackage[labelfont=bf]{caption}
\captionsetup{format=plain,font=footnotesize}
\usepackage{float}
\usepackage{geometry}
\usepackage{layouts}
\usepackage{longtable}
\usepackage{ragged2e}
\newlength\ltlen
\begin{document}
...
\setlength\ltlen{\dimexpr\textwidth-4\arrayrulewidth-6\tabcolsep}
\begin{longtable}{|>{\RaggedRight}p{0.4\ltlen}|>{\RaggedRight}p{0.2\ltlen}|>{\RaggedRight}p{0.4\ltlen}|}
\caption[...]{\textbf{...}}\\
\hline
... & ... & ...\\
\hline
\hline
\endfirsthead
\hline
... & ... & ...\\
\hline
\hline
\endhead
... & ...& ...\\
\hline
\end{longtable}
...
\setlength\ltlen{\dimexpr\textwidth-3\arrayrulewidth-4\tabcolsep}
\begin{longtable}{|>{\RaggedRight}p{0.3\ltlen}|>{\RaggedRight}p{0.7\ltlen}|}
\caption[...]{\textbf{...}}\\
\hline
... & ...\\
\hline
\hline
\endfirsthead
\hline
... & ...\\
\hline
\hline
\endhead
... & ...\\
\hline
\end{longtable}
...
\end{document}