# Using \bgroup \egroup with \frac

I understand from this post that \bgroup is equivalent to { and \egroup is equivalent to } (Edit: turns out that my understanding was wrong, see @david-carlisle comments for details). I have a scenario where I must replace {} with \bgroup and \egroup (edit: see the reason why I must not use the braces at the end of my post). This is okay with \texttt and math subscript, but I could not use it with math fractions. With \frac, the latex file compiles without errors but does not produce the fraction I would expect. Here is a MWE:

\documentclass[12pt]{article}

\usepackage{amsmath}

\begin{document}

% texttt example
texttt example using curly braces as usual:\\
\texttt{hello}

% texttt works with \bgroup and \egroup
The same texttt example using \textbackslash bgroup and \textbackslash egroup:\\
\texttt\bgroup hello\egroup

% math subscript example
A subscript example using curly braces as usual:\\
$x_{10} = 10$

% math subscript works with \bgroup and \egroup
The same subscript example using \textbackslash bgroup and \textbackslash egroup:\\
$x_\bgroup 10\egroup = 10$

% Reason for the post: A fraction example
A fraction example using curly braces as usual:\\
$\frac{x}{x-1}$

% Using \bgroup and \egroup, the fraction looks different
The following is supposed to be the same fraction example, only this time
using \textbackslash bgroup and \textbackslash egroup.
However, the result looks different:\\
$\frac\bgroup x \egroup\bgroup x-1 \egroup$

\end{document}


And the output is

By the way, I have Googled this already and couldn't find any result that shows how to use \bgroup and \egroup with \frac. Therefore, it is likely that I'm the first to ask this question (Apologies for that :).

So my question is: How to use \bgroup and \egroup with \frac instead of the standard usage with the curly braces {}?

Edit: Here is why I must not use the braces with \frac. I'm using RndTexExams, which is a package in R, and it generates multiple and randomized test versions based on examdesign package. One multiple choice question may have different versions, and the versions are surrounded by @ from both sides, separated by pipes, and delimited by braces. Example @{Version 1}|{Version 2}@. If you try to replace Version 1 with something that has braces in it, like \texttt{Version 1}, the R package will fail to parse this question and will produce a faulty output Latex file.

• no they are not equivalent, sometimes they accidentally work due to poor error checking. If you go \texttt\bgroup that is the same as \texttt{\bgroup} and the argument to the command is just \bgroup. Anything that happens after that is just untested accidental expansion from bad input. – David Carlisle Feb 6 '18 at 17:21
• see wipet's answer to the post that you link to he carefully explains that \bgroup may only be used in some cases as an equivalent token to { delimiting macro arguments is one case where \bgroup does not work at all. – David Carlisle Feb 6 '18 at 17:24
• How about \def\num{x} \def\denom{x-1} $\frac\num\denom$ – Steven B. Segletes Feb 6 '18 at 17:34
• Is it the brace itself {} which is prohibited, or the concept of bracing? If the former, you could use \catcode to make a different set of characters act like braces. – Steven B. Segletes Feb 6 '18 at 17:39
• I'm using RndTexExams, which is a package in R, and it generates multiple and randomized test versions based on examdesign package. One multiple choice question may have different versions, and the versions are surrounded by @ from both sides, separated by pipes, and delimited by braces. Example @{Version 1}|{Version 2}@. If you try to replace Version 1 with something that has braces in it, like \texttt{Version 1}, the R package will fail to parse this question and will produce a faulty output Latex file. I hope this is clear. If you have another question or need more info please let me know. – user8396743 Feb 6 '18 at 17:40

Here are two ways:

1. Place the numerator and denominator in their own macros and then invoke \frac without braces.

2. Redefine \frac to use brackets instead of braces.

The MWE:

\documentclass{article}
\let\svfrac\frac
\begin{document}
\def\num{x}
\def\denom{x-1}
$\frac\num\denom$

\def\frac[#1][#2]{\svfrac{#1}{#2}}
$\frac[x][x-1]$
\end{document}


You cannot freely exchange { by \bgroup and } by \egroup. The { has explicit syntactical meaning in many cases. For example {...} works only as parameter delimiters or definition body delimiters. You cannot replace them by \bgroup, \egroup in such situations. See my answer here for more information.

Our second example \texttt\bgroup hello\egroup works only thanks to the accident. The \texttt is macro and the parameter delimiters are expected. If no, then only first token (\bgroup here) is read as a parameter and the hello\egroup follows after macro expansion. For sake of simplicity, assume that the \texttt is defined like this:

\def\texttt#1{{\tt#1}}


Then \texttt\bgroup hello\egroup expands to

{\tt\bgroup}hello\egroup


Note that group is opened, then \tt font is set, then group is opened, then group is closed then hello is printed and then group is closed. But it is very fortune that it works. Don't use \texttt, much more natural is (for your needs):

\bgroup \tt hello\egroup


Your fourth example about $x_\bgroup 10\egroup$ works because this is well known feature of TeX. It is not macro parameter, but it is a group inside math mode.

Your fifth example does not work, because \frac is macro and it needs explicit braces {}. But you can use TeX primitive alternative of fractions which looks like:

$a = \bgroup x \over x-1\egroup$


Finally, I'll explain why your \frac\bgroup x \egroup\bgroup x-1 \egroup doesn't work. Suppose (for sake of simplicity) that \frac is defined like:

\def\frac#1#2{{#1\over#2}}


Then your example takes first parameter \bgroup and second parameter x and expands to:

{\bgroup\over x} \egroup\bgroup x-1 \egroup


Note that group is opened by {, then next group (which starts the fraction) is opened by \bgroup then fraction "none" \over "x" is created then } closes the fraction, then \egroup closes the outer group, then group is opened, x-1 is printed and group is closed.