Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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

1 Answer 1

up vote 7 down vote accepted

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.

share|improve this answer
    
My intention is the latter, framebox within the page. –  Jesse Aug 21 '13 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 –  David Carlisle Aug 21 '13 at 15:04
    
Would it be correct to say that \centering is for vertical box? –  Jesse Aug 21 '13 at 15:32
    
@Jesse "vertical mode" is the tex terminology or "par mode" the latex one, but basically yes that is correct. –  David Carlisle Aug 21 '13 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.