11

I want to have a paragraph, that is centered but in itself left-aligned. Does somebody of you know how to achieve this?

(I have text in a paragraph, on each line the text is shorter than the width, but if I just put "flushleft" around it comes to much to the left margin of the page)

1
  • I'm not sure if this is what you want, but how about using \raggedright inside a quote environment?
    – lockstep
    Sep 9, 2011 at 16:05

4 Answers 4

6

It is possible to create a stand-alone environment that can do it all in one - both centering and and internally align elements:

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{environ}% http://ctan.org/pkg/environ
\NewEnviron​{centerbox}[1][\linewidth]{% \begin{centerbox}[..] ... \end{centerbox}
  \noindent\makebox[\linewidth][c]{%
    \begin{minipage}{#1}%
      \raggedright% Minipage alignment
      \BODY% Typeset body/contents
    \end{minipage}%
  }
}%
\begin{document}
\lipsum[1]

\begin{centerbox}[0.5\linewidth]
  \lipsum[2]
\end{centerbox}

\lipsum[3]
\end{document}

​ The above code produces the centerbox environment (using the environ package). An optional argument specifies the width of the centered box, which defaults to \linewidth.

Centered box

4
  • 5
    er, the last line of the centered box looks too close to the next line. a \strut at the end would help. the first line of the box may also be closer than a normal \baselineskip but the short line above it is camouflaging the situation. note that both "last lines" have no descenders; that's a common cause of this problem. Sep 9, 2011 at 17:34
  • 4
    The adjustbox version would be: \newenvironment{centerbox}[1][\linewidth]{% \begin{centerbox}[..] ... \end{centerbox} \begin{adjustbox}{minipage={#1},center}% \raggedright }{% \end{adjustbox}% }% . However, I would prefer to simply use the center environment here, which also adds the required margin. Note that you can get the same \makebox effect with \leavevmode\hbox to \linewidth\bgroup\hss ... \hss\egroup which can be split and therefore you can avoid the use of environ (which isn't very efficient). Or you use \Makebox from my realboxes package. Sep 9, 2011 at 17:44
  • if you add it all into a minipage-setup, then footnotes and marginpar's wont work anymore, right?
    – Johannes L
    Sep 12, 2011 at 6:02
  • @Johannes: \footnote will produce a minipage-style footnote. However, if you want a footnote at the bottom of the page (outside of the minipage environment), you can use a \footnotemark (inside the minipage) and \footnotetext (outside the minipage) combination. \marginpar`, however, won't work. I guess you want both to work?
    – Werner
    Sep 12, 2011 at 6:08
11

Another approach, using this time the quoting package to define a new environment with equal left and right margins (thus, "centered") controlled by an optional argument with a default value of 2em, and with \RaggedRight contents:

\documentclass{article}
\usepackage{quoting}
\usepackage{ragged2e}
\usepackage{lipsum}

\newenvironment{RCText}[1][2em]
  {\begin{quoting}[leftmargin=#1,rightmargin=#1]\RaggedRight}
  {\end{quoting}}

\begin{document}

\lipsum[1]
\begin{RCText}[2cm]
  \lipsum[1]
\end{RCText}

\lipsum[1]

\end{document}

enter image description here

Here's yet another option, using this time a list environment; again, the margins can be set using the optional argument:

\documentclass{article}
\usepackage{ragged2e}
\usepackage{lipsum}

\newenvironment{RLCText}[1][2em]
  {\begin{list}{}{\setlength\leftmargin{#1}\setlength\rightmargin{#1}}\item[]\RaggedRight}
  {\end{list}}

\begin{document}

\lipsum[1]
\begin{RLCText}[2cm]
  \lipsum[1]
\end{RLCText}

\lipsum[1]

\end{document}

Both solutions admit page breaks inside the new environment.

1
  • With quoting v0.1a, one may also add the option indentfirst=false.
    – lockstep
    Sep 9, 2011 at 19:14
7
\documentclass{article}

\usepackage{lipsum}% just for filler text

\begin{document}
\begin{center}
    \parbox{0.5\textwidth}{\raggedright\lipsum[1]}
\end{center}
\end{document}

You can adapt 0.5\textwidth to whatever width you like. Note that the text in the \parbox won't break onto a new page.


Edit: Using ragged2e's \RaggedRight, as Mico suggested in his answer, instead of \raggedright (as I did) is helpful if you want hyphenation in your paragraph, which will result in a more evenly filled paragraph. Here's a comparison of the \lipsum[1] filler-text paragraph with \raggedright and with ragged2e's \RaggedRight:

Comparison of \raggedright and \RaggedRight

Note how \RaggedRight results in one fewer line. (I put boxes around them to make the empty spaces on the right more visible.)

1
  • 2
    Nice comparison of the effects of the \raggedright and \RaggedRight commands!
    – Mico
    Sep 9, 2011 at 17:48
2

You could use the ragged2e package and its command \RaggedRight to make the lines left-aligned. To create a paragraph that's both centered and has a smaller overall textwidth than the surrounding text, you could use the center and minipage environments. In the following MWE, the default width of this environment is 0.6*\textwidth.

\documentclass{article}
\usepackage{ragged2e,lipsum}
\begin{document}
\begin{center}
   \begin{minipage}{0.6\textwidth}
       \RaggedRight
       \lipsum[1] %% filler-text
   \end{minipage}
\end{center}       
\lipsum[2]  %% more filler-text
\end{document}

Addendum: If you have need to create several of these centered-raggedright paragraphs, it's helfpul to create a separate environment, say centragg, for them. The following MWE illustrates how to set it up and how to use it. Note that the centragg environment has a default width of 0.75\textwidth, but this can be overridden by specifying an explicit width, such as 5cm. In addition to invoking \RaggedRight automatically, it also adds a bit of extra inter-paragraph spacing in case there's more than 1 paragraph at a time in the centragg environment. (Traditionally, paragraphs that are typeset ragged-right have no special paragraph indentation.)

\documentclass{article}
\usepackage{ragged2e,lipsum}
\newenvironment{centragg}[1][0.75\textwidth]{% 
   \begin{center}
   \begin{minipage}{#1}
   \RaggedRight \setlength{\parskip}{0.5\baselineskip} \noindent\ignorespaces}
   {\end{minipage}\end{center}}

\begin{document}
\lipsum[2]
\begin{centragg}
   (Default width) \lipsum[2] %% filler-text
\end{centragg}       
\begin{centragg}[6cm]
   (5 cm wide) \lipsum[10] %% filler-text
\end{centragg}       
\lipsum[11]  %% more filler-text
\end{document}

The only (?) downside to this approach is that LaTeX will not break minipages (or parboxes, for that matter), across pages, risking an overfull page should the centered paragraph be longer than just a few lines.

2
  • Indeed, an this can be easily defined as an own environment as well, i.e. no need for environ package. Sep 9, 2011 at 17:45
  • Martin: We must have been thinking along similar lines! I was adding a second MWE to show how to put this into a separate environment while you were providing your comment. Notice that my approach does not have the problem of insufficient vertical spacing above and below the paragraphs. :-)
    – Mico
    Sep 9, 2011 at 18:05

You must log in to answer this question.

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