Experimenting with macro definitions that use strange delimiters (see e.g. this or this question), I thought it would be fun to have a macro that takes an argument of the form "a/b" and turns this into a fraction.

This is achieved by

\def\myfrac#1/#2 {\frac{#1}{#2}}

which scans everything before the slash into #1 and everything following the slash until next space into #2. Hence,

$\myfrac 72/31 $
$\myfrac 7/13 34\times\pi$


compiled code

as desired. But -- unsurprisingly --

$\myfrac 72/31$

gives a **! Missing } inserted.** error, as -- of course -- the delimiting space is missing. To circumvent this, I wanted to define

\def\myfrac#1${\@myfrac#1 }
\def\@myfrac#1/#2 {\frac{#1}{#2}}

i.e. scan everything up to the next $, add a space to the end and then pass it to the other macro. This, however, results in ! Missing $ inserted.

Note, however, that it works with a "normal" delimiter:


\def\myfrac#1x{\@myfrac#1 }
\def\@myfrac#1/#2 {\frac{#1}{#2}}

$\myfrac 72/31x$ $\myfrac 3/15 x$
$\myfrac 7/13 34\times\pi x$


compiled output

as expected. So is there a way to replace x in the above code with $?

  • 5
    This is because the <argument> text gobbles everything, including the $, which is never replaced. Use \def\myfrac#1${\@myfrac#1 $}.
    – Werner
    Aug 13, 2013 at 18:50
  • By the way, siunitx offers with the option quotient-mode=fraction the same: \num[quotient-mode=fraction]{72/31} Aug 14, 2013 at 20:07

2 Answers 2


Since this is just for experimenting, here it is:

\def\myfrac#1${\@myfrac#1 $}
\def\@myfrac#1/#2 #3${\frac{#1}{#2}#3$}
$\myfrac 72/31$ $\myfrac 3/15$ $\myfrac 7/13 34\times\pi$

enter image description here

The “parameter text” is removed from the input stream and substituted with the replacement text, so the closing $ must be reinserted at the end.

The $ in the replacement text of \myfrac and in the parameter text of \@myfrac can be any token that's not expected to be in the formula, so also \@nil could be used; but using $ is just as safe.

Don't use delimited arguments in LaTeX at the user level.


Your actual problem is that you are falling afoul of the issue discussed (ironically, this week) in Is \def sensitive to spacing?. Don't put a space after #2 and you will be fine. The reason your second workaround works is that you insert the space explicitly in \@myfrac. The reason your first workaround doesn't work is that you remove the $ as part of macro expansion (it's absorbed as part of the arguments) but don't put it back, so TeX is quite right that it's missing.

  • 1
    Well the space is there to act as "second delimiter" so that something like \myfrac 7/13 is set as \frac{7}{13} and not \frac{7}{1}3. The point of the "exercise" was to use two alternative delimiters (either a space or a $), so the space is desired.
    – Jonathan
    Aug 14, 2013 at 22:45
  • Perhaps you want a second delimiter that you don't implicitly consider to be discardable text, then. This will free you from having to gobble to the end of the math group, too.
    – Ryan Reich
    Aug 14, 2013 at 23:01

You must log in to answer this question.

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