1

It is possible in LaTeX to write expressions like \frac12, which should be equivalent to expressions like \frac{1}{2}. Is there a formal term for the first form of notation?

siracusa
  • 13,411
  • 6
    Welcome to TeX.SX! The form \frac12 works because the argument to an undelimited TeX macro (like \frac) is either a single token (1 is a single token, for example) or, a group of tokens delimited by {...}, in which case the outer braces are removed. That's why both forms are equivalent. However there isn't a special name to one of them... – Phelype Oleinik Aug 01 '19 at 02:12
  • 2
    Braces for macro arguments in TeX work similar to braces for statements in languages like C or JavaScript: they just group several of them to a single unit. While always using braces is generally considered a good coding style in LaTeX, as it improves readability, from the compiler's perspective the braces in \frac{1}{2} are just "more work to do", because it's unnecessary to group a single token. – siracusa Aug 01 '19 at 03:01
  • 1
    @siracusa I am not agree at all. I remember that a time ago David Carlisle said to me that if removing braces there is a chance to break math in multiple lines. That's why the use of braces is always a good practise. – manooooh Aug 01 '19 at 04:07
  • @manooooh Can you elaborate on how it would break? – siracusa Aug 01 '19 at 04:13
  • @siracusa I am not finding the fragment where (I believe) David said that. Let's wait his advice or instead decline my suggestion. – manooooh Aug 01 '19 at 04:26
  • 4
    @manooooh that is a different thing. ${ 1 = 0 }$ the braces act essentially like an implicit \hbox and prevent linebreaking and space stretching, but braces used as macro argument delimiters are a different thing entirely. – David Carlisle Aug 01 '19 at 07:00
  • @siracusa ^^^^^^ – David Carlisle Aug 01 '19 at 07:00
  • For the curious, the braces in @DavidCarlisle's example ${ 1 = 0 }$ delimit what Knuth calls a subformula, and indeed they have consequences I didn't anticipate before reading about them in the TeXbook! – frougon Aug 01 '19 at 07:31
  • @DavidCarlisle so what are the differences if \frac12 will not prevent linebreaking nor space stretching? – manooooh Aug 01 '19 at 08:18
  • 1
    @manooooh none at all to latex, at the tex layer it is impossible to detect if a single token macro argument is used with braces. But to human readers, syntax checkers and highlighters and latex to - whatever convertors, it is far better to stick to the documented syntax. – David Carlisle Aug 01 '19 at 08:23
  • With \frac12 you have a control word token \frac triggering the processing of two braceless undelimited arguments. With \frac{1}2 you have a control word token \frac triggering the processing of an undelimted argument nested in braces and a braceless undelimited argument. With \frac1{2} you have a control word token \frac triggering the processing of a braceless undelimted argument and an undelimited argument nested in braces. With \frac{1}{2} you have a control word token \frac triggering the processing of two undelimited arguments, each of them nested in braces. – Ulrich Diez Aug 01 '19 at 08:48
  • For the interested: I elaborated in detail on the ways in which (La)TeX processes arguments in the discussion How does TeX look for delimited arguments? . – Ulrich Diez Aug 01 '19 at 08:53

2 Answers2

4

No.


This is just part of the normal way TeX absorbs non-delimited macro arguments. If you define

\def\foo#1{Something using #1 (or not).}

or equivalently

\newcommand\foo[1]{Something using #1 (or not).}

when \foo is expanded, it absorbs the next token or balanced group in the input stream as its argument (which is available as #1 in the definition). Assuming normal category codes

  • a token is a single character (except \, {, }, % or a space or newline (since those are ignored)) or a command sequence (something starting with \),
  • a balanced group starts with { and ends with the first } that does not belong to a different group (that started later). The braces are stripped from the group upon absorption (that is, #1 does not contain them).

This is why \foo x and \foo{x}, \newcommand{\foo} and \newcommand\foo or \frac 12, \frac{1}{2}, \frac{1}2 and \frac1 2 are equivalent.

It is good practice to choose the version that is clearest, which usually means using a group. Personally, I explicitly omit the group in cases where I have to input a single token, like \newcommand\foo.

schtandard
  • 14,892
1

It might be worth adding that distinguishing between single-token arguments and arguments in braces is more explicit in LaTeX3/expl3. By convention, every expl3 macro name should end in : followed by a sequence of letters, called argument specifiers, each reflecting the type of the corresponding argument.

Beside many others we have

  • n for braced-group or single-token arguments, and
  • N for single-token arguments only, usually macro/function names or variable names.

Those specifiers still don't change the way (La)TeX processes the individual arguments, but it gives a hint to the user about how the arguments are used inside the macro.

For example, the l3tl package provides two functions \tl_if_empty:nTF and \tl_if_empty:NTF. While both functions check if a token list is empty and execute code conditionally, the former is expected to be called with a braced group of tokens, where the latter expects a token list variable that is first expanded before the test is performed on its contents.

siracusa
  • 13,411