# Longtable overflowing document - how do I define the width of a column?

I found this question that helped me defining a layout for a Liker questionnaire I have to create. The problem is that with very long questions that very same code is not working. Here is an example of what happens:

The code is basically the same as the one shown in the first reply of the previously linked question. The fact is that that code iteratively put Q1-10 as the question while I need to obviously write each question down. So this is the code I am using:

\documentclass{article}
\usepackage{amssymb}
\usepackage{array,longtable}
\begin{document}
\begin{longtable}{l*{5}{c}}
\multicolumn{1}{c}{Question} & Bad & Not Bad & Neutral & OK & Good \\
This is a very very very very very very very very very long question & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q2 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q3 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q4 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q5 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q6 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q7 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q8 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q9 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q10 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
\end{longtable}
\end{document}


This is the minimum (non) working example I could create that basically compiles into the resulting image I uploaded.

How can I resize the width of the "Question" block in order not to have the squares for the replies overflow the document?

Thanks!

• You can either use a p{<width>} column for the first column or try one of the packages that combine the features of longtable with tabularx such as ltablex, ltxtable, xltabular,... – leandriis May 12 at 11:06

Here is a possible solution uwing the xltabular package that combines the features of longtable(page breaks within a table) ad tabularx (automatic linebreaks in table cells):

\documentclass{article}
\usepackage{amssymb}
\usepackage{array,longtable}
\usepackage{xltabular}
\begin{document}
\begin{xltabular}{\textwidth}{X*{5}{c}}
\multicolumn{1}{c}{Question} & Bad & Not Bad & Neutral & OK & Good \\
This is a very very very very very very very very very long question & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q2 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q3 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q4 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q5 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q6 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q7 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q8 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q9 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q10 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
\end{xltabular}
\end{document}


Regarding the request in the comments: You can either use p type columns and specify the width and the alignment. This will cause an automatic linebreak. If you prefer manual linebreaks in the column headers, you might also want to use \makecell:

\documentclass{article}
\usepackage{amssymb}
\usepackage{array,longtable}
\usepackage{xltabular}
\usepackage{makecell}
\usepackage{ragged2e}
\begin{document}
\begin{xltabular}{\textwidth}{X>{\Centering}p{2cm}cccc}
\multicolumn{1}{c}{Question} & a very long column title & Not Bad & Neutral & OK & \makecell[cc]{a second \\ very very long \\ column title} \\
This is a very very very very very very very very very long question & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q2 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q3 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q4 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q5 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q6 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q7 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q8 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q9 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
Q10 & $\square$ & $\square$ & $\square$ & $\square$ & $\square$ \\
\end{xltabular}
\end{document}

• Thanks it worked wonders! I have one question: can I modify the width of the other columns too? I.e., instead of "Bad" I would like to put something longer and that breaks a bit the layout because the "Question" column gets very narrow. So I would like to fix the width of the other columns too! Thanks! – Masiar May 12 at 11:56
• @Masiar: I have edited my answer to add two versions on how to control the width of a longer colum headers. – leandriis May 12 at 12:06
• Thank you very much! – Masiar May 13 at 9:10