[Tex/LaTex] Centering a \framebox

boxeshorizontal alignment

Before asking, I have checked Centered box, text inside it flushed right?, Questions about \fbox, and Any way to center text within a \parbox?.

Here I have tested various centering methods (my intention is to use \centering only) on \framebox and \makebox alike.

\framebox{This is a sentence of text.}\par
\makebox{This is a sentence of text.}\par

{\indent \centering\framebox{This is a sentence of text.}}\par
{\indent \centering\makebox{This is a sentence of text.}}\par

\framebox{\centering This is a sentence of text.}\par
\makebox{\centering This is a sentence of text.}\par

\framebox[5cm]{\centering This is a sentence of text.}\par
\makebox[5cm]{\centering This is a sentence of text.}\par

\framebox[\linewidth]{This is a sentence of text.}\par
\makebox[\linewidth]{This is a sentence of text.}\par

\framebox[5cm]{\centering This is a sentence of text.}\par
\makebox[5cm]{\centering This is a sentence of text.}\par

\framebox{\centering\parbox{5cm}{This is a sentence of text.}}\par
\makebox{\centering\parbox{5cm}{This is a sentence of text.}}\par

\parbox{5cm}{\centering\framebox{This is a sentence of text.}}\par
\parbox{5cm}{\centering\makebox{This is a sentence of text.}}\par

\begin{center}
\framebox{This is a sentence of text.}\par
\makebox{This is a sentence of text.}
\end{center}

The only standard LaTeX method (no packages) I can achieve is using the center environment (the last one). Is that so, and why?

An image is shown below. enter image description here

Best Answer

It isn't clear whether you want to centre text within a framebox (what you said) or to centre a framebox within the page (what the last example does).

\framebox (line \makebox, \mbox etc) makes a horizontal box (LR box in the terminology of the latex book) as such it makes a singe line, so paragraph settings like \centering have no effect. The text is centred by default (like a tabular c column) which you can see if you use the optional width argument and specify a width wider than the text.

\noindent
\framebox[\linewidth]{This is a sentence of text.}

If you want paragraph material withn the box you need to nest a parbox such as

\fbox{\parbox{8cm}{\centering .....}}

To center a box in the text area you can use

\begin{center}\fbox{...}\end{center}

or

{\centering\fbox{...}\par}

where the first also adds some vertical space. Note that paragraph settings like \centering take effect at the end of the paragraph, so you need to close the group after the paragraph has finished.

Related Question