140

In normal text, how do I center part of a sentence (it happens to be the end of a sentence) but leave the text in the rest of the paragraph unaltered? Also, no vertical space should be added before or after the centered text.

In a word processor, I would add a new line just before the text to be centered and then "center" the text on the next line. I tried doing things similar to this with various LaTeX commands, but nothing worked.

This is what I want it to look like:

This is a really long sentence as an example.  The second half of this
                               sentence should be centered.
1
  • 3
    Does \centering not yield the desired result? Do you want to keep the indentation of the first line of the paragraph?
    – Jake
    Commented Jul 27, 2011 at 0:48

6 Answers 6

180

How about this:

\documentclass{article}

\begin{document}

This is a really long sentence as an example.  The second have of this\\
\centerline{sentence should be centered.}
\end{document}
3
  • 3
    This requires manually determining where the last line of the paragraph will start. A modification to the paragraph might produce undesired results. Commented Jul 27, 2011 at 1:23
  • 1
    @GonzaloMedina I didn't want to center the last line of a paragraph. I wanted to center specific words in the text. Commented Jan 27, 2014 at 13:35
  • This still adds some vertical space for me when used inside a table cell. Commented Jun 17, 2020 at 18:11
57

Please do not use \centerline if possible, it's not suitable for long text. Just patch LaTeX's center environment like this:

\documentclass{article}
\newenvironment{tightcenter}{%
  \setlength\topsep{0pt}
  \setlength\parskip{0pt}
  \begin{center}
}{%
  \end{center}
}

\begin{document}
text text text text text text text text text text text text text text text text text text text text text text text text
\begin{tightcenter}
foo
\end{tightcenter}
text text text text text text text text text text text text text text text text text text text text text text text text

\end{document}
6
  • This is an improvement but for me still adds some vertical spacing.
    – Roly
    Commented Aug 29, 2017 at 8:22
  • 14
    Can you elaborate on why \centerline should not be used? What does "not suitable for long text" mean?
    – mhucka
    Commented Jan 30, 2020 at 19:08
  • 1
    @Roly Adding \unskip after \begin{center} does the trick for me.
    – Witiko
    Commented Sep 19, 2020 at 18:44
  • Caution! This affects the whole document. Commented Jun 15, 2021 at 10:42
  • 1
    @scribe I think you're right; that looks like a good reason for avoiding \centerline. Thanks!
    – mhucka
    Commented Apr 9, 2023 at 0:04
28

You could use the TeX primitives \rightskip and \leftskip; a little example:

\documentclass{article} 

\begin{document}

\begingroup
\leftskip=0cm plus 0.5fil \rightskip=0cm plus -0.5fil
\parfillskip=0cm plus 1fil
This is a really long sentence as an example.  The last line of this
paragraph will be centered.\par
\endgroup
Another sentence that starts a new paragraph

\end{document}

enter image description here

The explanation of the code (as given in TeX by Topic):

For all lines of a paragraph but the last one the stretch components add up to zero so the \leftskip and \rightskip inserted are zero. On the last line the \parfillskip adds plus 1fil of stretch; therefore there is a total of plus 0.5fil of stretch at both the left and right end of the line.

4
  • +1 Works without needing to know where the line breaks (though the OP didn't state this as a requirement)
    – ThomasH
    Commented Oct 24, 2013 at 10:24
  • 1
    @GonzaloMedina why bother? what's wrong with {\par\centering ...text...\par} Commented Feb 11, 2015 at 11:34
  • There should also be a \noindent in front of the centered paragraph, otherwise the first line will not be completely centered.
    – maja
    Commented Jul 9, 2017 at 8:42
  • @wasteofspace Using \centering will not justify all the lines of the paragraph leading up to the last line. Try putting a six-line paragraph in Gonzalo's setting vs the same in yours and note the difference in formatting.
    – Don Hosek
    Commented Jun 30, 2021 at 17:58
15

Just place the portion to be centered in a full-width \makebox. Here are two examples in this MWE: 1) what the OP asked about, and 2) a paragraph that continues after the event.

\documentclass{article}
\parskip 1em
\begin{document}
This is a really long sentence as an example.  The second half of this\\
\makebox[\textwidth]{sentence should be centered.}

This is a really long sentence as an example.  
This is a really long sentence as an example.  
The second half of this\\
\makebox[\textwidth]{sentence should be centered.}
This is a really long sentence as an example.  
This is a really long sentence as an example.  
\end{document}

enter image description here

4

It can also be solved using a pair of hspace*:

\documentclass{article}

\begin{document}

This is a really long sentence as an example.  The second half of this\\
\hspace*{\fill} sentence should be centered. \hspace*{\fill}
\end{document}
2

You could use tabular environment, with one centred column:

    \documentclass{article}
    \begin{document}
    Left-justified here! \\
    \begin{tabular}{c}
    Text in here is centred.
    \end{tabular}
    \end{document}
3
  • Welcome to TeX.SX! But that's a very bad idea for long text, isn't it?
    – TeXnician
    Commented Nov 24, 2017 at 6:49
  • there is a \\ missing, otherwise the table will appear on the same line
    – jakun
    Commented Nov 24, 2017 at 7:08
  • Even with a preceeding \\ this will not be centered on the page. You need a tabular* and a width specification - but really a version of center is much easier. Commented Nov 24, 2017 at 7:31

You must log in to answer this question.

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