1

I'm trying to create a custom table environment that has the following properties:

  • It may be very long (i.e., longer than a page) and so I need to use a longtable
  • It may (or may not) have footnotes.

I've included an example here of what I'm trying to do. I have two tables, an "uppercase" and a "lowercase" table. The uppercase table has no footnotes, while the lowercase table does.

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage[referable]{threeparttablex}
\usepackage{booktabs}

\title{MWE}
\author{jlconlin}

\newenvironment{letterTable}{%
  \begin{longtable}{rl}
    \toprule
    Letter & Index \\
    \midrule
  \endfirsthead
  continued \\
    \toprule
    Letter & Index \\
    \midrule
  \endhead
    \bottomrule
    \emph{Continued on next page}
  \endfoot
    \bottomrule
    \insertTableNotes
  \endlastfoot
}
{\end{longtable}}
\begin{document}
\maketitle

\begin{letterTable}
    A & 1  \\
    B & 2  \\
    C & 3  \\
    D & 4  \\
    E & 5  \\
    F & 6  \\
    G & 7  \\
    H & 8  \\
    I & 9  \\
    J & 10 \\
    K & 11 \\
    L & 12 \\
    M & 13 \\
    N & 14 \\
    O & 15 \\
    P & 16 \\
    Q & 17 \\
    R & 18 \\
    S & 19 \\
    T & 20 \\
    U & 21 \\
    V & 22 \\
    W & 23 \\
    X & 24 \\
    Y & 25 \\
    Z & 26 \\
\end{letterTable}
\begin{ThreePartTable}
  \begin{TableNotes}
    \item[a] \label{tn:a} lowercase A
    \item[b] \label{tn:b} lowercase B
    \item[c] \label{tn:c} lowercase C
    \item[d] \label{tn:d} lowercase D
    \item[e] \label{tn:e} lowercase E
    \item[f] \label{tn:f} lowercase F
    \item[g] \label{tn:g} lowercase G
    \item[h] \label{tn:h} lowercase H
    \item[i] \label{tn:i} lowercase I
    \item[j] \label{tn:j} lowercase J
    \item[k] \label{tn:k} lowercase K
    \item[l] \label{tn:l} lowercase L
    \item[m] \label{tn:m} lowercase M
    \item[n] \label{tn:n} lowercase N
    \item[o] \label{tn:o} lowercase O
    \item[p] \label{tn:p} lowercase P
    \item[q] \label{tn:q} lowercase Q
    \item[r] \label{tn:r} lowercase R
    \item[s] \label{tn:s} lowercase S
    \item[t] \label{tn:t} lowercase T
    \item[u] \label{tn:u} lowercase U
    \item[v] \label{tn:v} lowercase V
    \item[w] \label{tn:w} lowercase W
    \item[x] \label{tn:x} lowercase X
    \item[y] \label{tn:y} lowercase Y
    \item[z] \label{tn:z} lowercase Z
  \end{TableNotes}
  \begin{letterTable}
    a\tnotex{tn:a} & 1  \\
    b\tnotex{tn:b} & 1  \\
    c\tnotex{tn:c} & 1  \\
    d\tnotex{tn:d} & 1  \\
    e\tnotex{tn:e} & 1  \\
    f\tnotex{tn:f} & 1  \\
    g\tnotex{tn:g} & 1  \\
    h\tnotex{tn:h} & 1  \\
    i\tnotex{tn:i} & 1  \\
    j\tnotex{tn:j} & 1  \\
    k\tnotex{tn:k} & 1  \\
    l\tnotex{tn:l} & 1  \\
    m\tnotex{tn:m} & 1  \\
    n\tnotex{tn:n} & 1  \\
    o\tnotex{tn:o} & 1  \\
    p\tnotex{tn:p} & 1  \\
    q\tnotex{tn:q} & 1  \\
    r\tnotex{tn:r} & 1  \\
    s\tnotex{tn:s} & 1  \\
    t\tnotex{tn:t} & 1  \\
    u\tnotex{tn:u} & 1  \\
    v\tnotex{tn:v} & 1  \\
    w\tnotex{tn:w} & 1  \\
    x\tnotex{tn:x} & 1  \\
    y\tnotex{tn:y} & 1  \\
    z\tnotex{tn:z} & 1  \\
  \end{letterTable}
\end{ThreePartTable}
\end{document}

In this example, the uppercase table comes first—and LaTeX fails to compile with this error:

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.51 \begin{letterTable}

If I move the uppercase table (with no table footnotes) after the lowercase table (with table footnotes) then everything compiles just fine.

  • Why does the order matter here?
  • What can I do to fix it so that the order doesn't matter?
2
  • The problem is the use of \insertTaleNotes in the definition of letterTable. This doesn't make sense outside of a ThreePartTable (but for some reason does work if there was a ThreePartTable prior to that). A quick fix would be to add an optional argument which tells the letterTable that it is used inside of ThreePartTable(or do some witchcraft to detect that alone).
    – Skillmon
    Commented Dec 13, 2017 at 21:54
  • @Skillmon I thought that was the case. I tried to modify my custom environment so I could give it that optional argument, but couldn't figure out how to make that work. Suggestions?
    – jlconlin
    Commented Dec 13, 2017 at 21:58

2 Answers 2

2

You can try to test for the content of the tablenotesbody:

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage[referable]{threeparttablex}
\usepackage{booktabs}
\usepackage{etoolbox}
\title{MWE}
\author{jlconlin}
\makeatletter
\newenvironment{letterTable}{%
  \begin{longtable}{rl}
    \toprule
    Letter & Index \\
    \midrule
  \endfirsthead
%  continued \\
    \toprule
    Letter & Index \\
    \midrule
  \endhead
    \bottomrule
    \emph{Continued on next page}
  \endfoot
    \bottomrule
    \ifcsempty{TPTL@body}{}{%test
    \insertTableNotes 
    \csgdef{TPTL@body}{}% delete the body
    }
  \endlastfoot
}
{\end{longtable}}
\begin{document}
%\maketitle



\begin{letterTable}
    A & 1  \\
    B & 2  \\
    C & 3  \\
    D & 4  \\
    E & 5  \\
    F & 6  \\
    G & 7  \\
    H & 8  \\
    I & 9  \\
    J & 10 \\
    K & 11 \\
    L & 12 \\
    M & 13 \\
    N & 14 \\
    O & 15 \\
    P & 16 \\
    Q & 17 \\
    R & 18 \\
    S & 19 \\
    T & 20 \\
    U & 21 \\
    V & 22 \\
    W & 23 \\
    X & 24 \\
    Y & 25 \\
    Z & 26 \\

\end{letterTable}


\begin{ThreePartTable}
  \begin{TableNotes}
    \item[a] \label{tn:a} lowercase A
    \item[b] \label{tn:b} lowercase B
    \item[c] \label{tn:c} lowercase C
    \item[d] \label{tn:d} lowercase D
    \item[e] \label{tn:e} lowercase E
    \item[f] \label{tn:f} lowercase F
    \item[g] \label{tn:g} lowercase G
    \item[h] \label{tn:h} lowercase H
    \item[i] \label{tn:i} lowercase I
    \item[j] \label{tn:j} lowercase J
    \item[k] \label{tn:k} lowercase K
    \item[l] \label{tn:l} lowercase L
    \item[m] \label{tn:m} lowercase M
    \item[n] \label{tn:n} lowercase N
    \item[o] \label{tn:o} lowercase O
    \item[p] \label{tn:p} lowercase P
    \item[q] \label{tn:q} lowercase Q
    \item[r] \label{tn:r} lowercase R
    \item[s] \label{tn:s} lowercase S
    \item[t] \label{tn:t} lowercase T
    \item[u] \label{tn:u} lowercase U
    \item[v] \label{tn:v} lowercase V
    \item[w] \label{tn:w} lowercase W
    \item[x] \label{tn:x} lowercase X
    \item[y] \label{tn:y} lowercase Y
    \item[z] \label{tn:z} lowercase Z
  \end{TableNotes}
  \begin{letterTable}
    a\tnotex{tn:a} & 1  \\
    b\tnotex{tn:b} & 1  \\
    c\tnotex{tn:c} & 1  \\
    d\tnotex{tn:d} & 1  \\
    e\tnotex{tn:e} & 1  \\
    f\tnotex{tn:f} & 1  \\
    g\tnotex{tn:g} & 1  \\
    h\tnotex{tn:h} & 1  \\
    i\tnotex{tn:i} & 1  \\
    j\tnotex{tn:j} & 1  \\
    k\tnotex{tn:k} & 1  \\
    l\tnotex{tn:l} & 1  \\
    m\tnotex{tn:m} & 1  \\
    n\tnotex{tn:n} & 1  \\
    o\tnotex{tn:o} & 1  \\
    p\tnotex{tn:p} & 1  \\
    q\tnotex{tn:q} & 1  \\
    r\tnotex{tn:r} & 1  \\
    s\tnotex{tn:s} & 1  \\
    t\tnotex{tn:t} & 1  \\
    u\tnotex{tn:u} & 1  \\
    v\tnotex{tn:v} & 1  \\
    w\tnotex{tn:w} & 1  \\
    x\tnotex{tn:x} & 1  \\
    y\tnotex{tn:y} & 1  \\
    z\tnotex{tn:z} & 1  \\
  \end{letterTable}
\end{ThreePartTable}
\end{document}
2
  • This works. But I don't know what the magic commands do. Can you please provide a few more comments to indicate what voodoo you are invoking?
    – jlconlin
    Commented Dec 13, 2017 at 22:22
  • I'm simply testing if there are tablenotes (TableNotes would store them in \TPTL@body. And I empty this command after the insert to avoid that the command affect the next table. Commented Dec 13, 2017 at 22:23
2

With etoolboxes \AtBeginEnvironment this can be solved easily.

Here \myInsertTableNotes is used, which is initially defined with

\let\myInsertTableNotes\relax

so doing nothing. And with

\AtBeginEnvironment{ThreePartTable}{\let\myInsertTableNotes\insertTableNotes}

the macor is set to \insertTableNotes within ThreePartTable.

The code:

\documentclass{scrartcl}
\usepackage{longtable}
\usepackage[referable]{threeparttablex}
\usepackage{booktabs}
\usepackage{etoolbox}

\title{MWE}
\author{jlconlin}

\newenvironment{letterTable}{%
  \begin{longtable}{rl}
    \toprule
    Letter & Index \\
    \midrule
  \endfirsthead
  continued \\
    \toprule
    Letter & Index \\
    \midrule
  \endhead
    \bottomrule
    \emph{Continued on next page}
  \endfoot
    \bottomrule
    \myInsertTableNotes
  \endlastfoot
}
{\end{longtable}}
\let\myInsertTableNotes\relax
\AtBeginEnvironment{ThreePartTable}{\let\myInsertTableNotes\insertTableNotes}
\begin{document}
\maketitle

\begin{letterTable}
    A & 1  \\
    B & 2  \\
    C & 3  \\
    D & 4  \\
    E & 5  \\
    F & 6  \\
    G & 7  \\
    H & 8  \\
    I & 9  \\
    J & 10 \\
    K & 11 \\
    L & 12 \\
    M & 13 \\
    N & 14 \\
    O & 15 \\
    P & 16 \\
    Q & 17 \\
    R & 18 \\
    S & 19 \\
    T & 20 \\
    U & 21 \\
    V & 22 \\
    W & 23 \\
    X & 24 \\
    Y & 25 \\
    Z & 26 \\
\end{letterTable}
\begin{ThreePartTable}
  \begin{TableNotes}
    \item[a] \label{tn:a} lowercase A
    \item[b] \label{tn:b} lowercase B
    \item[c] \label{tn:c} lowercase C
    \item[d] \label{tn:d} lowercase D
    \item[e] \label{tn:e} lowercase E
    \item[f] \label{tn:f} lowercase F
    \item[g] \label{tn:g} lowercase G
    \item[h] \label{tn:h} lowercase H
    \item[i] \label{tn:i} lowercase I
    \item[j] \label{tn:j} lowercase J
    \item[k] \label{tn:k} lowercase K
    \item[l] \label{tn:l} lowercase L
    \item[m] \label{tn:m} lowercase M
    \item[n] \label{tn:n} lowercase N
    \item[o] \label{tn:o} lowercase O
    \item[p] \label{tn:p} lowercase P
    \item[q] \label{tn:q} lowercase Q
    \item[r] \label{tn:r} lowercase R
    \item[s] \label{tn:s} lowercase S
    \item[t] \label{tn:t} lowercase T
    \item[u] \label{tn:u} lowercase U
    \item[v] \label{tn:v} lowercase V
    \item[w] \label{tn:w} lowercase W
    \item[x] \label{tn:x} lowercase X
    \item[y] \label{tn:y} lowercase Y
    \item[z] \label{tn:z} lowercase Z
  \end{TableNotes}
  \begin{letterTable}
    a\tnotex{tn:a} & 1  \\
    b\tnotex{tn:b} & 1  \\
    c\tnotex{tn:c} & 1  \\
    d\tnotex{tn:d} & 1  \\
    e\tnotex{tn:e} & 1  \\
    f\tnotex{tn:f} & 1  \\
    g\tnotex{tn:g} & 1  \\
    h\tnotex{tn:h} & 1  \\
    i\tnotex{tn:i} & 1  \\
    j\tnotex{tn:j} & 1  \\
    k\tnotex{tn:k} & 1  \\
    l\tnotex{tn:l} & 1  \\
    m\tnotex{tn:m} & 1  \\
    n\tnotex{tn:n} & 1  \\
    o\tnotex{tn:o} & 1  \\
    p\tnotex{tn:p} & 1  \\
    q\tnotex{tn:q} & 1  \\
    r\tnotex{tn:r} & 1  \\
    s\tnotex{tn:s} & 1  \\
    t\tnotex{tn:t} & 1  \\
    u\tnotex{tn:u} & 1  \\
    v\tnotex{tn:v} & 1  \\
    w\tnotex{tn:w} & 1  \\
    x\tnotex{tn:x} & 1  \\
    y\tnotex{tn:y} & 1  \\
    z\tnotex{tn:z} & 1  \\
  \end{letterTable}
\end{ThreePartTable}
\end{document}

You must log in to answer this question.

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