5

This is based on egreg's code and comments to Indenting text in \tabfill (tabbing environment), in which a command \tabfill is defined so that it wraps lines in a tabbing environment.

One problem with that approach is that it leads to a lot of white space between words:

\documentclass{article}
\makeatletter
\newcommand\tabfill[1]{%
  \dimen@\linewidth
  \advance\dimen@\@totalleftmargin
  \advance\dimen@-\dimen\@curtab
  \parbox[t]\dimen@{%
    \leftskip=2em\hspace*{-2em}#1\ifhmode\strut\fi}%
}
\makeatother

\textwidth=.75\textwidth % just to make wrapping more evident

\begin{document}
\begin{tabbing}
\hspace{3.1cm} \= \kill
Column 1 \> Column 2\\
bla bla \> \tabfill{Include longs words like extracurricular}\\
bla bla \> \tabfill{bla bla}
\end{tabbing}
\end{document}

enter image description here

The proposed solution is to use \RaggedRight from the ragged2e package:

\documentclass{article}
\usepackage{showframe}
\usepackage{ragged2e}
\makeatletter
\newcommand\tabfill[1]{%
  \dimen@\linewidth
  \advance\dimen@\@totalleftmargin
  \advance\dimen@-\dimen\@curtab
  \parbox[t]\dimen@{%
    \RaggedRight\leftskip=2em\hspace*{-2em}#1\ifhmode\strut\fi}%
}
\makeatother

\textwidth=.75\textwidth % just to make wrapping more evident

\begin{document}
\begin{tabbing}
\hspace{3.1cm} \= \kill
Column 1 \> Column 2\\
bla bla \> \tabfill{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}\\
bla bla \> \tabfill{Include longs words like extracurricular}\\
bla bla \> \tabfill{bla bla}
\end{tabbing}
\end{document}

Although this fixes the white space between words, it doesn't hyphenate the word extracurricular as I would have expected, instead preferring to leave a lot of white space at the end of the first line:

enter image description here

So my question is, how can I wrap lines in a tabbing environment, yet maintain "normal" interword spaces and hyphenations?

6

Congratulations! You've found a bug in the code. :)

The strut added at the end makes the last word not subject to hyphenation.

Here's a correct version with \RaggedRight:

\documentclass{article}
\usepackage{showframe}
\usepackage{ragged2e}
\makeatletter
\newcommand\tabfill[1]{%
  \dimen@\linewidth
  \advance\dimen@\@totalleftmargin
  \advance\dimen@-\dimen\@curtab
  \parbox[t]\dimen@{%
    \RaggedRight\leftskip=2em\hspace*{-2em}#1\ifhmode\unskip\nobreak\strut\fi
  }%
}
\makeatother

\textwidth=.75\textwidth % just to make wrapping more evident

\begin{document}
\begin{tabbing}
\hspace{3.1cm} \= \kill
Column 1 \> Column 2\\
bla bla \> \tabfill{bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla}\\
bla bla \> \tabfill{Include longs words like extracurricular}\\
bla bla \> \tabfill{bla bla}
\end{tabbing}
\end{document}

enter image description here

You may also want to set

\finalhyphendemerits=0

before \RaggedRight, so TeX will be less unwilling to hyphenate the last but one line.

\makeatletter
\newcommand\tabfill[1]{%
  \dimen@\linewidth
  \advance\dimen@\@totalleftmargin
  \advance\dimen@-\dimen\@curtab
  \parbox[t]\dimen@{%
    \finalhyphendemerits=\z@
    \RaggedRight\leftskip=2em\hspace*{-2em}#1\ifhmode\unskip\nobreak\strut\fi
  }%
}
\makeatother

As suggested in comments by Martin Schröder (the author of ragged2e), one can use the special parameter \RaggedRightLeftskip and \dimexpr (or calc features).

\makeatletter
\newcommand\tabfill[1]{%
  \parbox[t]{\dimexpr\linewidth-\@totalleftmargin-\dimen\@curtab}{%
    \finalhyphendemerits=\z@
    \RaggedRightLeftskip=2em
    \RaggedRight\hspace*{-2em}#1\ifhmode\unskip\nobreak\strut\fi
  }%
}
\makeatother
  • \finalhyphendemerits=0 or \finalhyphendemerits=\z@? (I don't know what they mean, so). – Sverre Nov 4 '14 at 18:58
  • 1
    @Sverre It's the same, since we're in a \makeatletter context; \z@ is some nanosecond faster. – egreg Nov 4 '14 at 21:47
  • That looks like a job for calc or \dimexpr. And there is \RaggedRightLeftskip. – Martin Schröder Nov 6 '14 at 10:38

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.