[Tex/LaTex] Centering text in box

boxeshorizontal alignment

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?

Best Answer

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}

enter image description here

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}.

Related Question