\bgroup
is a synonym for {
, which is defined in Plain TeX using \let\bgroup={
.
It interacts with TeX's "digestive system" in hairy ways: {
s and \bgroup
s start the same sort of groups, called simple groups, and each can be terminated with either }
s or \egroup
s, since they are the same. But when the TeX digestive system encounters them, they are of different catcodes, so commands that look ahead, e.g., in LaTeX with \section\bgroup Title}
, can break this matching.
\begingroup
is different. It is a TeX primitive, and it matches a different sort of group that TeX accounts for separately, called "semi-simple groups" (a Knuth joke, I assume). Thus a \begingroup
must be terminated by an \endgroup
, not a }
, and vice versa for \endgroup
.
I generally avoid \bgroup
, and use \begingroup
, but \bgroup
could be useful if you are messing about with a nested token list.
I have said that you are inclined to plain TeX code. But your question seemed me to be interesting. So, I tried to do something despite this is LaTeX problem.
You can start with experimenting with my code:
\newcount\openLnum
\newtoks\currtext
\def\Open{\begingroup\let\bgroup=\relax \let\egroup=\relax
\expandafter\checkbracesJ\autobracelist\end
\let\ifIamInGroup=\iffalse \currtext={}\checkbracesA
}
\def\checkbracesA{\futurelet\tmp\checkbracesB}
\def\checkbracesB{%
\let\next=\checkbracesN
\ifx\tmp\spacetoken \let\next=\checkbracesC \let\nexxt=\checkbracesA \addtocurrtext{ }\fi
\ifx\tmp\bgroupOri \let\next=\checkbracesC \let\nexxt=\checkbracesD \fi
\ifx\tmp\egroupOri \let\next=\checkbracesC \let\nexxt=\checkbracesE \fi
\ifx\tmp\autobraced \let\next=\checkbracesH \fi
\ifx\tmp\Close \let\next=\checkbracesC \let\nexxt=\checkbracesF \fi
\ifx\tmp\Open \global\advance\openLnum by1 \let\next=\relax \fi
\next
}
\def\checkbracesC{\afterassignment\nexxt \let\next= }
\long\def\checkbracesN#1{\addtocurrtext#1\checkbracesA}
\def\checkbracesD{\begingroup \let\ifIamInGroup=\iftrue \currtext={}\checkbracesA}
\def\checkbracesE{\ifIamInGroup \addtocurrtextclosebrace
\else \currtext\expandafter{\expandafter{\the\currtext}}%
\fi \checkbracesA
}
\def\checkbracesF{%
\ifIamInGroup \addtocurrtextclosebrace \expandafter\checkbracesF
\else \expandafter\checkbracesG \fi
}
\def\checkbracesG{%
\ifnum\openLnum>0 \global\advance\openLnum by-1
\def\next{\expandafter\endgroup \expandafter
\currtext \expandafter\expandafter\expandafter
{\expandafter\the\expandafter\currtext \the\currtext}\checkbracesA}%
\else \def\next{\expandafter\endgroup \the\currtext}%
\fi \next
}
\def\checkbracesH#1{\addtocurrtext#1\futurelet\tmp\checkbracesI}
\def\checkbracesI{\ifx\tmp\bgroupOri \expandafter\checkbracesB
\else \expandafter\checkbracesD \fi
}
\def\checkbracesJ#1{\ifx#1\end \else \let#1=\autobraced \expandafter\checkbracesJ \fi}
\def\addtocurrtextclosebrace{\expandafter\endgroup
\expandafter\currtext\expandafter\expandafter\expandafter
{\expandafter\the\expandafter\currtext\expandafter{\the\currtext}}%
}
\long\def\addtocurrtext#1{\currtext\expandafter{\the\currtext#1}}
\let\bgroupOri=\bgroup
\let\egroupOri=\egroup
\def\tmp/{\let\spacetoken= }\tmp/ %
\def\Close{^\Close^}
\def\autobraced{^\autobraced^}
\def\autobracelist{}
Save it to the file (say) openclose.tex
and do the following tests:
\documentclass{article}
\input openclose
\long\def\Minipage#1{\begin{minipage}{\textwidth}#1\end{minipage}}
\long\def\Quote#1{\begin{quote}#1\end{quote}}
\begin{document}
\Open
\Minipage{\Quote{\texttt{\textbf{
Something\Close
% This inserts the closing braces automatically. It means that
% \Minipage{\Quote{\texttt{\textbf{ Something}}}} is processed.
\def\autobracelist{\Minipage \Quote \texttt \textbf}
\Open
\Minipage\Quote\texttt
\textbf Something else\Close
% This inserts the opening braces after listed commands and
% inserts the closing braces automatically.
\def\autobracelist{}
\Open
\newcommand\kk[1]{\textcolor{RoyalBlue}{\text{\textup{\textbf{\texttt{#1\Close
\message{\meaning\kk}
% This defines the \kk macro as is your desire.
\end{document}
Note that my \Open
doesn't insert the starting {
immediatelly. This is needed because \Open
command works at main TeX processor level. It cannot be inserted between [1]
and \textcolor
in your \newcommand
example.
If you need insert the open brace immediatelly then you can define \def\Openbrace{\expandafter\Open\expandafter{\iffalse}\fi}
.
Edit The \Open
...\Close
pair acts as the autobalancing procedure. If the text between \Open
...\Close
isn't balanced then the balancing braces are added. Else text si unchanged. You can try:
\Open abc\Close % no change: -> abc
\Open a{b}c{d}\Close % no change: -> a{b}c{d}
\Open a{b{c\Close % added braces: -> a{b{c}}
\Open a}b}c\Close % added braces: -> {{a}b}c
\Open a}b}c{d{e\Close % added braces: -> {{a}b}c{d{e}}
2nd Edit: I've added the support of nested \Open...\Close
pairs. First, the inner pairs are processed and then the outer pairs (like normal parenthesis):
\Open text \Open inner1\Close text \Open inner2 \Open inner3\Close text \Close \Close
->
\Open text processed-inner1 text \Open inner2 processed-inner3 text \Close \Close
->
\Open text processed-inner1 text processed-(inner2 processed-inner3 text)\Close
Edit Aug.30 New version of openclose.tex
: more efficient code and new feature: if the open brace already exists after control sequence listed in \autobracelist
then new open brace isn't inserted here.
Best Answer
Short answer to your question: No, at least not in principle.
Longer answer: Here's the actual definition of the LaTeX command
\frac
:What this shows is that LaTeX's
\frac
command is a (very well-designed) wrapper around TeX's\over
command. By the syntax rules of TeX, if the "arguments" of the\frac
command are not enclosed in curly braces, TeX will happily treat the first nonblank item/character it encounters after\frac
as#1
and the second item as#2
. Hence,\frac12
is (to TeX's parser) the same as\frac{1}{2}
, and\frac xy
-- note the space between thec
and thex
-- is the same as\frac{x}{y}
.That said, I suspect that if you get into a habit of leaving off the braces whenever the numerator and denominator both consist of a single letter or digit, you'll soon have forgotten that you're employing a shorthand method. Sooner or later, then, you'll write something like
\frac 1 12
and start wondering why it's not being rendered as\frac{1}{12}
...Addendum: You mention toying with the idea of creating the one-argument macro
Unfortunately, this isn't going to fly because TeX doesn't let you mix letters and digits in the name of a macro (unless you resort to a
\cs
...\endcs
detour). However, why even bother creating such a one-argument version of the\frac
macro? Note that\frac1{<denominator>}
is a perfectly valid LaTeX expression: the1
that follows\frac
in\frac1
will be interpreted by TeX as the#1
part of the two-argument macro -- which is exactly what you want, correct?