7

Today I came across these three commands:

\@nil
\@cdr
\@car

Some are mentioned here and here, but I didn't get the idea of it. It seems to have to do with the macro character.

I haven't seen any description about it on google and TeX.SX.

MaestroGlanz
  • 2,348

1 Answers1

10

the names come from lisp.

In lisp, car returns the head of a list, cdr returns the tail of a list and nil is an empty list.


in latex

\def\@car#1#2\@nil{#1}
\def\@cdr#1#2\@nil{#2}

(\@nil is not defined at all)

so

\@car abc\@nil

expands to a

and

\@cdr abc\@nil

expands to bc

David Carlisle
  • 757,742
  • In expl3 lingo, \tl_head:n {abc} and \tl_tail:n {abc} – egreg Feb 13 '17 at 22:02
  • But how does this work, if \@nil is undefined. Why is there no undefined error? – MaestroGlanz Feb 13 '17 at 22:02
  • 6
    @MaestroGlanz tokens only make an undefined error if you try to expand them. tokens used to delimit a macro argument are not expanded, try \def\foo#1\wibblethingy{hello #1}...\foo Maestro\wibblethingy – David Carlisle Feb 13 '17 at 22:06
  • 2
    @egreg we should put in a change request \tl_car:n and tl_cdr:n to honour the cultural heritage – David Carlisle Feb 13 '17 at 22:08
  • Aaaaah. Good to know. – MaestroGlanz Feb 13 '17 at 22:08
  • @UlrichDiez yes they are only really usable in internal code where you have tight control over the input arguments – David Carlisle Feb 14 '17 at 08:58
  • @MaestroGlanz Be aware that with \@carand \@cdr the first argument is an undelimited argument and thus leading space tokens might get discarded silently and that pairs of braces surrounding the entire first argument and/or the entire second argument get silently removed. Not always the desired behaviour. – Ulrich Diez Feb 14 '17 at 09:27
  • @UlrichDiez If I try \@cdr abc de\@nil, the result is bc de. There isn't any space eaten up. It is exactly, what I expect. – MaestroGlanz Feb 14 '17 at 09:31
  • @MaestroGlanz I use the symbol ␣ for denoting a space. Try \expandafter\@cdr\@firstofone{␣}abcde\@nil and you will get bcdeas the space token delivered by \@firstofone will not be taken into account when \@cdr "fetches" its first argument. Discarding leading space tokens is standard behaviour when TeX "fetches" undelimited arguments. But it is not standard behaviour when TeX fetches delimited arguments. Therefore \expandafter\@cdr\@firstofone{␣}a␣bcde\@nil will deliver ␣bcde instead of a␣bcde and \expandafter\@car\@firstofone{␣}a␣bcde\@nil will deliver a instead of . – Ulrich Diez Feb 14 '17 at 09:49