21

I know that you can combine tabularx and longtable functionality with either ltxtable or ltablex, but is there something similar for tabulary? My searching has come up dry when looking for any way to get tabulary to work with very long tables.

The table I am working with has large amounts of text that requires line breaking in multiple columns. Some cells have very little, and some have a lot, and I would like to see the column widths juggled to minimize total table length. I realize I could manually do this with the {>{\hsize=.5\hsize}X>{\hsize=1.5\hsize}X} functionality in tabularx, but what I'm making is intended to be a template that should work well with any particular arrangement of data that gets thrown in. Am I doomed to using tabularx and manually adjusting the hsize numbers each time I want to make a table?

  • 1
    TX isn't that bad: "doomed" probably isn't the word I'd use:-) If you don't need table headers repeated at the page breaks you could just inbox the TY table. If you do need headings probably isn't impossibly difficult to modify ltxtable so that it mixes TY and longtable instead of TX, but I'm busy this week so someone else might like the challenge:-) – David Carlisle Oct 17 '12 at 23:10
  • Yeah, the repeated table headers are unfortunately pretty key. I manually defined a few p columns in TX that pretty well handle things, but there is still an l column I can imagine getting a little strange in fringe cases that I'm not sure I can bear to hard code a width for. – Eric Oct 18 '12 at 15:20
  • @DavidCarlisle For those that don’t need the table headers repeated at page breaks, could you explain what “just inbox[ing]” a tabulary table means? Is wrapping in something like \mbox what does the trick? – caw Nov 13 '18 at 7:05
15

Updated to support setting longtable \LTleft and \LTright margins.

enter image description here

\documentclass{article}
\usepackage{longtable,tabulary}
\setlength\textheight{10\baselineskip}
\topmargin-4\baselineskip
\headheight0pt
\pdfpageheight18\baselineskip

\def\a{Red green blue yellow green. }
\def\b{One two three four five six. }
\makeatletter

\def\ltabulary{%
\def\endfirsthead{\\}%
\def\endhead{\\}%
\def\endfoot{\\}%
\def\endlastfoot{\\}%
\def\tabulary{%
  \def\TY@final{%
\def\endfirsthead{\LT@end@hd@ft\LT@firsthead}%
\def\endhead{\LT@end@hd@ft\LT@head}%
\def\endfoot{\LT@end@hd@ft\LT@foot}%
\def\endlastfoot{\LT@end@hd@ft\LT@lastfoot}%
\longtable}%
  \let\endTY@final\endlongtable
  \TY@tabular}%
\dimen@\columnwidth
\advance\dimen@-\LTleft
\advance\dimen@-\LTright
\tabulary\dimen@}

\def\endltabulary{\endtabulary}

\makeatother

\begin{document}


\begin{ltabulary}{LLL}
HEAD 1& HEAD 2& HEAD 3\endhead
\a\a&\a&\a\a\a\\
\b\b&\b&\b\b\b\\
x&y&z\\
x&y&z\\
x&y&z\\
x&y&z\\
\b\b&\a&\b\a\b\\
x&y&z\\
x&y&z\\
x&y&z\\
x&y&z\\
x&y&z\\
\end{ltabulary}


\noindent X\dotfill X

\LTleft=2cm

\begin{ltabulary}{LLL}
HEAD 1& HEAD 2& HEAD 3\endhead
\a\a&\a&\a\a\a\\
\b\b&\b&\b\b\b\\
x&y&z\\
x&y&z\\
x&y&z\\
x&y&z\\
\b\b&\a&\b\a\b\\
x&y&z\\
x&y&z\\
x&y&z\\
\end{ltabulary}

\noindent X\dotfill X
\end{document}
  • 2
    Thanks for this solution. The only problem with it is that it removes all the hyphenations. Is it possible to have them back? It is especially true for the cases when there is a long word in a narrow column. – v_2e Aug 21 '13 at 13:07
  • 4
    @v_2e tabulary L column is same as \begin{flushleft} which prefers leaving a line short over hyphenation, you can use J columns instead for justified columns with hyphenation (the usual latex default). words longer than the column width would hyphenate with either L or J but the first word of a paragraph doesn't hyphenate in TeX start the text with \hspace*{0pt} to get round that (true in general not just in this sort of table) – David Carlisle Aug 21 '13 at 13:10
  • 2
    To make captions work with this, see this answer. – justibus Aug 25 '14 at 11:47
  • 1
    This works excellent. I ended up using it quite heavily. Perhaps you should make an official release (with caption fix) – Andrey Pro Aug 2 '17 at 14:49
  • 1
    What is about the official release? Is there an official package for this solution? – buhtz Sep 8 '17 at 14:55

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.