3

I want to create a table using longtable where all columns have a fixed length except the last one, which should take all remaining space. The table itself should be full page width.

I tried the following with longtable, but it is not working:

\documentclass{article}

\usepackage{longtable}
\usepackage{tabu}
\usepackage{array}
\usepackage{booktabs}

\setlength\parindent{0pt}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}

\begin{document}

\begin{longtable}[h]{p{2cm}l@{\extracolsep{\fill}}}
\toprule
Icon   & Description \\
\midrule
Icon 1 & Means this and that \\
Icon 2 & Means some other thing \\
\bottomrule
\end{longtable}

\end{document}

With tabu I would achieve something like this as follows:

\begin{tabu} to \textwidth {p{2cm}X}

Here's an online example: https://es.sharelatex.com/project/555e01773b14e98f7d55ed84

How to do the same using longtable?

Edit: I know I can do this with other environments. The question is specifically whether this can be achieved with longtable.

3 Answers 3

3

You can use the ltablex package, which brings the functionalities of longtable in tabularx environments:

\documentclass{article}

\usepackage[ nomarginpar]{geometry}%showframe,
\usepackage{ragged2e}
\usepackage{array}
\usepackage{ltablex}
\keepXColumns
\usepackage{booktabs}
\usepackage{lipsum}

\setlength\parindent{0pt}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}
\raggedbottom
\begin{document}

\begin{tabularx}{\linewidth}{p{2cm}>{\arraybackslash}X}
  \caption{A demo of the possibilities of \texttt{ltablex}}
  \\ \toprule
  Icon & Description\medskip
  \endfirsthead
  \midrule
  Icon & Description\medskip% \
  \endhead
  \midrule
  \endfoot
  \bottomrule
  \endlastfoot%\\
         & \lipsum[1] \\
  \midrule
  Icon 1 & Means this and that \lipsum[2] \\
  Icon 2 & Means some other thing\lipsum[3] \\
  Icon 3 & Means this and that \lipsum[4] \\
  Icon 4 & Means some other thing\lipsum[5]
\end{tabularx}

\end{document} 

enter image description here

8
  • Thank you. There are also other options such as longtabu. However I am looking for a way to achieve this with longtable ...
    – Grodriguez
    Commented May 21, 2015 at 17:24
  • s I said latablex combines longtable and tabularx (and loads both). See my updated answer for a demo.
    – Bernard
    Commented May 21, 2015 at 18:24
  • Yes I know what you said but I cannot use ltablex (these tables are generated by an external tool which uses longtable)
    – Grodriguez
    Commented May 22, 2015 at 7:39
  • You just have to replace the name of the environment and the preamble of the tables. That shouldn't be too hard, I guess. If there are many of them, search and replace with regular expressions should do it easily.
    – Bernard
    Commented May 22, 2015 at 7:56
  • That is exactly what I am doing now, but it introduces an extra step in the build process which I want to avoid. Hence my question: Can the same effect be achieved with longtable.
    – Grodriguez
    Commented May 22, 2015 at 8:09
2

The usual way to achieve this is to use ltxtable where long table meets tabularx. The interface is different from tabu, the command \LTXtable reads an external file containing your longtable, e.g.

\documentclass{article}

\usepackage{longtable}
\usepackage{tabu}
\usepackage{array}
\usepackage{booktabs}
\usepackage{filecontents,ltxtable}

\setlength\parindent{0pt}
\setlength\LTleft{0pt}
\setlength\LTright{0pt}

\begin{document}

\begin{filecontents}{longtab.tex}
\begin{longtable}{@{}p{2cm}X@{}}
  \toprule
  Icon   & Description \\
  \midrule
  Icon 1 & Means this and that \\
  Icon 2 & Means some other thing \\
  \bottomrule
\end{longtable}
\end{filecontents}

\LTXtable{1.0\linewidth}{longtab.tex}

\end{document}

ltablex does not have this restriction (see Bernard's example). Note that it loads longtable and tabluarx internally.

2

I found a way to achieve what I need by just adding an additional, "dummy" column. Just changing this:

\begin{longtable}[h]{p{2cm}l@{\extracolsep{\fill}}}

to this:

\begin{longtable}[h]{p{2cm}l@{\extracolsep{\fill}}l}

seems to do the trick.

Example: enter image description here

You must log in to answer this question.

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