Threeparttable 和 longtabu 有一致的頂跳

Threeparttable 和 longtabu 有一致的頂跳

我正在嘗試創建一個帶有腳註的大表格(跨頁)和一個寬度可調的列。 MWE 如下。

\documentclass{scrartcl}

\usepackage{blindtext}

\usepackage{tabu,longtable}
\usepackage{threeparttable} % <-- the package below already loads it
\usepackage[referable]{threeparttablex}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{hyperref}

\usepackage{etoolbox}
\makeatletter
\chardef\TPT@@@asteriskcatcode=\catcode`*
\catcode`*=11
\expandafter\patchcmd\csname\string\threeparttable\endcsname
  {\TPT@hookin{tabular}}
  {\TPT@hookin{tabular}\TPT@hookin{tabu}}
  {}{}
\catcode`*=\TPT@@@asteriskcatcode
\makeatother

\begin{document}

\listoftables

\bigskip

\blindtext

\begin{ThreePartTable} 
\begin{TableNotes}
\footnotesize
\item[a] \label{tn:aa} Footnote in long table 1 (not linked).
\item[b] \label{tn:bb} Footnote in long table 2.
\source Some general note
\end{TableNotes}
\captionof{table}{A long table} % <-- caption inside longtabu within ThreePartTable does not produce correct hyperlink in LOT
\vspace{-\abovecaptionskip} % <-- removes the extra space (correct value?) 
\begin{longtabu}to\linewidth{llX} 
%\caption{A long table} \\ % <-- see above why it is commented
\toprule
Column 1 & & Column 2 \\
\midrule
\endfirsthead
\caption[]{A long table} \\
\toprule
Column 1 & & Column 2 \\
\midrule
\endhead
\cmidrule{3-3}
\multicolumn{3}{r}{\small\textit{continued}}
\endfoot
\bottomrule
\insertTableNotes\\
\endlastfoot
AAAA\tnotex*{tn:aa} & & BBBB \\
\newpage % <-- just testing break page
CCCC & & DDDD\tnotex{tn:bb} \\
\end{longtabu}
\end{ThreePartTable}

\blindtext

\end{document}

問題是\caption{}ThreePartTable 中的 longtabu 內部無法在 LOT 中產生正確的超連結。所以我是使用的替代解決方案\captionof{}。但這不好,因為它在標題和表中第一行之間沒有一致性跳過。我想使用\caption{},有人知道解決方案嗎?

答案1

以下解決方案有效。我定義了一個虛擬計數器,並使用\refstepcounter{dummy}它神秘地修正了參考系統。

不過我想要一個更優雅的解決方案:\setdummy每次使用 longtabu 時都調用,而不需要手動調整空格\vspace{-\baselineskip}

\documentclass[captions=tableheading]{scrartcl}

\usepackage{blindtext}

\usepackage{hyperref}
\usepackage{tabu,longtable}
%\usepackage{threeparttable} % the package below already loads it
\usepackage[referable]{threeparttablex}
\usepackage{booktabs}
\usepackage{caption}

\usepackage{etoolbox} 
\makeatletter
\chardef\TPT@@@asteriskcatcode=\catcode`*
\catcode`*=11
\expandafter\patchcmd\csname\string\threeparttable\endcsname
  {\TPT@hookin{tabular}}
  {\TPT@hookin{tabular}\TPT@hookin{tabu}} % allow threeparttable with tabu
  {}{}
\catcode`*=\TPT@@@asteriskcatcode
\makeatother

\makeatletter
\newcounter{dummy}
\newcommand{\setdummy}{\refstepcounter{dummy}\addtocounter{dummy}{-1}}
\makeatother

\begin{document}

\listoftables

\bigskip

\blindtext

\begin{ThreePartTable}
\begin{TableNotes}
\footnotesize
\item[a] \label{tn:aa} A note
\item[b] Another note
\end{TableNotes}
\vspace{-\baselineskip} % correct the \\ in \setdummy
\begin{longtabu}{ll}
\setdummy \\
\caption{A long table}
\label{tab:test1} \\
\toprule
Column 1 & Column 2 \\
\endfirsthead
\toprule
Column 1 & Column 2 \\
\midrule
\endhead
\cmidrule{2-2}
\multicolumn{2}{r}{\textit{continued}}
\endfoot
\bottomrule
\insertTableNotes
\endlastfoot
% the contents of the table
A & B\tnotex{tn:aa} \\
%\newpage % just testing page break
C\tnote{b} & D \\
\end{longtabu}
\end{ThreePartTable}

\blindtext

\begin{table}[h]
\centering
\caption{A regular table}
\label{tab:test2}
\begin{tabu}{ll}
\toprule
Column 1 & Column 2 \\
\midrule
A & B \\
\midrule
C & D \\
\bottomrule
\end{tabu}
\end{table}

\blindtext

\end{document}

相關內容