# How can I produce 3 centered columns, with the middle one fitting the width of its content?

How can I produce a document similar to this, but cause the middle column to take up only as much space as it needs (rather than explicitly specifying a width), such that as much space as possible is available to the left and right columns?

\documentclass{article}

\begin{document}
\noindent
\fbox{\parbox{0.333\textwidth}{\centering Left content centered \\ between margin and title }}
\fbox{\parbox{0.333\textwidth}{\centering \Huge Title }}
\fbox{\parbox{0.333\textwidth}{\centering Right content \\ also centered }}

\end{document}


-

You can calculate the width for the contents and store it in an auxiliary length; then you can calculate the other two widths using this value (you need to take into account also \fboxsep and \fboxrule):

\documentclass{article}

\newlength\mylen
\newcommand\MyCont{\Huge Title}
\settowidth\mylen{\MyCont}

\begin{document}
\noindent
\fbox{\parbox{\dimexpr0.5\textwidth-0.5\mylen-3\fboxsep-3\fboxrule\relax}{\centering Left content centered \\ between margin and title }}%
\fbox{\parbox{\mylen}{\centering\MyCont}}%
\fbox{\parbox{\dimexpr0.5\textwidth-0.5\mylen-3\fboxsep-3\fboxrule\relax}{\centering Right content \\ also centered }}

\end{document}


Of course, if you are inetrested in producing tabular material, the best approach would be to use the tabularx package (as in zeroth's answer), or the tabu package. A little example with the latter:

\documentclass{article}
\usepackage{tabu}

\begin{document}
\noindent
\begin{tabu}{|X[c]|c|X[c]|}
Left content centered between margin and title &  \Huge Title &
Right content  also centered
\end{tabu}
\end{document}


-
Neat! Is this how most fancy layout packages calculate widths as well? Is there a nice way to do this without having to type the content twice? Also could you explain how this works (what the \dimexpr, 0.5, and 3 are for)? –  jtbandes Oct 2 '12 at 20:30
@jtbandes to answer your questions: 1) Yes, some packages use (a more sophisticated variant of) this mechanism. 2) I updated my answer. 3) \dimexpr is an e-TeX macro allowing operations involving lengths; the factors used appear because you need two additional columns and,as I mentioned, you need to take into account \fboxsep and \fboxsep for three framed boxes. –  Gonzalo Medina Oct 2 '12 at 20:36

The package you can use for this is the tabularx. This lets you specify a tabular environment which needs a width argument. Furthermore a new column specifier, X, is introduced.

This new column type will take up the rest of the space available and divide that space amongst the X columns.

In your case this amounts to:

% \usepackage{tabularx}
\begin{tabularx}{\textwidth}{|>{\centering}X|c|>{\centering\arraybackslash}X|}
o & o & o
\\
o & o & o
\end{tabularx}


The \arraybackslash is a work around for the \cr which is not correctly interpreted. See the manual for more information.

This will yield:

-
Is it possible to vertically center the cells when using \newline to break lines? –  jtbandes Oct 2 '12 at 20:50
@jtbandes, yes and no. It can be made, however, I cant find anything that works without being a bit hackarish. For instance you can use the multirow package and use it on the lines of a newline statement. However, this will have unforeseen problems in certain cases. I would not recommend this path. –  zeroth Oct 3 '12 at 6:00
Actually the answer on my other question has a good idea: tex.stackexchange.com/questions/75066/… –  jtbandes Oct 3 '12 at 6:05
@jtbandes yes, very good. I am not too familiar with the tabu package. I only referred to by the tabularx. :) –  zeroth Oct 3 '12 at 6:23