57

Probably I'm missing something, but I'm really struggling to find an answer to this simple question:

How to center text within a \parbox?

Side note: I don't want to switch to any other environment like minipage. In particular, I use \pbox, an auto-sizing version of \parbox. There don't seem to be an analogue for minipage.


Ok, so \centering as presented in one of the posts is working for the given simple example. I'm however somehow unable to adapt it to my concrete case that is:

\usepackage{pbox}
\usepackage{bussproofs}
...

\pbox{1\textwidth}{
  \AxiomC{$ Rule premise $}
  \UnaryInfC{$ Rule conclusion $}
  \DisplayProof (Rule name) \\
  Some text here in new line
}

It seems \pbox indeed works differently with respect to \centering than \parbox. In particular this works:

\usepackage{bussproofs}
...

\parbox{1\textwidth}{\centering
  \AxiomC{$ Rule premise $}
  \UnaryInfC{$ Rule conclusion $}
  \DisplayProof (Rule name) \\
  Some text here in new line
}

I'll probably have to use the original \parbox after all or perhaps switch to a varwidth.


I thank you all for your help. I think I'll be able to solve my issue somehow using a combination of the ideas that you presented.

6
  • 3
    only 1 of the five commands you show in your edit is defined in standard latex. If we have no idea how the commands are defined we can't say why they don't work. Please edit again to make a complete example using as few packages as possible (preferably just the one that defines pbox) that shows the problem. May 14, 2012 at 22:34
  • Ok, you're right. I'll try to improve on that.
    – julkiewicz
    May 14, 2012 at 22:37
  • You probably need neither \pbox nor varwidth but a plain tabular. Without knowing more details about your macros it's difficult to say more. Please write down a complete minimal example of usage.
    – egreg
    May 14, 2012 at 22:53
  • Yes, tabular is possible as well. For now, I think I'll manage. Thank you for your help.
    – julkiewicz
    May 14, 2012 at 22:58
  • Please try to avoid EDIT headers. Simply update you posts to reflect the latest version. Your third edit for example could have been easily added to the sentence were you first talk about \pbox. If required you can add ---- to get a visible separator. May 15, 2012 at 7:44

2 Answers 2

73

just use \centering inside the box.

\parbox{2cm}{\centering
  one two three four five six}

I assume you are using this package, so with some limitations you can do this:

\documentclass{article}

\usepackage{pbox}

\begin{document}

\parbox{2cm}{\centering
  one two three four five six eight nine ten
aaaaaa\\
bbb}

\bigskip

\pbox{5cm}{\relax\ifvmode\centering\fi
  one two three four five six eight nine ten\\
aaaaaa\\
bbb}


\end{document}
6
  • 1
    Hmm, I tried and I got errors. Could you please give an example of how it should be used properly?
    – julkiewicz
    May 14, 2012 at 22:23
  • code added as requested, did you try \parbox or your \pbox which might be (and probably is) completely different. You may need to post a small complete document showing which package you load. May 14, 2012 at 22:26
  • Oh ok. Can I have multiple lines with \centering?
    – julkiewicz
    May 14, 2012 at 22:26
  • yes that makes three centred lines at normal font sizes. Or you can linebreak manually with \\ all as usual. May 14, 2012 at 22:30
  • Ok, the issue seems to be more complex than I thought. Your solution works for the outlined example, however if possible, please see my edit.
    – julkiewicz
    May 14, 2012 at 22:32
14

There is the varwidth environment from the package with the same name, which is an auto-sizing version of minipage. You only need to give a maximal width.

The text centering inside it or inside \parbox can and should be done using \centering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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