18

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

2
  • 1
    Insert the \par inside the group, not outside and you'll see something different. For example {...\centering...\par}.
    – Werner
    Commented Aug 21, 2013 at 14:40
  • @Werner Only the second set works, having the effect. Thanks for the hint.
    – Jesse
    Commented Aug 21, 2013 at 14:54

1 Answer 1

24

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.

4
  • My intention is the latter, framebox within the page.
    – Jesse
    Commented Aug 21, 2013 at 14:56
  • @Jesse yes sorry I'd misread some of your examples (as you may have noticed in a comment I deleted on your question) Hopefully the last bit of the answer does what you want and the first part explains why the other examples didn't work Commented Aug 21, 2013 at 15:04
  • Would it be correct to say that \centering is for vertical box?
    – Jesse
    Commented Aug 21, 2013 at 15:32
  • @Jesse "vertical mode" is the tex terminology or "par mode" the latex one, but basically yes that is correct. Commented Aug 21, 2013 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .