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.

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.
share|improve this question
1  
Does \centering not yield the desired result? Do you want to keep the indentation of the first line of the paragraph? –  Jake Jul 27 '11 at 0:48

3 Answers 3

up vote 34 down vote accepted

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}
share|improve this answer
    
You have to add a line break before \centerline{}, but otherwise perfect :) Thanks! –  Tyson Williams Jul 27 '11 at 1:19
    
Oops my bad; I was fooled by my narrow default page width! I've made the change. –  cmhughes Jul 27 '11 at 1:23
3  
This requires manually determining where the last line of the paragraph will start. A modification to the paragraph might produce undesired results. –  Gonzalo Medina Jul 27 '11 at 1:23
    
@GonzaloMedina I didn't want to center the last line of a paragraph. I wanted to center specific words in the text. –  Tyson Williams Jan 27 at 13:35

Please no 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}
share|improve this answer

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.

share|improve this answer
    
+1 Works without needing to know where the line breaks (though the OP didn't state this as a requirement) –  ThomasH Oct 24 '13 at 10:24

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.