26

The nag package suggests I use the center environment in LaTeX, rather than the TeX \centerline command.

When I do that, however, I find that the former inserts a blank line above the line (in my case), whereas the latter does not.

I'd prefer to use the "correct" LaTeX env, but I don't want the extra blank line.

I tried \centering, as recommended here, but that still added space above, although that link suggests that it should not.

I'm doing this within the letter class, if that makes a difference.

cdmackay
  • 393
  • 3
    center doesn't really add a blank line (LaTeX almost never does that) but it does add vertical space. \cemtering doesn't although if you paragrphs have space (\parskip) between them (as is default in letter) you may be seeing that. \left-\right and \centerline are odd constructs in latex, they are almost the only box commands that don't start a paragraph if in vmode, they are not color safe ad generaly they are a relic of theings being copied from plain TeX into LaTeX 1. – David Carlisle Feb 19 '13 at 02:02
  • 1
    If the content is just one line or in a box, then try doing something like \hspace*{\fill}<your content>\hspace*{\fill} – A.Ellett Feb 19 '13 at 02:05

2 Answers2

29

You don't really want \centerline, that doesn't allow line breaks and whose behavior may surprise.

I don't really see why you want something centered inside a chunk of justified text without giving it some room. But if you really insist, \centering is the correct solution. It doesn't add more vertical space than what a normal start of paragraph would. If you're using a nonzero parskip, then you're the cause of your own trouble.

Here's a way to get a center environment that doesn't add vertical space around it even with nonzero parskip set.

Don't use a nonzero parskip, your readers will be grateful.

\documentclass{article}
% \usepackage{parskip}

\newenvironment{nscenter}
 {\parskip=0pt\par\nopagebreak\centering}
 {\par\noindent\ignorespacesafterend}

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
\begin{nscenter}
\bfseries Here's some short material\\ I wanted centered
\end{nscenter}
Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget,
consectetuer id, vulputate a, magna. Donec vehicula augue eu
neque. Pellentesque habitant morbi tristique senectus et netus et

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
\end{document}

Here's the result (thanks to A. Ellett for the precious text), first with zero parskip

enter image description here

and then with nonzero parskip (obtained by uncommenting the line in the source)

enter image description here

egreg
  • 1,121,712
  • thanks; well I don't use a non-zero parskip, but it appears that the letter class does... – cdmackay Feb 19 '13 at 18:51
  • as to why I want this, I use it for the Subject of a letter, after the opening. The extra space added seems too much, to me. – cdmackay Feb 19 '13 at 18:54
  • @cdmackay I didn't notice the reference to letter, sorry. – egreg Feb 19 '13 at 18:54
  • np. Also, it's amusing that your "Don't use a nonzero parskip, your readers will be grateful." itself appears in a set of para, on this page here, that are separated by space :) – cdmackay Feb 19 '13 at 18:56
  • @cdmackay Nonzero parskip has its usages; here I have no control about it, because that's decided by the Powers That Be, but it's not so displeasing and perhaps good for the purpose of these messages. For longer documents it's distracting and spoils the page with white stripes. – egreg Feb 19 '13 at 18:58
  • agreed! I think the reason it looks OK without the space, in letter, is that I'm using it immediately after \opening, which is short and off to the LHS, and the centred Subject I'm using is generally short too, so it doesn't interfere with the opening, and still stands out.

    thanks again.

    – cdmackay Feb 19 '13 at 19:06
  • @egreg, "I don't really see why you want something centered inside a chunk of justified text without giving it some room. But if you really insist, \centering is the correct solution.", the reason is that inside a figure enviroment, one usually need to center the graphics or Tikz figure and the extra space generated is not needs. However +1 because your \centering solves the problem. – alfC Jun 07 '13 at 22:25
  • @egreg Using your macro, I can define custom vertical space above (change parskip=..); how do I define custom vertical space below? – blackened Sep 20 '15 at 16:38
  • @egreg I managed vertical space below with: \newenvironment{mycenter} {\par\addvspace{1.5ex}% \nopagebreak\centering} {\par\addvspace{1.5ex}% \ignorespacesafterend} but know I cannot figure out where to insert \noindent What should I do? – blackened Sep 21 '15 at 13:48
  • @blackened Since this question is about not having spaces above and below the centered part, your requests should be a new question altogether. – egreg Sep 21 '15 at 13:55
12

You can use

\hspace*{\fill}This is my content\hspace*{\fill}

if all it is is one line to be centered.

Or, since the center environment is defined in terms of the \trivlist, you can use the definition of \trivlist to undo the unwanted extra whitespace.

\documentclass{article}
\pagestyle{empty}
\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut
purus elit, vestibulum ut, placerat ac, adipiscing vitae,     felis.
%%
\newline\hspace*{\fill}
        \textbf{Here's some short material I wanted centered}
\hspace*{\fill}\newline
%%
Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget,
consectetuer id, vulputate a, magna. Donec vehicula augue eu
neque. Pellentesque habitant morbi tristique senectus et netus et


\noindent\textbf{Here's a slew of stuff to center without added whitespace:}
\vspace{\dimexpr-2\parsep-2\parskip\relax}%
\begin{center}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel,
wisi. Morbi auctor lorem non justo. Nam lacus libero, pretium at,
lobortis vitae, ultricies et, tellus. Donec aliquet, tortor sed
accumsan bibendum, erat ligula aliquet magna,
\end{center}
\vspace{\dimexpr-2\parsep-2\parskip\relax}%
%%
\textbf{Back to regular formatting:}    
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus
elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur
dictum gravida mauris. 
\end{document}

enter image description here

A.Ellett
  • 50,533
  • Thanks, this answer helped a similar problem, giving the exact behaviour and precise vertical spacing control needed, unlike egreg's one, which introduced other issues. In my humble opinion, this should be the accepted answer. – gsl Apr 08 '18 at 16:05