![左对齐 longtable 标题从表格的起始点开始](https://linux22.com/image/346691/%E5%B7%A6%E5%AF%B9%E9%BD%90%20longtable%20%E6%A0%87%E9%A2%98%E4%BB%8E%E8%A1%A8%E6%A0%BC%E7%9A%84%E8%B5%B7%E5%A7%8B%E7%82%B9%E5%BC%80%E5%A7%8B.png)
我无法将longtable
表 1 中的标题左对齐,从相同的表对象左边距开始。我本质上是在尝试实现我在表 2 中所做的操作。
我偶然看到一个建议使用的帖子LTcapwidth
。当我手动更改textwidth
因素(例如\setlength{\LTcapwidth}{.76\textwidth}
MWE 中的表 1)时,这种方法有效,但是,我正在处理包含大量因素的手稿,longtables
并且希望使用更灵活的解决方案,而不是手动调整\textwidth
所有因素。
最小工作示例
\documentclass{article}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{floatrow}
\begin{document}
\section{Table Woes}
%%%\setlength{\LTcapwidth}{.76\textwidth}
\begin{longtable}[c]{
@{}
>{\arraybackslash}p{0.10\linewidth}
>{\arraybackslash}p{0.10\linewidth}
>{\arraybackslash}p{0.10\linewidth}
>{\arraybackslash}p{0.10\linewidth}
>{\arraybackslash}p{0.10\linewidth}
>{\arraybackslash}p{0.10\linewidth}
@{}
}
\caption{Caption needs to be left aligned beginning at same starting position as table}
\label{tab:longtable_captionlable} \\
\toprule
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\midrule
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\bottomrule
\end{longtable}
\begin{table}[h]
\begin{floatrow}
\ttabbox
{\caption{Using floatrow, caption is properly left aligned from the beginning of left table margin.}\label{tab:table_captionlable}}
{\begin{tabular}{|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\hline
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\hline
\end{tabular}}%
\end{floatrow}
\end{table}
\end{document}
答案1
由于您正在使用floatrow
,因此您可以使用此包提供的功能。来自文档:
7.2 longtable 包
7.2.1 floatrow 包中的新增内容
(...) 如果您写
LTcapwidth=table
或LTcapwidth=contents
,您将获得等于表格宽度的标题宽度。
您可以使用 为longtable
s设置此键\floatsetup[longtable]
。代码:
\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{floatrow}
\floatsetup[longtable]{LTcapwidth=table}
\begin{document}
\section{Table Works}
\begin{longtable}[c]{@{}*{6}{>{\arraybackslash}p{0.10\linewidth}}@{}}
\caption{Caption needs to be left aligned beginning at same starting
position as table\label{tab:longtable_captionlable}}\\
\toprule
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\midrule
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\bottomrule
\end{longtable}
\begin{table}[h]
\ttabbox{%
\caption{Using floatrow, caption is properly left aligned from the
beginning of left table margin.}\label{tab:table_captionlable}%
}{%
\begin{tabular}{*{6}{|p{0.10\textwidth}}|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\hline
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\hline
\end{tabular}%
}%
\end{table}
\end{document}
另请注意,您可以*{num}{cols}
在表格列规范中使用 -expression 来提高代码的可读性。
答案2
这是一个简单的解决方案:
\documentclass{article}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{array}
\usepackage{booktabs}
\usepackage{floatrow}
\begin{document}
\section{Table Woes}
{\setlength{\LTcapwidth}{\dimexpr0.6\textwidth + 10\tabcolsep\relax}
\begin{longtable}{@{}*{6}{>{\arraybackslash}p{0.10\linewidth}}@{}}
\caption{Caption needs to be left aligned beginning at same starting position as table}
\label{tab:longtable_captionlable}\\
\endhead
\toprule
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\midrule
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\bottomrule
\end{longtable}}
\begin{table}[h]
\begin{floatrow}
\ttabbox
{\caption{Using floatrow, caption is properly left aligned from the beginning of left table margin.}\label{tab:table_captionlable}}
{\begin{tabular}{|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|p{0.10\textwidth}|}
\hline
Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\
\hline
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
1 & 2 & 3 & 4 & 5 & 6 \\
\hline
\end{tabular}}%
\end{floatrow}
\end{table}
\end{document}