1

I am trying to achieve same thing as in this question, but with longtable environment. Table itself is centered.

Using caption package and

\captionsetup[longtable]{justification=raggedright,singlelinecheck=off}

makes caption aligned to the left, but the caption sticks past the left edge of the table.

And I can't just add margin= or something, because my tables are not of the same width.

Is there a way to correct this, or maybe an alternative to longtable?


Example document:

\documentclass[a5paper,12pt,titlepage]{article}
\usepackage[a5paper]{geometry}

\usepackage{longtable}
\usepackage{caption}
\captionsetup[longtable]{skip=0pt,justification=raggedright,singlelinecheck=off}


\usepackage{lipsum}

\begin{document}

\lipsum[3-4]

\begin{longtable}{l|p{2.5cm}|p{2.5cm}}
%----------------------------------------
\caption{This label isn't where I want it}\label{tab:test}\\ \hline
Name & Description & Example\\
\endfirsthead
%----------------------------------------
\caption*{Table \ref{tab:test} (continued)}\\\hline
\endhead
%----------------------------------------
\hline
Something A & here's supposed to be some text & some text here \\ \hline
Something B & description of B & something more about B\\ \hline
Something C & describe C here & some example involving C \\ \hline
Something D & dfjashdfkjha sdflhasdkf hfsadf asdf & dsjkdlaskd ksjdla jdlkasj \\ \hline
\end{longtable}
\end{document}

Caption is aligned like this:

enter image description here

This is how I want it to look:

enter image description here

| improve this question | | | | |
  • Welcome to TeX.SX! A small example would improve the question. – egreg Jan 19 '18 at 23:27
  • Perhaps tabularx with package ltablex? It brings the functionalities of longtable to tabularx. – Bernard Jan 19 '18 at 23:27
2

enter image description here

enter image description here

you can determine caption start width by setting \LTcapwidth to your table width. since its width is not know (in advance) it can be only by "trial and error" method ...

\documentclass[a5paper,12pt,titlepage]{article}
\usepackage[a5paper]{geometry}

\usepackage{longtable}
\usepackage{caption}
\captionsetup[longtable]{skip=0pt,justification=raggedright,singlelinecheck=off}

\usepackage{lipsum}

\begin{document}
\lipsum[3-4]

{\setlength\LTcapwidth{0.85\linewidth}
\begin{longtable}{l|p{2.5cm}|p{2.5cm}}
%----------------------------------------
\caption{This label isn't where I want it}\label{tab:test}\\ \hline
Name & Description & Example\\
\endfirsthead
%----------------------------------------
\caption*{Table \ref{tab:test} (continued)}\\\hline
\endhead
%----------------------------------------
\hline
Something A & here's supposed to be some text & some text here \\ \hline
Something B & description of B & something more about B\\ \hline
Something C & describe C here & some example involving C \\ \hline
Something D & dfjashdfkjha sdflhasdkf hfsadf asdf & dsjkdlaskd ksjdla jdlkasj \\ \hline
\end{longtable}
}
\end{document}

rdit:

in case, that you like use ltablex the width caption box width is the same as the width of table. therefore, it is known in advance and setting of table is simpler. for example:

\documentclass[a5paper,12pt,titlepage]{article}
\usepackage[a5paper]{geometry}

\usepackage{ltablex}
\usepackage{caption}
\captionsetup[longtable]{skip=0pt,justification=raggedright,singlelinecheck=off}

\usepackage{lipsum}

\begin{document}
\lipsum[3-4]

{\setlength\LTcapwidth{0.9\linewidth}
\begin{tabularx}{0.9\linewidth}{l|X|X}
%----------------------------------------
\caption{This label is where I want it}\label{tab:test}\\
    \hline
Name & Description & Example\\
\endfirsthead
%----------------------------------------
\caption*{Table \ref{tab:test} (continued)}\\\hline
\endhead
%----------------------------------------
\hline
Something A & here's supposed to be some text & some text here \\ \hline
Something B & description of B & something more about B\\ \hline
Something C & describe C here & some example involving C \\ \hline
Something D & dfjashdfkjha sdflhasdkf hfsadf asdf & dsjkdlaskd ksjdla jdlkasj \\ \hline
\end{tabularx}
}
\end{document}

which gives:

enter image description here

| improve this answer | | | | |
  • I guess this can be used in conjunction with ltablex (suggested by @Bernard) if I use the width given to tabularx. – Hedede Jan 21 '18 at 9:22
  • yes, with it the situation is simpler since width of table is known in advance. similarly is when all columns are of p{...} type. – Zarko Jan 21 '18 at 10:16
  • @Hedede, i add example of use ltablex. now you can accept answer ... :-) – Zarko Jan 21 '18 at 12:23

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.