1

I have the following packages loaded:

\usepackage[]{multirow}
\usepackage{longtable}
\usepackage{csquotes}
\usepackage{tabulary}
\usepackage{array}
\newcolumntype{L}{>{\centering\arraybackslash}m{3cm}}
\usepackage{booktabs,dcolumn,caption}
\captionsetup{labelsep=newline,singlelinecheck=false} % optional
\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"

And within my document have:

\begin{table}[!ht]
\centering
\begin{longtable}{|L|c|L|}\hline
\textbf{Example Sentence} & \textbf{Category} & \textbf{Explanation}
\\\hline
\multicolumn{1}{|m{3cm}|}{We send the EU £350m a week}
 & \multicolumn{1}{|m{3cm}|}{Use a real number, but change its meaning to be in the wrong context
}&  \multicolumn{1}{m{3cm}|}{This is a true National Statistic but it “is not an amount of money that the UK pays to the EU”. In fact, the UK’s net contribution is more like £250m a week which equates to £136m. }
\\\hline
\multicolumn{1}{|m{3cm}|}{Potential cost of leaving the EU as £4,300 per household per year, rather than as the equivalent £120bn for the whole country}
 & \multicolumn{1}{|m{3cm}|}{Make the number look big (but not too big)}&  \multicolumn{1}{m{3cm}|}{Insert text}
\\\hline
\end{longtable}
\end{table}

Altered from this source.

I have a few issues:

First two rows of table

  1. At the moment, everything is centre aligned and so the text contains too much spacing instead of being left aligned and tight
  2. I want to make the whole table wrap to its contents. At the moment I limit everything on a per column basis to 3cm but actually I want to limit the whole table to be at max the width of my page (if that is a variable somewhere?) and autowrap.
  3. My table doesn't spill over the page as I thought longtable would do. If I remove the table wrapping in the longtable I cannot specify [!ht] so my table would appear on some random part of the page.

How do I solve this?

  • as others have said you should never put longtable in a table. But also [!ht] isn't usually a good option to use for tables, it prevents the float being put on a float or bottom of a page (as no b or p) so makes it far more likely that the float can not be placed and drifts to the end of the document, and ! means that the typographic constraints set by the document class are ignored, which presumably typically makes worse output – David Carlisle Aug 21 '16 at 21:55
2

Let's examine your claims:

At the moment, everything is centre aligned and so the text contains too much spacing instead of being left aligned and tight

This claim is incorrect. You do specify the L column type (which you've defined to center-set its contents), but then you override this setting via \multicolumn{1}{|m{3cm}|}{...} wrappers. The m column type fully justifies its contents. What one sees in your screenshot is not centered text but fully justified text with large "holes" between lots of words.

To get ragged-right formatting, either use

\multicolumn{1}{|>{\raggedright\arraybackslash}m{3cm}|}{...}

wrappers or remove the wrappers entirely and replace \centering with \raggedright in the definition of the L column type.

...I want to limit the whole table to be at max the width of my page (if that is a variable somewhere?) and autowrap

Have you looked into the ltxtable environment? Use it in conjunction with longtable environments.

My table doesn't spill over the page as I thought longtable would do. If I remove the table wrapping in the longtable I cannot specify [!ht] so my table would appear on some random part of the page.

The premise that longtable is a floating environment is false. It is not. The reason why [ht!] has no effect when applied to a longtable is that a longtable always starts right where it's encountered in the input stream. Thus: Never encase a longtable inside a table. Never, ever. No way, no how.


Here's how I would re-work your code. Note that I've re-organized the material inside the longtable to distinguish more clearly between header, footer, and body of the table. I've also gotten rid of all vertical lines.

\documentclass{article}
\usepackage{longtable,array,ragged2e}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}p{3.75cm}}
\usepackage{booktabs,caption}
\captionsetup{labelsep=newline,singlelinecheck=false}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[british]{babel}
\begin{document}
\begin{longtable}{@{}LLL@{}}
\toprule
\textbf{Example Sentence} & \textbf{Category} & \textbf{Explanation}\\
\midrule
\endhead
\bottomrule
\endlastfoot
We send the EU £350m a week & 
Use a real number, but change its meaning to be in the wrong context&  
This is a true National Statistic but it ``is not an amount of money that the UK pays to the EU''. In fact, the UK's net contribution is more like £250m a week which equates to £136m.\\
\midrule
Potential cost of leaving the EU as £4,300 per household per year, rather than as the equivalent £120bn for the whole country & 
Make the number look big (but not too big)&  
Insert text\\
\end{longtable}
\end{document}

enter image description here

| improve this answer | |
  • How do I make it so the third column is slightly wider than the others as it is likely to be the longest? – Dhruv Ghulati Aug 22 '16 at 21:46
  • 1
    @DhruvGhulati - Let's assume you still want the sum of the three column widths to be 11.25cm. First, change the definition of the L column type to take a parameter: \newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}. Second, start the longtable as follows, say: \begin{longtable}{@{}L{3.5cm}L{3.5cm}L{4.25cm}@{}}. – Mico Aug 22 '16 at 22:31
  • Hi @Mico, this may benefit other readers, but I added \caption{The caption of the table} \label{table:somename} just above \toprule but am now getting a compilation error. I want a caption to appear on top of the table. – Dhruv Ghulati Aug 23 '16 at 14:55
  • @DhruvGhulati - Did you place a double backslash (line break directive) after\caption? – Mico Aug 23 '16 at 15:28
  • 1
    @DhruvGhulati - So, indeed, your code is missing a line-break directive after \caption and \label (and before \toprule). – Mico Aug 23 '16 at 16:04
0

A longtable is not a float, so you must not use the table environment.

I propose two solutions, based on ltablex, which brings the functionalities of longtable to tabularx. One solution use vertical and horizontal lines, the other uses the rules from booktabs, without vertical lines:

\documentclass{article}
\usepackage[utf8]{inputenc} %
\usepackage[T1]{fontenc} %
\usepackage{geometry}%
 \usepackage{array,ltablex, makecell}%
\renewcommand\theadfont{\normalsize\bfseries}
\setcellgapes{4pt}
\usepackage{multirow}
\usepackage{csquotes}
\usepackage{tabulary}
\usepackage{ragged2e} \usepackage{array}
\usepackage{booktabs,dcolumn,caption}
\captionsetup{labelsep=newline,singlelinecheck=false} % optional
\newcolumntype{d}[1]{D{.}{.}{#1}} % "decimal" column type
\renewcommand{\ast}{{}^{\textstyle *}} % for raised "asterisks"

\usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.2pt}

\begin{document}


\centering\keepXColumns
\makegapedcells
\begin{tabularx}{\linewidth}{|*{3}{ >{\RaggedRight\arraybackslash}X|}}%
\hline %
 \thead{Example Sentence} & \thead{Category} & \thead{Explanation} \\%
\hline
We send the EU £\,350\,m a week
 & Use a real number, but change its meaning to be in the wrong context
& This is a true National Statistic but it “is not an amount of money that the UK pays to the EU”. In fact, the UK’s net contribution is more like £\,250\,m a week which equates to £\,136\,m. \\%
\hline
Potential cost of leaving the EU as £\,4,300 per household per year, rather than as the equivalent £\,120\,bn for the whole country & Make the number look big (but not too big) & Insert text \\%
\hline
\end{tabularx}%
\vskip1cm

\begin{tabularx}{\linewidth}{*{3}{ >{\RaggedRight\arraybackslash}X}}%
\toprule %
 \thead{Example Sentence} & \thead{Category} & \thead{Explanation} \\%
\midrule
We send the EU £\,350\,m a week
 & Use a real number, but change its meaning to be in the wrong context
& This is a true National Statistic but it “is not an amount of money that the UK pays to the EU”. In fact, the UK’s net contribution is more like £\,250\,m a week which equates to £\,136\,m. \\%
\addlinespace
Potential cost of leaving the EU as £\,4,300 per household per year, rather than as the equivalent £\,120\,bn for the whole country & Make the number look big (but not too big) & Insert text \\%
\bottomrule
\end{tabularx}

\end{document} 

enter image description here

| improve this answer | |

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.