# Use a box to fill text to right margin

I often use an eqnarray (I know it's better to use align, but let's not worry about that for this question unless it changes the answer) as a way to list a bunch of definitions or descriptions of symbols. Here's an example:

\begin{eqnarray*}
f & \text{:} & \begin{minipage}[t]{\textwidth}The objective function that we're trying to maximize.\end{minipage} \\
h & \text{:} & \begin{minipage}[t]{\textwidth}The concave heuristic function that we can use to approximate $f$.\end{minipage}
\end{eqnarray*}


This produces:

A common problem, however, is that if my descriptions are too long, they will bleed past the end of the page. I've discovered that minipage can help in these kinds of situations. I've tried something like this:

    \begin{eqnarray*}
\sqrt{\sin{f-g}} & \text{:} & \begin{minipage}[t]{\textwidth}This is the description of a function but unfortunately goes past the right margin over the edge of the page.\end{minipage}
\end{eqnarray*}


However, this causes the content to bleed over past the right margin, as seen:

One hack to fix this is to manually figure out how 'long' this minipage block should be, and use that measurement instead of \textwidth in the above code block. This is inflexible and requires change whenever the lenght of my equations change. How do I use a box/environment/other solution to automatically begin a newline when the text hits the absolute right margin on the page? My desired output would look something like:

Thank you!

-
A tabularx environment would work wonders here. Are you at all concerned with breaking such an environment across the page boundary? – Werner Oct 30 '13 at 17:12
There is no possibility of f & \text{:} & \begin{minipage}[t]{\textwidth} fitting into \textwidth as the third column is already that wide, so it is overfull even if empty. You should use a list, eg \begin{description}\item[$\sqrt{\sin f-g}$: ] This is.... – David Carlisle Oct 30 '13 at 17:14
Thanks for the tip, Werner! tabularx seems promising, however it seems to have the same problem: imgur.com/UhqQMBv. The code that produced that is below. (Sorry, I don't know how to put code blocks in comments!) Suggestions? \begin{tabularx}{\textwidth}{rcl} $f$ &:& The objective... \\ $h$ &:& The heuristic... \\ $\sqrt{\sin{f-g}}$ &:& This is the description of a function but unfortunately goes past the right margin over the edge of the page. \end{tabularx} – Michael Tingley Oct 30 '13 at 17:54
Using \begin{tabularx}{\textwidth}{rcX}...\end{tabularx} seems to have accomplished what I was looking for, thanks! – Michael Tingley Oct 30 '13 at 18:01

The following option uses tabularx to set the list/descriptions, and is therefore not breakable across the page boundary:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{tabularx}% http://ctan.org/pkg/tabularx
\newenvironment{mylist}
{\par\centering\vspace{\abovedisplayskip}%
\tabularx{\linewidth}{@{}l@{~:~}X@{}}}
{\endtabularx
\par\vspace{\belowdisplayskip}}
\begin{document}
\lipsum[2]

\begin{mylist}
$f$ & The objective function that we're trying to maximize. \\
$h$ & The concave heuristic function that we can use to approximate~$f$\kern-.2em. \\
$g$ & \lipsum*[2]
\end{mylist}

\lipsum[2]

\begin{mylist}
$f$ & The objective function that we're trying to maximize. \\
$h$ & The concave heuristic function that we can use to approximate~$f$\kern-.2em.
\end{mylist}

\lipsum[2]
\end{document}


You have to use an X-column in order to benefit from tabularx's automatic resizing of columns. I've also used \linewidth instead of \textwidth, since the former is more appropriate if you're inside other list-like environments (where \textwidth might be too wide). As reference, see Difference between \textwidth, \linewidth and \hsize.

For ease-of-use, I've only used two columns, with the middle-column always being a colon (~:~). It just promotes consistency to have a general setup, rather than duplicating the output for no apparent reason.

Note that your mylist environment will always have width \linewidth, even though the descriptions might be very short.

-
This is fantastic, thank you very much! One last question, it seems that putting a tabularx environment inside of a center environment doesn't center the list/descriptions. How would you recommend accomplishing this? (Of course, this is only relevant if the width of the longest row on the table is less than the linewidth.) – Michael Tingley Oct 30 '13 at 20:24
@MichaelTingley: Yes, in those instances, if you wish to have things centered, a different approach would have to be taken (possibly a 2-stage process: measure first, then set). tabularx requires a length according to which it measures and sets the X-columns. So, even if your contents is not as wide as \linewidth, it will be made to fit \linewidth. One could manage the two-stage process, but I'll have to do that later... – Werner Oct 30 '13 at 20:40