4

I don't quite understand how braces and spaces are treated in LaTeX3.

For example: The documentation uses the following convention for N parameters:

\my_func:N \my_tl

And for n parameters:

\my_func:n { \my_tl \my_tl }

I am wondering if these are equivalent:

\my_func:N \my_tl % vs
\my_func:N { \my_tl }
\my_func:n { \my_tl \my_tl } % vs
\my_func:n { {\my_tl} {\my_tl} }

And as a general question: When can we add/remove braces or spaces in LaTeX3?

My current understanding about the doc (could be wrong or very wrong):

  1. Whitespaces are ignored, but not entirely, at least \my_func is not the same as \my func. I have no idea where exactly whitespaces are ignored.

  2. Braces may be used to create balanced text, as well as for grouping. But there are already \group_begin: and \group_end:, so I don't know if braces are still used for grouping.

1
  • 3
    latex3 (expl3) isn't a new executable it is tex, so braces and grouping and tokenisation are all performed by tex in the same as in classic plain tex (with a slightly different catcode setup) if braces are being used to delimit a single token argument they can be dropped (but it's not good style to do so) if they are being used for tex grouping (as in your last example) they can not be dropped, exactly as in latex2e (or plain tex) Jun 28, 2020 at 19:36

1 Answer 1

7

Spaces are ignored between tokens, because the space character is assigned category code 9 (ignored). However any non letter (more precisely, any character with category code different from 11) stops the scanning for a control word name.

Thus \my func is still five tokens, precisely

\my•f•u•n•c

(the bullet separates tokens). Also _ and : are assigned category code 11, so they can be part of a control word name. Space and underscore are not equivalent.

An N argument should not be braced. For a function defined with \cs_new:Npn (or similar, but not necessarily if defined with \cs_new_eq:NN)

\my_func:N \my_tl
\my_func:N { \my_tl }

are essentially equivalent, but the latter should be avoided for consistency.

Why? For instance \token_to_str:N { \foo } would be disastrous. (Thanks to Joseph Wright for raising the point.)

To the contrary,

\my_func:n { \my_tl \my_tl }
\my_func:n { {\my_tl} {\my_tl} }

are definitely not equivalent. The former has no groups, the latter has two groups.

Braces behaves the same as ever: they delimit n arguments, but they create a group (a simple group in “TeX, the program” language) if not used to delimit an argument. The functions \group_begin: and \group_end: are the primitives \begingroup and \endgroup respectively and form a group as well (a semisimple group).

It's better to use them for grouping, but in some circumstances {} can be used.

4
  • There are no n arguments in plain tex. So from a plain tex perspective, braces are only for grouping? Then for \my_func:n { \my_tl \my_tl } there must be some magic inside \my_func:n that removes one level of grouping. Am I correct on this?
    – Cyker
    Jun 29, 2020 at 16:39
  • @Cyker What do you mean? If you do \def\foo#1{x#1y}, then the argument to \foo is what expl3 calls a “type n” argument. There is no grouping if you call \foo{abc}, because the braces are discarded as soon as the argument is grabbed.
    – egreg
    Jun 29, 2020 at 16:41
  • I see all braces as grouping in plain tex, but the command beforehand can discard them. For \foo{abc}, I see it as: \foo reads {, a, b, c, }, discards { and } and does its job with abc. The grouping doesn't take effect because \foo discarded the braces. The result is no grouping, but it doesn't harm if all braces are seen as grouping conceptually?
    – Cyker
    Jun 29, 2020 at 16:53
  • @Cyker I see it differently.
    – egreg
    Jun 29, 2020 at 17:01

You must log in to answer this question.

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