Centering text in box

I would like to horizontally center the text in the \fbox:

\fbox{\vbox{\hbox{Words,}\hbox{words,}\hbox{and only words again}}}


How would I do this?

• – Werner
Oct 8 '15 at 3:47

Tailor made for a stack. I show a normal stack on the left, an understack in the center and a centerstack on the right. Also, the left stack uses a space (default) as the end-of-line delimiter, while on the right 2 stacks, I have reset the end-of-line (EOL) delimiter to \cr.

\documentclass{article}
\usepackage{stackengine}
\begin{document}
\fbox{\Longstack{Words, words, {and only words again}}}
or
\setstackEOL{\cr}%
\fbox{\Longunderstack{Words,\cr words,\cr and only words again}}
or
\fbox{\Centerstack{Words,\cr words,\cr and only words again}}
\end{document}


Interline baseline skip can be reset with \setstackgap{L}{<length>}. Note also that center stacks add a \strut to each line, so that vertical centering is not affected by the presence of descenders. The strutting can be made the default for the other stacks as well, with the invocation of \strutlongstacks{T}.

Use a tabular.

\documentclass{article}

\begin{document}
\fbox{%
\begin{tabular}{@{}c@{}}
words, \\
words,\\
and only words again
\end{tabular}%
}

\begin{tabular}{|@{\hspace{\dimexpr\fboxsep+0.5\arrayrulewidth}}c@{\hspace{\dimexpr\fboxsep+0.5\arrayrulewidth}}|}\hline
words, \\
words,\\
and only words again \\\hline
\end{tabular}%

\end{document}