2

I'm currently learning TeX from the TeXbook, and am trying to implement some macros with their actual definitions. I'm trying to figure out how \centerline works, but each of these definitions don't center the text in exactly the same way.

\centerline{This should be centered.}
\line{\hss This should be centered. \hss} % This is off by a bit.
\hbox to \hsize{\hss This should be centered.\hss}
\hbox to \hsize{\hskip 0pt plus 1fil minus 1fil This should be centered.
                \hskip 0pt plus 1fil minus 1fil} % This is also off by a bit.

For me this outputs:

Is there something about TeX that I'm not taking into account?

7
  • 2
    \line{\hss This should be centered.\hss} Remove the space.
    – Johannes_B
    Commented Oct 22, 2017 at 6:35
  • Thank you! Now thinking about it I don't know how I missed that. Commented Oct 22, 2017 at 6:37
  • 5
    I'm voting to close this question because it has been solved in the comments. Commented Oct 22, 2017 at 6:38
  • 3
    I never understand why in these instances someone doesn't just write an answer instead of closing as off-topic (which it is not). Commented Oct 23, 2017 at 1:10
  • 1
    @ShreevatsaR I voted to reopen. I do think that closing as off-topic because the comments helped is just ridiculous. The question might be a duplicate, though.
    – Johannes_B
    Commented Oct 29, 2017 at 6:52

1 Answer 1

4

These are the relevant definitions from plain.tex (lines 575 and 578):

\def\line{\hbox to\hsize}
\def\centerline#1{\line{\hss#1\hss}}

This definition of \centerline means that \centerline followed by an argument expands to the sequence of tokens \line, {, \hss, the argument, \hss, }.

So of the four example lines in the question, the first one:

\centerline{This should be centered.}

is equivalent to typing \line{\hss This should be centered.\hss} which in turn is equivalent to the third one:

\hbox to \hsize{\hss This should be centered.\hss}

while the second one:

\line{\hss This should be centered. \hss} % This is off by a bit.

is different: it contains an additional space token before the second \hss. So what gets centered on a line is the characters in This should be centered. (with a space at the end) rather than those in This should be centered. (without a space at the end) as in the first example.

The fourth one:

\hbox to \hsize{\hskip 0pt plus 1fil minus 1fil This should be centered.
                \hskip 0pt plus 1fil minus 1fil} % This is also off by a bit.

has the same problem, containing an additional space (note that multiple spaces and even the newline here are equivalent to just one space) after the ..


You could debug this by adding

\tracingoutput = 1
\showboxbreadth = \maxdimen % or at least 29, for this example

to the .tex file (and then looking in the .log file, or adding \tracingonline=1). The first line shows:

..\hbox(6.94444+0.0)x469.75499, glue set 181.19687fil
...\glue 0.0 plus 1.0fil minus 1.0fil
...\tenrm T
...\tenrm h
...\tenrm i
...\tenrm s
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm s
...\tenrm h
...\tenrm o
...\tenrm u
...\tenrm l
...\tenrm d
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm b
...\kern0.27779
...\tenrm e
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm c
...\tenrm e
...\tenrm n
...\kern-0.27779
...\tenrm t
...\tenrm e
...\tenrm r
...\tenrm e
...\tenrm d
...\tenrm .
...\glue 0.0 plus 1.0fil minus 1.0fil

while the second line shows:

..\hbox(6.94444+0.0)x469.75499, glue set 178.97464fil
...\glue 0.0 plus 1.0fil minus 1.0fil
...\tenrm T
...\tenrm h
...\tenrm i
...\tenrm s
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm s
...\tenrm h
...\tenrm o
...\tenrm u
...\tenrm l
...\tenrm d
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm b
...\kern0.27779
...\tenrm e
...\glue 3.33333 plus 1.66666 minus 1.11111
...\tenrm c
...\tenrm e
...\tenrm n
...\kern-0.27779
...\tenrm t
...\tenrm e
...\tenrm r
...\tenrm e
...\tenrm d
...\tenrm .
...\glue 4.44444 plus 4.99997 minus 0.37036
...\glue 0.0 plus 1.0fil minus 1.0fil

and near the end of this second log you can see the extra ...\glue 4.44444 plus 4.99997 minus 0.37036 (the end-of-sentence glue), just before the \hss glue.

1
  • This is a very verbose answer :-) +1
    – Johannes_B
    Commented Oct 31, 2017 at 7:50

You must log in to answer this question.

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