4

I'd like to implement sizing functions like \Huge in a way that makes them work in math mode as well. Essentially I want to create a command myHuge which maps {foo \myHuge bar} to {foo \mbox{\Huge $bar$}} so that the sizing command is in text mode while everything else still is in math mode.

The problem here is that for the sake of consistency I want to use the same syntax used for text mode sizing commands, i.e. apply the command to everything up to the end of the current group. This answer gave me an idea of how to achieve this using \afterassignment in combination with assignment to a tokens register. But for some reason, this doesn't work well with \frac:

\documentclass[10pt]{article}
\usepackage{amsmath}
\newtoks\myToks
\def\myHuge{\afterassignment\myHugeDo\myToks=\bgroup\Huge$}
\def\myHugeDo{\bgroup\mbox{\the\myToks$\egroup}\egroup}

\begin{document}
This works: $a + {b \myHuge c} + d$ \\
This does not: $\frac{\myHuge x}{y}$ \\
This however does: $\frac{{\myHuge x}}{y}$
\end{document}

I had even included the outer level of \bgroup…\egroup in the definition of \myHugeDo in an attempt to make the second example behave like the third, but apparently that's not where the problem lies. Using {…} instead doesn't improve things. The message I get is this:

! Missing $ inserted.
<inserted text> 
                $
l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Missing } inserted.
<inserted text> 
                }
l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Missing } inserted.
<inserted text> 
                }
l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Missing } inserted.
<inserted text> 
                }
l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Too many }'s.
<argument> \the \myToks $\egroup 

l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Too many }'s.
\mbox #1->\leavevmode \hbox {#1}

l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Too many }'s.
\myHugeDo ...mbox {\the \myToks $\egroup }\egroup 

l.9 This does not: $\frac{\myHuge x}{y}
                                       $ \\
! Missing $ inserted.
<inserted text> 
                $
l.10 This however does: $\frac{{\myHuge x}}{y}
                                              $
! Extra }, or forgotten $.
\frac  #1#2->{\begingroup #1\endgroup \@@over #2}

l.10 This however does: $\frac{{\myHuge x}}{y}
                                              $
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./t.aux) )
(\end occurred inside a group at level 1)

### simple group (level 1) entered at line 10 ({)
### bottom level

How can I fix this? Is there a different approach I should consider?

3
  • You may have noticed that LaTeX never uses {<font switch>...} in math mode (it used to, but things changed with LaTeX2e, issued more than 20 years ago).
    – egreg
    Commented Jun 29, 2015 at 15:29
  • @egreg: Yes, I noticed. Do you mean to imply that this was due to issues like the one I'm writing about, and not a mere design decision? After all, it does support {foo \scriptstyle bar}, which has a very similar visual effect as a font size switch.
    – MvG
    Commented Jun 29, 2015 at 15:34
  • \scriptstyle performs a very different action than a font switching command.
    – egreg
    Commented Jun 29, 2015 at 15:51

1 Answer 1

6

In the early days of LaTeX, a syntax like {\rm foo} was used in math mode, where nowadays \mathrm{foo} is the correct syntax.

With the release of LaTeX2e in 1994, such commands were disabled by default. They continue to work with the standard classes for compatibility reasons, but they shouldn't be used in newer documents.

There are several reasons for such a choice: clearer delimitation of what's to be typeset in a different font, for instance.

The fact that size changing commands still are in the form of declarations is of no relevance. Changing font size in the middle of a paragraph should be avoided; the common place of font size changing commands is in environment definitions. However, in some situations like title pages, things like {\Large text\par} can appear, but they are rare.

If you really want to use larger letters in math, you can easily define commands such as \mathHuge:

\newcommand{\mathHuge}[1]{\mbox{\Huge$#1$}}

which will not suffer from the problem you see from your definition in \frac.

Why doesn't it work in \frac? Here is the definition of \frac in LaTeX:

% latex.ltx, line 4355:
\def\frac#1#2{{\begingroup#1\endgroup\over#2}}

and it's essentially the same in amsmath.

What happens when you do \frac{\myHuge a}{b}? You get

{\begingroup\myHuge a\endgroup\over b}

The { and \begingroup are executed, which means opening the respective group types, then \myHuge is expanded, leading to

\afterassignment\myHugeDo\myToks=\bgroup\Huge$a\endgroup\over b}

OK, \myHugeDo is set aside and \myToks starts; since a token register assignment must end with an explicit brace, the register will be loaded with

\Huge$a\endgroup\over b

Since the assignment has been performed, \myHugeDo is expanded, producing

\bgroup\mbox{\the\myToks$\egroup}\egroup

Another group is started, then a box is built with

\mbox{\Huge$a\endgroup\over b$\egroup}

and this is where things go wrong.

The definition of \mbox is \leavevmode\hbox{#1}, so we get

\leavevmode\hbox{\Huge$a\endgroup\over b$\egroup}

and you see that \endgroup is between two $ tokens, so completely out of place.

Is there a way out? I see none. And even if it existed, such a syntax should not be promoted.

2
  • Thanks a lot, in particular for the explanation of why this fails. Indeed, if I do \def\frac#1#2{{{#1}\over#2}} then it does work. I fear that's not the only situation where the distinction in how a group is built might cause my custom sizing command to fail. And come to think of it, there probably was a reason to use \begingroup…\endgroup in the first place. Too bad.
    – MvG
    Commented Jun 29, 2015 at 16:09
  • @MvG Yes, there are good reasons for using \begingroup...\endgroup and not {...}.
    – egreg
    Commented Jun 29, 2015 at 16:10

You must log in to answer this question.

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