5

I am trying to accomplish something like this: footnote in table with hyperlinks

Except using the caption package along with it, which I need for other things. The two approaches don't seem to mesh well together and I can't seem to figure out how to get around it. The warning I get is the following:

Package caption Warning: \label without proper \caption on input line 32. See the caption package documentation for explanation.

And the references don't resolve (I get the dreaded ?? in their place). If I try to add a \caption prior to the \label for the \tnotex entries, it basically just tries to add multiple captions to the same table in my caption font.

\documentclass[letterpaper]{article}

% This package allows for footnoting within tables
\usepackage{enumitem,booktabs,cfr-lm}
\usepackage{tabularx}
\usepackage[referable]{threeparttablex}
\renewlist{tablenotes}{enumerate}{1}
\makeatletter
\setlist[tablenotes]{label=\tnote{\alph*}, ref=\alph*, itemsep=\z@, topsep=\z@skip, partopsep=\z@skip,parsep=\z@,itemindent=\z@,labelindent=\tabcolsep,labelsep=.2em,leftmargin=*,align=left,before={\footnotesize}}
\makeatother

\usepackage{caption}

\begin{document}

\begin{table}[ht]       
    \centering            
    \begin{threeparttable}  
        \begin{tabular}{|l|c|c|}
            \hline
            A & 1 & 2 \tnote{1} \\
            \hline
            B & 2 & 1 \\
            \hline
            C & 3\tnotex{tn:2} & 3 \\
            \hline
            Line & producing & space. \\
            \hline
        \end{tabular}
        \begin{tablenotes}
            \item[1] This is the first note.
            \item[2] \label{tn:2} This is the hyperlinked note.
        \end{tablenotes}      
    \end{threeparttable}
    \caption{A table caption.}
\end{table}% 

\end{document}

It works fine without the caption package, but I use that for setting caption fonts and colors in my document. Does anybody know a workaround?

6
  • Just for fun (not at a pc so cannot test), what happens if you add a label after the caption? Is it even threeparttablex related? What happens if you remove threeparttable but keep the list you've made? My guess is the same error. I guess it is the caption vs label noob test (common first time user error of not placing the label after the caption)
    – daleif
    Feb 17, 2016 at 21:22
  • @cfr I know about why the op changed the list, just wanted to rule out that the problem should stem from threeparttablex
    – daleif
    Feb 17, 2016 at 23:05
  • @daleif It isn't clear the OP knows why the list was changed, actually. That code is intended to automate the numbering of the table notes which makes no sense if you are going to say \item[1].
    – cfr
    Feb 17, 2016 at 23:22
  • 1
    No, it does not work fine without the caption package. You get an empty reference instead of the desired "2". See my answer below.
    – user2574
    Feb 21, 2016 at 9:19
  • 1
    @daleif Consider it disconfirmed in light of later testing, controlling for factors which probably mistakenly suggested success initially. Cleaned up to remove my erroneous comments. Sorry about the confusion.
    – cfr
    Feb 21, 2016 at 20:34

3 Answers 3

7

The package threeparttablex loads threeparttable and can certainly be used, as is clear from other answers, including the one linked from the question, to customise the standard tablenotes environment for use with tabular etc. and is not limited to use with longtable as suggested in another answer.

In fact, the code you have works fine for me with only a couple of minor changes. You are redefining tablenotes to enable automatic enumeration but then overriding that with hard-coded labels. This doesn't make sense. If you want the non-recommended-not-best-practice Arabic numerals as markers, just change the customisation of tablenotes appropriately.

\setlist[tablenotes]{label=\tnote{\arabic*}, ref=\arabic*, itemsep=0pt, topsep=0pt, partopsep=0pt ,parsep=0pt, itemindent=0pt, labelindent=\tabcolsep, labelsep=.2em, leftmargin=*, align=left, before={\footnotesize}}

and then write

    \begin{tablenotes}
      \item This is the first note.
      \item \label{tn:2} This is the hyperlinked note.
    \end{tablenotes}

If you prefer to write \item[1] etc., then don't redefine the tablenotes environment in the first place.

With these changes and the addition of \usepackage{hyperref}, the hyperlinked note works fine for me:

hyperlinked tablenote

Complete code:

\documentclass[letterpaper]{article}
\usepackage{enumitem,cfr-lm}% you aren't using booktabs
\usepackage{tabularx}
\usepackage[referable]{threeparttablex}
\renewlist{tablenotes}{enumerate}{1}
\setlist[tablenotes]{label=\tnote{\arabic*}, ref=\arabic*, itemsep=0pt, topsep=0pt, partopsep=0pt ,parsep=0pt, itemindent=0pt, labelindent=\tabcolsep, labelsep=.2em, leftmargin=*, align=left, before={\footnotesize}}

\usepackage{hyperref}
\usepackage{caption}

\begin{document}
\begin{table}[ht]
  \centering
  \begin{threeparttable}
    \begin{tabular}{|l|c|c|}
      \hline
      A & 1 & 2 \tnote{1} \\
      \hline
      B & 2 & 1 \\
      \hline
      C & 3\tnotex{tn:2} & 3 \\
      \hline
      Line & producing & space. \\
      \hline
    \end{tabular}
    \begin{tablenotes}
      \item This is the first note.
      \item \label{tn:2} This is the hyperlinked note.
    \end{tablenotes}
  \end{threeparttable}
  \caption{A table caption.}
\end{table}
\end{document}
17
  • 1
    +1: I've found out my error. I had a misplaced\noalign message which made me think it could only work with longtable or latblex. Actually the problem was only where to put the captioncommand, which differs in both cases. I must say the documentation is not very clear to me…
    – Bernard
    Feb 18, 2016 at 0:05
  • @Bernard To be fair, I am pretty sure I relied partly on the source code when I did this (without hyperref in my case, but that turns out to not matter). I probably overreacted to the suggestion that the OP was just plain confused when I knew that I was using something very, very like it perfectly successfully. (And recently - I knew I'd read this stuff within the past three or four months.) And the linked answer does make the point right off that threeparttablex can extend threeparttable's functionality using referable.
    – cfr
    Feb 18, 2016 at 0:27
  • And also \setTableNotesFont. Well, I've learnt something! Thanks for your explanations.
    – Bernard
    Feb 18, 2016 at 0:31
  • 1
    Ah! I saw that differently. Numbering from some order on the cells of the table; I think it would be tricky, as there are several possibilities to define a total order.
    – Bernard
    Feb 18, 2016 at 1:47
  • 1
    @AxelSommerfeldt Thanks for your patience in making this point. I checked and I'm sure you're right about why I thought it (as well as its not being true). I've edited accordingly. And removed my erroneous comment.
    – cfr
    Feb 21, 2016 at 20:31
7

First of all, this is not an incompatibility between caption and threeparttablex package. The caption package only gives a hint that something is going wrong here, and it's correct about this.

But lets first start with the default implementation of \item, offered by LaTeX2e itself:

\def\@item[#1]{%
  ...
  \if@noitemarg
    \@noitemargfalse
    \if@nmbrlist
      \refstepcounter\@listctr
    \fi
  \fi
  ...}

As one can see here, \item will only produce a reference (which can be used by \ļabel later on) if the list is numbered and \item was used without optional argument. So especially \item[2] will not generate a reference which can be used by \label and \ref. (This seems to be a design decision made by Leslie Lamport.)

Example document:

\documentclass{article}

\begin{document}

\ref{item1} \ref{item2}

\begin{enumerate}
\item \label{item1} Text
\item[2] \label{item2} Text
\end{enumerate}

\end{document}

Both \ref will output 1 here (and not 1 and 2) because only the \item without optional argument is generating a reference, so both \label are actually using the same reference created by the \item without optional argument.

The threeparttablex package redefines \item (inside tablenotes only), so \item[2] will produce a reference (by defining \@currentlabel): [1]

\renewcommand\tablenotes{%
  \let\TPTL@item=\item
  \renewcommand\item[1][]{\TPTL@item[##1]%
    \phantomsection\protected@edef\@currentlabel{##1}}
  ...}

Example document:

\documentclass[letterpaper]{article}
\usepackage[referable]{threeparttablex}

\begin{document}

\begin{table}[ht]
    \centering
    \begin{threeparttable}
        \begin{tabular}{|l|c|c|}
            \hline
            A & 1 & 2 \tnote{1} \\
            \hline
            B & 2 & 1 \\
            \hline
            C & 3 \tnotex{tn:2} & 3 \\
            \hline
            Line & producing & space. \\
            \hline
        \end{tabular}
        \begin{tablenotes}
            \item[1] This is the first note.
            \item[2] \label{tn:2} This is the hyperlinked note.
        \end{tablenotes}
    \end{threeparttable}
    \caption{A table caption.}
\end{table}

\end{document}

The outcome is fine, and \tnotex{tn:2} will produce a 2 as expected. (Note: One can add the usage of the caption package here without problems.)

Now we will add the usage of \renewlist{tablenotes} offered by the enumitem package:

\documentclass[letterpaper]{article}
\usepackage[referable]{threeparttablex}

\usepackage{enumitem}
\renewlist{tablenotes}{enumerate}{1}

\begin{document}

\begin{table}[ht]
    \centering
    \begin{threeparttable}
        \begin{tabular}{|l|c|c|}
            \hline
            A & 1 & 2 \tnote{1} \\
            \hline
            B & 2 & 1 \\
            \hline
            C & 3 \tnotex{tn:2} & 3 \\
            \hline
            Line & producing & space. \\
            \hline
        \end{tabular}
        \begin{tablenotes}
            \item[1] This is the first note.
            \item[2] \label{tn:2} This is the hyperlinked note.
        \end{tablenotes}
    \end{threeparttable}
    \caption{A table caption.}
\end{table}

\end{document}

Upps, the 2 is gone now. What has happened?

Well, the \renewlist{tablenotes}{enumerate}{1} re-defines the tablenotes environment to use an ordinary enumerate environment instead. And as we know from above \item[2] inside an ordinary enumerate environment will not generate a reference for usage with \label at all, so this eliminats the feature offered by threeparttablex that \item[xxx] actually will generate a reference. So \label{tn:2} will now pick the reference instead generated by the last command which has generated one. But there isn't any, so the output is simply empty. Adding a \section demonstrates this:

\documentclass[letterpaper]{article}
\usepackage[referable]{threeparttablex}

\usepackage{enumitem}
\renewlist{tablenotes}{enumerate}{1}

\begin{document}

\section{Test}  % <= This line added

\begin{table}[ht]
    \centering
    \begin{threeparttable}
        \begin{tabular}{|l|c|c|}
            \hline
            A & 1 & 2 \tnote{1} \\
            \hline
            B & 2 & 1 \\
            \hline
            C & 3 \tnotex{tn:2} & 3 \\
            \hline
            Line & producing & space. \\
            \hline
        \end{tabular}
        \begin{tablenotes}
            \item[1] This is the first note.
            \item[2] \label{tn:2} This is the hyperlinked note.
        \end{tablenotes}
    \end{threeparttable}
    \caption{A table caption.}
\end{table}

\end{document}

Now \tnotex{tn:2} will produce a 1 (instead of the expected 2) because \label{tn:2} (still) does not point to the \item[2] but to the (first) section instead.

But why does the example document offered by cfr gives the correct output? Because it uses \item without optional argument, and as we know from above this will always produce proper references.

Now the last part of the story: What does the caption package has to do with all of this?

Well, the caption package has a feature which detects \label without proper reference within figure, table, threeparttable etc. And since this is exactly the problem here this feature will strike.

BUT: I confess that the content of the warning message offered by the caption package is misleading here. I choose the text "\label without proper \caption" since a \label without a leading \caption is usually the problem here. I will change it to the more technically correct text "\label without a proper reference" in the next version.

[1] This is a documented feature of the threeparttablex package: "By using the package option »referable« we will change a few threeparttable internals such that (a) The manually specified note markers issued by \item[...] is referable, and ..." (Taken from the threeparttablex documentation, as of May 22, 2014)

2
  • 1
    Great explanation! However, it is not really an incompatibility between the two packages but between certain uses of them. In truth, the only reason I redefined tablenotes in terms of an enumitem list in the first place was to automate the numbering of the \items within the list. If you are going to hard-code those labels anyway, then there isn't much reason to use enumitem to redefine tablenotes at all. At least, that's my way of thinking about it.... So it is fine to use these 2 packages together provided you either (1) don't use enumitem here or (2) automate tablenotes.
    – cfr
    Feb 21, 2016 at 19:28
  • 1
    @cfr Thanks for the feedback! Yes, you are right about the "incompatibility", so I just dropped that part of the sentence.
    – user2574
    Feb 21, 2016 at 19:57
4

My workaround is probably not exactly what you were looking for, but for tables with footnotes, I think it is a good solution to use a tabularx environment and put the footnote in a final p-column with the width of the table specified. I also recommend inline enumerations as footnote to use the full width of table. Macro \tabref is used to customize the appearance of the references. Putting it together:

\documentclass[letterpaper]{article}

\usepackage[inline]{enumitem}
\usepackage{booktabs,cfr-lm,tabularx,ragged2e}
\usepackage{caption,hyperref}
\captionsetup[table]{position=top,aboveskip=6pt}

\newcommand*{\tabref}[1]{\textsuperscript{\ref{#1}}}

\begin{document}

\begin{table}[ht]
  \centering
  \captionbox{A table caption made somewhat longer to see the
    effect\label{tab:example}}{%
    \begin{tabularx}{0.5\textwidth}{@{}c>{\hfil}X<{\hfil}c@{}}
      \toprule
      Line & producing & space \\
      \midrule
      A & 1 & 2\tabref{tn:1} \\
      B & 2 & 1 \\
      C & 3\tabref{tn:2} & 3 \\
      \bottomrule
      \multicolumn{3}{@{}p{0.5\textwidth}@{}}{\RaggedRight
        \begin{enumerate*}
        \item  This is the first note.\label{tn:1}
        \item  This is the hyperlinked note\label{tn:2}.
        \end{enumerate*}%
      }%
    \end{tabularx}%
  }%
\end{table}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .