[Tex/LaTex] Linenumbers with lineno (edtable) breaks inside threeparttable because of caption

captionsline-numberingthreeparttable

I'm trying to show linenumbers for tables using lineno, i.e. edtable. The table is set using threeparttable and linenumbers work fine, but compilation fails when including a caption. The error message shown is you can't use \hrule here except with leaders. I use the caption package, but compilation also fails when using KOMA captions. The MWE illustrates the issue.

\documentclass{scrartcl}

\usepackage{threeparttable}
\usepackage{caption}
\usepackage[edtable]{lineno}

% That's the definition of caption@hrule from caption3.sty
% Redefine something here?
% \makeatletter
% \renewcommand*\caption@hrule{\hrule\@height\z@}
% \makeatother

\begin{document}

\begin{table}\centering
\begin{threeparttable}
\caption{A simple table}
\resetlinenumber[1]
\begin{edtable}{tabular}{l*{3}{c}}
                          & \multicolumn{1}{c}{(1)}   & \multicolumn{1}{c}{(2)}   & \multicolumn{1}{c}{(3)}   \\
Variable 1                &  1.85                     & 0.92                      & 1.11                      \\
                          & (0.34)                    & (0.24)                    & (0.14)                    \\
\end{edtable}
\end{threeparttable}
\end{table}

\end{document}

Best Answer

After some headaches:

\documentclass{scrartcl}

\usepackage[edtable]{lineno}
\usepackage{caption}
\usepackage{threeparttable}

\usepackage{xpatch}
\makeatletter
\newenvironment{etabular}
  {\resetlinenumber[1]\let\@centercr\relax\edtable{tabular}}
  {\endedtable}
\newenvironment{etabular*}
  {\resetlinenumber[1]\let\@centercr\relax\edtable{tabular*}}
  {\endedtable}

\catcode`\*=11
\xpatchcmd{\threeparttable}
 {\TPT@hookarg{tabularx}}
 {\TPT@hookarg{tabularx}\TPT@hookin{etabular}\TPT@hookarg{etabular*}}
 {}{}
\catcode`\*=12
\makeatother


\begin{document}

\begin{table}\centering
\begin{threeparttable}[b]
\caption{A simple table}
\begin{etabular}{l*{3}{c}}
           & \multicolumn{1}{c}{(1)} & \multicolumn{1}{c}{(2)} & \multicolumn{1}{c}{(3)} \\
Variable 1 &  1.85\tnote{1}          & 0.92                    & 1.11                    \\
           & (0.34)                  & (0.24)                  & (0.14)                  \\
\end{etabular}
\begin{tablenotes}
\item[1] A note
\end{tablenotes}
\end{threeparttable}
\end{table}

\end{document}
  1. The edtable environment should be made known to threeparttable;

  2. however we can't pass it to the hooks, because edtable has an optional argument, so I use a "wrapper" for it;

  3. the command \resetlinenumber is not allowed in threeparttable, so I hide it in the new environment;

  4. a \@centercr emitted by edtable causes the infamous "Perhaps a missing \item" error, so I neutralize it;

  5. the \threeparttable environment is patched to made the etabular environment known to it (see this answer).

Similar patches are made for the etabular* environment that internally calls \begin{edtable}{tabular*}. One has to use \TPT@hookarg instead of \TPT@hookin, in this case, like for tabular*.

enter image description here