[Tex/LaTex] Table with variable-width columns, width decided based on row content

tables

I'm typesetting some material in two columns, the left one left-adjusted and the right one right-adjusted (it's a list of songs in a concert, with song names on the left and composer names on the right). My approach so far has been to use \begin{tabular}{L{4.2cm}R{3.2cm}}
where the column definitions are as follows:

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

However, with this approach my table becomes a little too wide. Some rows have more content on the left side (long song names) and some rows have more content on the right side (long composer names), but none of them have both – so if I could find a way to adjust the column widths automatically based on the content, that would solve my problem.

I don't know if this is even possible with a tabular approach – and I have no problem with switching to some other approach if it helps. The ultimate goal is to be able to have rows in a list with part of the row left-aligned, and the rest of the row right-aligned:

|<- left edge of text         right edge of text->|
|The first song                        by Some Guy|
|                                   who lived then|
|                                                 |
|Some other song                   by Someone Else|
|and another one by the same guy    who also lived|
|                                                 |
|A fourth song    by a guy with a Really Long Name|
|                               aw, he's dead, Jim|

Note how the "other song by the same guy" and "fourth song" rows overlap, i.e. the right column of the latter starts before the end of the left column of the former. So instead of rigidly defining the column widths, I'd like to define the width of the entire table (between the pipes above) and have the rest done for me automagically.

What's the best approach to achieve this in (Xe)LaTeX?

Best Answer

enter image description here

\documentclass{article}

\newcommand\zz[2]{\par
\bigskip
\noindent\begin{tabulary}{\textwidth}{LR}#1&#2\end{tabulary}\par
}
\usepackage{tabulary}


% http://tex.stackexchange.com/questions/87097/is-it-possible-to-make-an-underfull-tabulary-spread-out/87543#87543
\makeatletter

\def\foo#1\def\TY@ratio#2#3!!{
\def\TY@checkmin{#1%
#3}}
\expandafter\foo\TY@checkmin!!

\makeatother

\setlength\textwidth{10cm}

\begin{document}


\noindent X\dotfill X

\zz{The first song}{by Some Guy who lived then}

\zz{Some other song}{by Someone Else}

\zz{and another one by the same guy}{who also lived}


\zz{A fourth song }{by a guy with a Really Long Name
                               aw, he's dead, Jim}


\noindent X\dotfill X

\end{document}
Related Question