3

I want to center text on a page vertically and horizontally. I took the answer from here https://tex.stackexchange.com/a/2327/110351 and created the following document:

\documentclass[pagesize=auto, paper=102mm:51mm, paper=landscape]{scrartcl}

\usepackage[margin=3mm]{geometry}

\begin{document}%
  \pagestyle{empty}%
  \setlength{\parindent}{0pt}%
  \setlength{\topskip}{0pt}%      
  \vspace*{\fill}%
  \begin{center}
    Test

    More tests

    More tests
  \end{center}
  \vspace*{\fill}%
\end{document}

With this I have more space at the top of the page than at the bottom, i.e. the text is lower than it needs to be.

Output

How can I fix this?

Edit: Could it be related to the text or font? When I highlight the text, the box is larger above the letters than below the letters. So it might seem like the text is not centered vertically, while it actually is.

  • If I compile this MWE I get the expected behavior. Maybe you could try \vspace*{\fill} and see if that works better – Raven Sep 1 at 7:42
  • I added a screenshot of the output pdf. That is how it looks like for me. \fill and \stretch{1} seem to do the same, but I edited the code so it matches the title. – Fabian Sep 1 at 7:52
  • There might be some paragraph spacing defined in the document class or something like this... – Raven Sep 1 at 8:24
  • 2
    The last paragraph in your question (“Edit: Could it be…”) contains the answer: you can compensate for this effect by adding a line containing only the command \nointerlineskip at the very beginning of the center environment. – GuM Sep 1 at 11:19
  • @GuM -- This is worth posting as an answer. – barbara beeton Sep 1 at 13:54
3

In good typography, the distance between two consecutive lines of text on a page should not depend on how tall the letters they contain are (well, this doesn’t hold true only in good typography!). For this reason, (La)TeX places their baselines at a fixed distance (for example, 12 points, or about 4.2mm), independently of the actual height of the glyphs inside. In this way, there is enough room to include, say, a math formula containing an exponent, or a \big parenthesis, in a line without having to move it vertically.

TeX implements this behavior by means of a mortar called “interline glue”, which is inserted between consecutive lines of text. In the implementation of \vspace*, special care is taken not to affect the way interline glue is inserted: this is usually correct, but, in your case, it has the side effect of positioning the first line of text as if it were taller than it actually is. This may or may not be what you want: if it is not, you can counteract it simply issuing the \nointerlineskip command, which suppresses the interline glue that would have normally been inserted before the line that immediately follows.

Note that, in the LaTeX implementation of \vspace*, the “interline glue” machinery adds over the “\topskip glue” appliance. More precisley, \vspace* adds an invisible horizontal rule to prevent the space it inserts from disappearing at a page break: now, setting \topskip to zero prevents unwanted vertical space from being inserted above this invisible rule; on the other hand, issuing the \nonterlineskip command prevents the interline glue (and hence, the vertical space it takes) from being added below the same rule. So, in your case, you need both.

MWE:

\documentclass[pagesize=auto, paper=102mm:49mm, paper=landscape]{scrartcl}
\usepackage[margin=3mm]{geometry}

% \showoutput

\begin{document}
  \pagestyle{empty}
  \setlength{\parindent}{0pt}
  \setlength{\topskip}{0pt}
  \nointerlineskip
  \setlength{\parskip}{0pt}
  \vspace*{\fill}
  % \showthe\prevdepth
  \begin{center}
    Test

    More tests

    More tests

    More tests

    More tests

    More tests

    More tests

    Test
  \end{center}
  \vspace*{\fill}
\end{document}

Setting \parskip to zero is not essential in this case, but it might be important in other contexts. Uncomment the commented lines to see some diagnostic messages.

  • It should be noted that center adds space above and below, which you can see by adding \hrule before and after. However, the gaps appear to be equal. You can also use` \hrule height0pt\vfill` instead of \vspace*{\fill}. \hrule is not affected by \baselineskip or \lineskip. – John Kormylo Sep 1 at 17:42
  • Works perfectly for me. Thank you. I have never encountered the \nointerlineskip command before. – Fabian Sep 2 at 7:34

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.