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$
produce

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:
\documentclass{minimal}
\makeatletter
\def\myfrac#1x{\@myfrac#1 }
\def\@myfrac#1/#2 {\frac{#1}{#2}}
\makeatother
\begin{document}
$\myfrac 72/31x$ $\myfrac 3/15 x$
$\myfrac 7/13 34\times\pi x$
\end{document}
produces

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

<argument>text gobbles everything, including the$, which is never replaced. Use\def\myfrac#1${\@myfrac#1 $}. – Werner Aug 13 '13 at 18:50siunitxoffers with the optionquotient-mode=fractionthe same:\num[quotient-mode=fraction]{72/31}– Qrrbrbirlbel Aug 14 '13 at 20:07