18

In TeX, code like I\kern37ptlike\hskip100ptcake works perfectly well. That makes me wonder how exactly the syntax of primitive commands like \kern, \hskip etc. work? How does TeX determine where their arguments end?

Does TeX just utilise the fact that (as far as I know) all length units consist of two alphabetic characters?

(I cannot see the need for a MWE.)

10
  • 1
    They keep scanning until they find l (ell) in the first case, which is not a length, then decides that the kern is ended, inserts the kern, and keeps going with the ell. The \hskip does the same, but with c. In case of an space, it's gobbled. Better use \kern37pt like and \hskip100pt cake. (Disclaimer: I'm a not specially proficient in TeX.)
    – Manuel
    Jul 18, 2015 at 19:41
  • I noticed that \kern{37pt} breaks. TeX is really the worst when it comes to consistent syntax.
    – Gaussler
    Jul 18, 2015 at 19:42
  • 1
    The syntax is \kern37pt, the {..} is not the syntax. Of course it crashes.
    – Manuel
    Jul 18, 2015 at 19:43
  • 2
    I think you're thinking of LaTeX, which would likely prefer \kern{37pt} if \kern weren't already a TeX dimension command, which has its own syntax.
    – jon
    Jul 18, 2015 at 19:51
  • 2
    @Gaussler Not that difficult, depending on what \def\whatever{\afterassignment\dowhatever\dimen0=}, then \whatever37ptlike would work and use \dowhatever, after storing 37pt in \dimen0.
    – Manuel
    Jul 18, 2015 at 19:57

1 Answer 1

24

The argument for \kern is a ⟨dimen⟩, so TeX first looks for a ⟨decimal number⟩ (which can be an integer in any admissible representation, or a fractional number in decimal notation, with period or comma for separating the fractional part) and then for a ⟨unit of measure⟩, that is, a pair of letters representing one of the accepted units (pt, cm, mm, in, dd, pc, cc, bp, sp, em, ex). Any number of spaces between the numbers and the units are ignored, an optional space can follow the unit (it is looked for with expansion). Any other pair of letters will trigger an error message

! Illegal unit of measure (pt inserted).

Thus \kern37pclike has a correct value for \kern, but \kern 37 pc like would have given exactly the same result.

Be careful with \kern, because it's a command that does different things if called in vertical or horizontal (or math) mode. Using \enspace before having started a paragraph will make you scratch your head.

The argument to \hskip should instead be a ⟨skip⟩; the same as before applies, but after the ⟨dimen⟩ stating the natural width, TeX looks for the keyword plus or minus (with expansion). If it finds it, it looks again for a ⟨dimen⟩ with the same rules as before, but the ⟨decimal number⟩ can also be followed by fil, fill or filll (not the real truth, but a good approximation) again with a trailing optional space that's looked for and ignored. Notwithstanding if plus is found or not, TeX looks for a minus keyword, same rules.

So \hskip 2pt minuscule chances of errors will trigger a puzzling error message.

The same as before is valid whenever TeX is looking for a ⟨dimen⟩ or a ⟨skip⟩. In case of doubts, particularly with a ⟨skip⟩, add \relax, which is what \setlength, \hspace and \vspace do in LaTeX.

5
  • Out of curiosity, what's the real truth (at least a small peak of it) with fil(|l|ll)?
    – Manuel
    Jul 18, 2015 at 20:03
  • 3
    An answer like only egreg can make them :-D. (Seriously, I could see it was his without looking at the name.)
    – Gaussler
    Jul 18, 2015 at 20:03
  • 8
    @Manuel The keyword fil looks for a following l which looks for a following l. So \hskip 0pt plus 1 fil L l minus 2 fiLL is a legal specification. And \hskip 0pt plus 1fil Let's go will make you scratch your head. ;-)
    – egreg
    Jul 18, 2015 at 20:14
  • Specifically such commands look for a 'digit string' which may have no digits at all, so that something like \kern , pt will give you a kern of 0.
    – Jon
    Jul 19, 2015 at 1:29
  • @Jon Yes, there are sum subtle points in what TeX thinks is a decimal number. Indeed, a decimal number can also have an empty integral or fractional part (or even both); but if the integral part is specified with any other notation than decimal, it can't have a fractional part
    – egreg
    Jul 19, 2015 at 21:22

You must log in to answer this question.

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