0

I have a table with three columns which include a description, a short abbreviation, and a value. The description might be a very long entry, so I use the p columntype to break the lines automatically. I am using longtable because the final table will span a couple of pages.

At the moment, I use this code

\documentclass{article}
\usepackage{longtable}

\begin{document}

\begin{longtable}{p{0.5\textwidth}cc} 
\hline
Column 2 &  Column 2 & Column 3\\ 
\hline
short entry & Entry s.1 & Value v.s1\\
  & Entry s.2 & Value v.s2 \\
\hline
here is a very long text in the first column that will be broken into multiple rows & Entry l.1 &  Value v.l1 \\
 & Entry l.2 &  Value v.l2 \\
\hline
\end{longtable}

\end{document}

to get this table:

current version

As you can see, if the entry in the first column is short, it is easy to align the last two columns. But if the text in the first column is broken into multiple lines, there naturally is a similar, but now empty, space in the last two lines. So in the example, what I would want to do is to "move up" Entry l.2 and Value v.l2 two "rows" up.

3 Answers 3

1

You can use \multirow from the multirow package for spanning a cell over multiple rows, fe.:

\documentclass{article}
\usepackage{longtable}
\usepackage{multirow}

\begin{document}

\begin{longtable}{p{0.5\textwidth}cc} 
\hline
Column 2 &  Column 2 & Column 3\\ 
\hline
short entry & Entry s.1 & Value v.s1\\
  & Entry s.2 & Value v.s2 \\
\hline
\multirow{2}{0.5\textwidth}{here is a very long text in the first column that will be broken into multiple rows} & Entry l.1 &  Value v.l1 \\
 & Entry l.2 &  Value v.l2 \\
 \\
\hline
\end{longtable}

\end{document}

For text wrapping you need to set the width of the column manually – in the example it's set to .5\textwidth. See also this answer to "Text wrapping in multirow columns".

EDIT: To avoid manually checking the rows your text is spanning you can use nested tables for the last two columns instead of using \multirow, fe.:

\documentclass{article}
\usepackage{longtable}

\begin{document}

\begin{longtable}{p{0.5\textwidth}c} 
\hline
Column 2 &\begin{tabular}{cc}Column 2 & Column 3\end{tabular}\\ 
\hline
short entry &\begin{tabular}{cc}
    Entry s.1 & Value v.s1\\
    Entry s.2 & Value v.s2
\end{tabular}\\
\hline
here is a very long text in the first column that will be broken into multiple rows &\begin{tabular}{cc}
    Entry l.1 & Value v.l1 \\
    Entry l.2 & Value v.l2
\end{tabular}\\
\hline
\end{longtable}

\end{document}

I think I'd prefer checking twice because I would try to avoid long text in tables if possible.

3
  • Thanks for your answer. I had tried to use the multirow package, but did not modify the second argument sufficiently. After changing it slightly to \multirow{3}{0.5\textwidth}, there is only a "underfull \vbox" warning left. So I basically have to always check first how many rows the cell will take, and have to compare manually for the column width, correct? Feb 15, 2016 at 10:49
  • @user3825755 see edit for an alternate solution which does not require you to always manually check the column width / number of rows etc.
    – Max
    Feb 15, 2016 at 11:21
  • Thanks for the update, I think I will stick with the first variant, as the problem will only occur on a few instances, and looks a bit "cleaner" (easier to avoid syntax errors) Feb 15, 2016 at 11:31
1

You can use booktabs package also.

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\begin{document}
\begin{longtable}{p{0.5\textwidth}p{3cm} p{3cm}} 
\toprule
Column 1 &  Column 2 & Column 3\\ 
\midrule
short entry & Entry s.1 & Value v.s1\\
& Entry s.2 & Value v.s2 \\
\midrule
There is a very long text in the  & Entry l.1 &  Value v.l1 \\
first column that will be broken  &Entry 1.2 & Value v.12\\
into multi rows&&\\
\bottomrule
\end{longtable}
\end{document}

booktabs

1
  • What is the specific contribution of the booktabs package, apart from the toprule etc? I was also using booktabs to get the nicer lines, but omitted that for a simper example. While your answer will of course work, it has to be manually adjusted for every new entry, and if the columnwidth is changed, it has to be changed all over again Feb 15, 2016 at 10:57
0

Since all your columns are p{some width}, you can use \newline inside a cell, and have less rows. Alternatively, you can use the makecell package, which allows for line breaks inside cells:

\documentclass{article}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{makecell}
\renewcommand\cellalign{lt}

\begin{document}

\begin{longtable}{p{0.5\textwidth}p{3cm} p{3cm}}
  \toprule
  Column 1 & Column 2 & Column 3 \\
  \midrule
  short entry & Entry s.1 & Value v.s1 \\
                                                                                    & Entry s.2 & Value v.s2 \\
  \midrule
  There is a very long text in the first column that will be broken into multi rows & \makecell{Entry l.1 & \\Entry 1.2} & \makecell{Value v.l1\\Value v.12} \\
  \addlinespace
  There is a very long text in the first column that will be broken into multi rows & Entry l.1\newline Entry 1.2 & Value v.l1\newline Value v.12 \\
  \bottomrule
\end{longtable}

\end{document} 

enter image description here

2
  • Actually, the type of the last two columns is "c" rather than "p" (I think you might have copied the code from murugan's answer). Does the makecell solution still work then? Feb 15, 2016 at 11:28
  • Yes, I modified his/her code and didn't notice this difference with yours. Makecell is done precisely for that, and it also allows for a common formatting. Another way would be to change p{some width} to >{\centering}p{some width} (loading the array package). The only thing you'll have to do manually is to decide how many entries will be grouped.
    – Bernard
    Feb 15, 2016 at 11:34

You must log in to answer this question.

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