# How to use hfill and underbrace together

I'm basically trying to achieve what was answered in How to make nested table span all underlying cells but this time with an horizontal brace under 1 2 3. But as soon as I use \underbrace, 1 2 3 collaps. It seems like \underbrace is cancelling the effect of \hfill.

-

\documentclass{article}
\begin{document}
\noindent
\makebox[0.75\textwidth]{left\hfill right}\\
$\underbrace{\makebox[0.75\textwidth]{1\hfill2\hfill3}}_{abc}$

\end{document}


and the same with a tabular of an unknown width:

\documentclass{article}
\newsavebox\TBox
\begin{document}

\savebox\TBox{%
\begin{tabular}{lcr}
left & & right\\
l & center & r\\
\end{tabular}}

\noindent\usebox\TBox\\
$\underbrace{\makebox[\wd\TBox]{\kern\tabcolsep% l\hfill2\hfill r\kern\tabcolsep}}_{abc}$

\end{document}


-
Sorry, your answer is useless to me because I'm inside of a table. And I cannot use \textwidth a I don't know the number of cells in advance. –  Dejan Jan 4 '12 at 10:19
@Dejan: see edited answer –  Herbert Jan 4 '12 at 10:35

To keep the width of the "box" that contains the "1 2 3" elements from collapsing, you could use the \makebox[<width>]{<text>} command. To use this command, you have to tell LaTeX how wide this box should be. In the MWE below, I create a length variable, \tabularwidth, to store the value of the box's intended width. In addition, I use the tabular* rather than the tabular environment in order to instruct LaTeX to set the width of the tabular environment to the same value.

\documentclass{article}
\usepackage{amsmath}
\newlength\tabularwidth
\setlength\tabularwidth{0.5\textwidth} %% or whatever value you want
\begin{document}
\noindent
\begin{tabular*}{\tabularwidth}{l@{\extracolsep{\fill}}r}
some text & more text \\
\multicolumn{2}{l}{$\underbrace{ \makebox[\tabularwidth]{1 \hfill 2 \hfill 3}}_{\text{abc}}$} \\
\end{tabular*}
\end{document}


Addendum: If you can't (or don't want to) set the width of the tabular environment in advance and prefer to go with whatever width LaTeX comes up with when it goes about constructing the tabular environment, you may want to proceed as follows. You first construct an "invisible box" of the same width as the material that's supposed to get the underbrace material, and you then save the resulting box's width as a length parameter. Finally, this length parameter is used in the subsequent construction of the actual tabular environment. The revised MWE looks like this:

\documentclass{article}
\usepackage{amsmath}
\newlength\UBwidth % "UB" stands for "underbrace"
\newsavebox\UBbox

\begin{document}

% use TeX's \savebox command to construct a box whose width is of interest
\savebox\UBbox{%
\begin{tabular}{lr}
some text & more text \\ % assume this is the relevant line of the table
\end{tabular}}
\setlength\UBwidth{\wd\UBbox} % save width of \UBbox

\noindent
\begin{tabular}{lr}
some text & more text \\
\multicolumn{2}{l}{$\underbrace{ \makebox[\UBwidth]{1 \hfill 2 \hfill 3}}_{\text{abc}}$} \\
\end{tabular}
\end{document}


If this approach seems a bit convoluted to you, rest assured that I agree with you. However, taking this approach (or some other method that generates the width) is required because LaTeX does not provide a simple mechanism for retrieving a tabular environment's overall width (or the width of any non-empty subset of columns of a tabular environment) in "real time," i.e., while the tabular environment is still being built up. Incidentally, that's also why, in my first answer, I suggested you take the tabular* rather than the tabular approach...

-
I would prefer that Tex determines the optimal width of the table instead of me. I'm creating the tex code dynamically so I cannot know in advance. Is there a solution that works without having to specify the table width? –  Dejan Jan 4 '12 at 10:22
@Dejan: I'm writing an addendum to my answer to address this follow-up question. –  Mico Jan 4 '12 at 11:21
Thank you very much for your efforts. However, my case is in reality even more complicated as the multicolumns do not necessarily span all columns. The multicolumns can be anywhere in the table having any count of columns. Also, the columns are not equal sized. –  Dejan Jan 4 '12 at 15:48
@Dejan: In this case, I suggest you either edit/augment your original question or post a new question with more specific information about the work you need to get done. By the way, the code I posted does not require the underlying columns to be of equal width. –  Mico Jan 4 '12 at 17:20