5

Not sure whether the standard actually allows this, but at least in my LaTeX installation (TeX Live 2019/Debian), I can write for instance $\sqrt\frac12$ instead of $\sqrt{\frac{1}{2}}$. However, this does not work for manually defined commands:

The MWE

\documentclass{article}
\newcommand{\myfrac}[2]{\frac{#1}{#2}}
\newcommand{\mysqrt}[1]{\sqrt{#1}}

\begin{document}
$\mysqrt\myfrac12$
\end{document}

produces the error

! Argument of \myfrac has an extra }.
<inserted text> 
                \par 
l.6 $\mysqrt\myfrac
                   12$

Question: How do I define my own commands in a way such that they also allow for such shorthands skipping the brackets.

I fear that this has been answered somewhere already. In my defence, “robust commands” seems to mean an entirely different thing, so I was not able to find an answer here.

4
  • 6
    Don't go into this habit. There are cases where one can drop braces but it doesn't work everywhere and it needs quite some skill and knowledge of the code to know when it can be safely done and when not. It is much easier to simply add braces. – Ulrike Fischer Jul 3 '20 at 10:47
  • 4
    it's a really bad habit to get in to but you have already done this macro arguments may always omit the brackets and take a single token \mysqrt\myfrac is legal and equivalent to \mysqrt{\myfrac} so you get what you get. – David Carlisle Jul 3 '20 at 10:54
  • Maybe that should be the topic of another question, but why is that habit so bad? \frac12 is way shorter than \frac{1}{2}. It is not only faster to write (a problem IDEs might help to mitigate) but also takes less space in the code and makes it easier to parse. It also seems quite robust, for instance (\dots)^\frac12 works as well. (Why) should I stop doing that? – Keba Jul 5 '20 at 15:18
  • The same holds for \sqrt2, also I guess I have never actually used \sqrt\frac12 despite that it works. \sqrt{\frac12} might even be more readable. Hence the “why should I not go into this habit” question mainly ask for \frac12 etc., not for \sqrt\frac12. – Keba Jul 5 '20 at 15:19
14

In TeX/LaTeX you can omit curly braces with a non-delimited argument only in case the argument is to consist of a single token.

Let's look at your example:

\documentclass{article}
\newcommand{\myfrac}[2]{\frac{#1}{#2}}
\newcommand{\mysqrt}[1]{\sqrt{#1}}

\begin{document}
$\mysqrt\myfrac12$
\end{document}

In the TeXbook Donald E. Knuth makes an analogy between the ways in which TeX works and the ways in which a digestive tract works:

The eyes look at the .tex-input-file line by line. After looking at a line of .tex-input they move the characters of that line of .tex-input into the mouth.
("look and move" here means

  • copying from the .tex-input-file to some area of memory managed by TeX,
  • converting from the computer-platform's character-encoding to the TeX-engine's internal character-encoding,
  • removing all space-characters at the right end of the line,
  • attaching at the right end of the line a character whose code-point-number in the TeX-engine's internal character-encoding-scheme equals the value of the integer-parameter \endlinechar. Usually that value is 13 while 13 denotes the return-character in the TeX-engine's internal character-encoding-scheme. With traditional TeX-engines the internal-character-encoding-scheme is ASCII. With TeX-engines based on XeTeX or LuaTeX the internal character-encoding-scheme is unicode/utf-8 whereof ASCII is a subset. )

The mouth takes these input-characters for a set of instructions for producing tokens (control-sequence-tokens, character-tokens) and sending these tokens down the gullet. (The mouth divides the "stream of input-characters" produced by the eyes into small bites and according to these bites produces tokens (control-sequence-tokens, character-tokens) and sends these tokens down the gullet which implies that in the gullet you have a "stream of tokens"/a "token-stream".)

In the gullet expansion of expandable tokens, e.g. macros, takes place. This means expandable tokens get removed from the token-stream and replacement-tokens (if there are any) are inserted into the token-stream. This happens (in some sort of regurgitation-process) until there are no expandable tokens left in the token-stream. The tokens that result from expanding expandable tokens in the gullet are send to TeX's stomach. Thus usually only non-expandable tokens reach TeX's stomach.

I wrote "usually" here because there are exceptional circumstances where expansion is suppressed so that expandable tokens can reach the stomach: E.g., when the stomach requests tokens from the gullet that shall belong to the ⟨parameter text⟩ or the ⟨balanced text⟩ of a \def-assignment, expansion is suppressed. With the ⟨balanced text⟩ of an \edef-assignment expansion is not suppressed. E.g., with the tokens belonging to the ⟨balanced text⟩ of a token-register-assignment expansion is suppressed. (But with token-register assignments expansion is not suppressed until finding the left brace { before the ⟨balanced text⟩, which in turn is trailed by ⟨right brace⟩.)

In the stomach processing of non-expandable tokens takes place.


Let's look at the line of .tex-input $\mysqrt\myfrac12$:

The eyes have looked at the line of .tex-input and have produced the characters (not "tokens" yet!)
$, \, m, y, s, q, r, t, \, m, y, f, r, a, c, 1, 2, $, ⟨return⟩
into the mouth.
(The ⟨return⟩-character is due to the integer-parameter \endlinechar having the value 13 which denotes the ⟨return⟩-character in the TeX-engine's internal character-encoding-scheme.)

First the mouth produces the catcode-3(math-shift)-character-token $3 and sends that down the gullet. Such a character-token is not expandable, so it just passes the gullet and reaches the stomach where it causes switching to math-mode and where it is removed.

So in the mouth the characters
\, m, y, s, q, r, t, \, m, y, f, r, a, c, 1, 2, $, ⟨return⟩
are left.

In the mouth TeX produces the control-word-token \mysqrt. This is sent down the gullet where expansion of expandable tokens takes place.
So in the mouth you have the characters: \, m, y, f, r, a, c, 1, 2, $, ⟨return⟩ .
In the gullet you have the tokens: \mysqrt .

The token \mysqrt is expandable, therefore the gullet requests more tokens from the mouth, i.e., tokens that are suitable for forming \mysqrt's non-delimited argument.

The next character in the mouth is not an opening-curly-brace-character (is not a character of category-code 1(begin group)). Thus TeX assumes that \mysqrt's non-delimited argument consists of a single token, not of a (curly-brace-balanced) set of tokens nested in a pair of curly braces. The mouth produces the token \myfrac and sends it down TeX's gullet.

So in the mouth you have the characters: 1, 2, $, ⟨return⟩ .
In the gullet you have the tokens: \mysqrt, \myfrac .

Expansion of \mysqrt in TeX's gullet yields:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \sqrt, {1, \myfrac, }2 .

Expansion of \sqrt in TeX's gullet yields:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \protect, \sqrt␣, {1, \myfrac, }2 .

(The denotes that a space-character (codepoint-number 32 in ASCII) is part of the name of the control-sequence-token in question. Under normal category-code-régime such tokens cannot be obtained by having TeX read and tokenize lines/characters of a file of .tex-input. But they can be obtained via \csname..\endcsname. They also can be obtained by temporarily switching the category-code of the space-character to 11(letter) before having TeX read and tokenize things from the .tex-input-file.)

\protect in this situation equals \relax and therefore is not expandable and therefore is sent down into TeX's stomach where it has no effect, thus you get:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \sqrt␣, {1, \myfrac, }2 .
Tokens in the stomach: \protect .

Now \protect is processed by the stomach and hereby gets removed. (The meaning of \protect equals the meaning of the \relax-primitive which in turn denotes no-op for TeX's stomach.)

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \sqrt␣, {1, \myfrac, }2 .
Tokens in the stomach:

Expanding \sqrt␣ in TeX's gullet yields:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \@ifnextchar, [12, \@sqrt, \sqrtsign, {1, \myfrac, }2 .
Tokens in the stomach:

Processing \@ifnextchar yields that at some stage you have:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \sqrtsign, {1, \myfrac, }2 .
Tokens in the stomach:

Expanding \sqrtsign in TeX's gullet yields:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \radical, "12, 212, 712, 012, 312, 712, 012, \relax, {1, \myfrac, }2 .
Tokens in the stomach:

The tokens \radical, "12, 212, 712, 012, 312, 712, 012, \relax, {1 are not expandable. Therefore they will not be processed in TeX's gullet but will be sent towards TeX's stomach. Therefore you get:

Characters in the mouth: 1, 2, $, ⟨return⟩ .
Tokens in the gullet: \myfrac, }2 .
Tokens in the stomach: \radical, "12, 212, 712, 012, 312, 712, 012, \relax, {1 .

The stomach needs more tokens in order to be able to carry out the \radical..-directive. The stomach "addresses" the gullet in order to have the gullet deliver more tokens.

When in TeX's gullet the attempt of expanding \myfrac takes place, \myfrac is followed by a closing curly brace token.

This circumstance in TeX's gullet disturbs TeX's gathering of \myfrac's two non-delimited arguments.

TeX makes the user aware of its disturbance by submitting an error message ! Argument of \myfrac has an extra }.

8

The input \sqrt\frac12 only works by chance.

The macro \sqrt is basicelly defined as

\@ifnextchar[\@sqrt\sqrtsign

Since no [ follows the call, you get

\sqrtsign\frac12

and then

\radical "270370\relax\frac12

Now TeX wants to see to what \radical applies to and it happens to be \frac12 that expands to

{\begingroup 1\endgroup\over 2}

and the braces allow the construction to work.

Try with

\sqrt3^2

and you'll get the same as

{\sqrt{3}}^2

With \sqrt\cos x you get

! Missing { inserted.
<to be read again>
                   \mathop
l.6 $\sqrt\cos
               x$

Learn to use the proper markup:

\sqrt{\frac{1}{2}}

and you'll be OK every time.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.