4

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?

  • Note: I realize I could do this with \multicolum et al, but I would like an approach which lets me generate the content from somewhere else (just separating the columns with &, or whatever is appropriate) and then have the widths decided for me based on content on each row. – Tomas Aschan Oct 2 '13 at 8:19
  • Is it possible to abandon tables/arrays and build a custom object? I am thinking of a box/minipage (with frames if you need that) and then a command that puts an \hfill between its two parameters. – papabravo Oct 2 '13 at 8:31
  • tabulary columns are adjusted according to how much content is in each column. – David Carlisle Oct 2 '13 at 8:38
  • @DavidCarlisle: After skimming the readme for tabulary, I still don't find any way to do this without multicolums. Am I missing something? – Tomas Aschan Oct 2 '13 at 8:43
  • @papabravo: Yes, I'm open to any approach that gives me the desired result. Using a tabular environment (of any kind) is by no means a requirement - it just happens to be the approach I thought would work... :P – Tomas Aschan Oct 2 '13 at 8:44
3

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}
  • This is great, and I might be able to adjust it to what I want, but in its current form it's not good enough =( I'd like to improve the following: 1) The line breaks in my example should be honored. Composer and years on different lines, each song title on a separate line etc. 2) There should only be blank lines between entries for new composers, so the second and third lines in your example should not be separated by a blank line. 3) If possible, I'd like to disallow line breaks within an entry as the one that appears in your last song title. – Tomas Aschan Oct 2 '13 at 12:25
  • I wasn't sure if the 2nd and third in your ascii art were intended as one entry, you could put them back into the same \zz. If the left entry never breaks over a line you could use \mbox{#1} in the definition, otherwise to stop the column getting so narrow use \setlength\tymin{5cm} or whatever length is enough. If you mark newlines in the source with \newline (or just a blank line paragraph break) they will be honoured in the output – David Carlisle Oct 2 '13 at 12:31
  • Nice! With those changes, this was just what I needed. Thanks! – Tomas Aschan Oct 2 '13 at 15:20

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.