2

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

enter image description here

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.

  • 4
    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 06 '18 at 17:21
  • 2
    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 06 '18 at 17:24
  • Thank you, @david-carlisle. But still, in my case, I must use \frac without the standard braces. Is there another way to do this, please? – user8396743 Feb 06 '18 at 17:28
  • see https://tex.stackexchange.com/questions/271466/problem-in-a-macro-definition-plain-tex/271467#271467 – David Carlisle Feb 06 '18 at 17:29
  • you must need to delimit the numerator and denominator with something \bgroup/\egroup are not really suitable for this, but it is hard to suggest an alternative if you do not say why {} can not be used. – David Carlisle Feb 06 '18 at 17:30
  • The question is: why should you need to use \frac without the standard braces? This seems an XY-question: probably, if you describe your problem, a solution can be found in a different fashion. – egreg Feb 06 '18 at 17:32
  • 1
    How about \def\num{x} \def\denom{x-1} $ \frac\num\denom $ – Steven B. Segletes Feb 06 '18 at 17:34
  • 1
    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 06 '18 at 17:39
  • 1
    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 06 '18 at 17:40
  • @steven-b-segletes: I guess your suggestion \def\num{x} may work in my case. The only problem is that I have too many fractions, so this solution may not be scalable. I would appreciate it if you can show me how to use \catcode to make a different set of characters act like braces. – user8396743 Feb 06 '18 at 17:47
  • 1
    Even without catcode changes you can do this: \documentclass{article} \let\svfrac\frac \def\frac[#1][#2]{\svfrac{#1}{#2}} \begin{document} $ \frac[x][x-1] $ \end{document} – Steven B. Segletes Feb 06 '18 at 17:49
  • 1
    @steven-b-segletes: This solved my problem. Many thanks. Once I collect enough points, I will come back and upvote these helpful comments. – user8396743 Feb 06 '18 at 17:57
  • @user8396743 -- your explanation about using an "r" package is important information. It would be very helpful if you added it into your question as an edit. information that appears only in comments sometimes gets lost, and thus fails to be helpful to future searchers for answers to similar questions. – barbara beeton Feb 06 '18 at 18:03
  • @barbara-beeton: Thank you for the suggestion. I will implement it right away. – user8396743 Feb 06 '18 at 18:36

2 Answers2

3

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.

wipet
  • 74,238
1

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}