4

I am new to StackExchange, so any suggestions to improve my question framing are welcome.

I have some trouble getting the caption of my table aligned. Since I need a landscape, multipage table, I am using threeparttablex combined with longtable. I figured out that commenting out the margin adjustments in line 10 in the MWE below fixes my problem, but I do like to keep my margins that way in the rest of my document.

Note: The captions of the other 'regular' threeparttables (no x, no longtable) in my document are fine. Also, I tried \centering at multiple locations in this table, which didn't help.

My question is: how to align the table caption with this table, while also keeping my margins at 2.6cm? Thanks in advance!

\documentclass{book}

\usepackage[flushleft]{threeparttable}
\usepackage{longtable}
\usepackage{pdflscape}
\usepackage{threeparttablex}
\usepackage{caption}
\usepackage{booktabs}

\usepackage[margin=2.6cm]{geometry} %commenting this out fixes the caption alignment

%adjust table caption setup
\DeclareCaptionLabelSeparator*{spaced}{\\[2ex]}
\captionsetup[table]{textfont=it,justification=raggedright,singlelinecheck=false,labelsep=spaced, skip=0pt}

\begin{document}

\begin{landscape}

\begin{ThreePartTable}
    \begin{TableNotes}
        \small
        \item \textit{Note.} Table notes
    \end{TableNotes}

    \begin{longtable}{p{1cm}p{1cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}}

        \caption{My caption}\\ 

        \toprule
        Header & Header & Header & Header & Header & Header & Header \\
        \midrule
        \endfirsthead

        \multicolumn{7}{l}{\textit{My caption (continued)}}\\ %cheating a little bit here
        \toprule
        Header & Header & Header & Header & Header & Header & Header \\
        \midrule    
        \endhead

        \midrule
        \multicolumn{7}{r}{{\textit{Continued on next page}}} \\
        \midrule
        \endfoot
        \bottomrule

        \insertTableNotes
        \endlastfoot

        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
        1 & 1 & 1 & 1 & 1 & 1 & 1\\
    \end{longtable}

\end{ThreePartTable}

\end{landscape}

\end{document}

Table caption not aligned with table

  • Thanks for your reply, @Zarko, but I need the singlelinecheck=false because my table caption should be on the top left (and not in the middle). – Elisabeth May 10 at 9:43
  • sorry, i misunderstood your question. – Zarko May 10 at 9:48
  • @Inquisitive Lurker, I hadn't thought of manually adjusting the caption yet and it does the job, thanks! Of course it's not super elegant and I am still wondering why this is happening, but for now it helps me continue :) – Elisabeth May 10 at 9:54
  • @Zarko no problem! – Elisabeth May 10 at 9:54
  • @Elisabeth I turned my comment into an answer, since I'd made a small mistake - you'll need to adjust the margins separately for each table, because they depend on table width. – Disenchanted Lurker May 10 at 10:13
1

There's probably a better, automatic solution for this, but as a quick fix, you can adjust the caption's margins manually:

\captionsetup{margin=1.4cm}

You could set that at the start of the document, but since your tables probably all have different widths, you'll likely have to adjust this separately for each of them: Place the above line at the beginning of each table, like this:

%[...]
\begin{longtable}{p{1cm}p{1cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}}

        \captionsetup{margin=1.4cm} % <---  adjust as needed
        \caption{My caption}\\ 
%[...]
1

you need to define width of caption:

\setlength\LTcapwidth{\dimexpr17cm+14\tabcolsep}

where 17cm is sum of columns widths and 14\tabcolsep sum of tabcolsep lengths around them. complete code is:

%%%% 3parttablex                                            %%%%%
%---------------------------------------------------------------%
%   my answer ...                                   2017-10-14  %
%   https://tex.stackexchange.com/questions/395852/             %
%       a-long-table-in-the-landscape-page/396077#396077        &
%---------------------------------------------------------------%
%   special:    table notes are inserted in                     %
%               \endfoot and \endlastfoot                       %
%---------------------------------------------------------------%

\documentclass[a4paper, 11pt, oneside]{book}
\usepackage[margin=30mm]{geometry}

\usepackage{ragged2e}
\usepackage{array,
            booktabs,
            longtable,
            makecell}
\usepackage[referable]{threeparttablex}
\usepackage{caption}
\DeclareCaptionLabelSeparator*{spaced}{\\[1ex]}
\captionsetup[table]{textfont=it,
                     justification=raggedright,
                     singlelinecheck=false,
                     labelsep=spaced,
                     skip=1ex}
\usepackage{siunitx}
\usepackage{pdflscape}

\usepackage{lipsum} % for dummy text in captions
\begin{document}
\begin{landscape}
    \sffamily
    \setlength\tabcolsep{4pt}
    \centering
\begin{ThreePartTable}[
\setlength\LTcapwidth{\dimexpr17cm+14\tabcolsep}
%
\begin{TableNotes}[para,flushleft]\footnotesize
\note{General table notes}

\item[*] first note
\item[**] second note
\end{TableNotes}
%
    \begin{longtable}{p{1cm}p{1cm}p{3cm}p{3cm}p{3cm}p{3cm}p{3cm}}
\caption{My caption. \lipsum[66]}\\
     \toprule
Header & Header & Header & Header & Header & Header & Header \\
    \midrule
\endfirsthead
\caption*{My caption. \lipsum[66]}\\
    \toprule
Header & Header & Header & Header & Header & Header & Header \\
    \midrule
\endhead
    \midrule
\multicolumn{7}{r}{{\textit{Continued on next page}}} \\
\endfoot
    \midrule[\heavyrulewidth]
    \insertTableNotes
\endlastfoot
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
1 & 1 & 1 & 1 & 1 & 1 & 1\\
\end{longtable}
    \end{ThreePartTable}
    \end{landscape}
\end{document}

which gives:

enter image description here

enter image description here

note: i slightly change design of your table (distances in caption, rules in table)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.