42

Can someone give me a detailed starters' guide to \mathchoice? I was trying to define commands for underscript and overscript (which I have found out to exist and be called \underset and \overset), and up till now I've been using scaled arrays (matrices) for that, i.e. compressing a column vector containing the line of text and the overscript/underscript stuff to avoid the matrix expanding and leaving space between itself and the surrounding environment, and rescaling its contents to normal size or to a smaller size for the overscript/underscript.

Problem is, as soon as the height of a "cell" of this column vector is altered, my text starts flying above the rest of the text or sinking below it. So I have to work on the scalings and it's often boring to find the exactly right scaling factors to align the text with overscript/underscript with nearby text. Working on this, I tried to define a command \lower, and bumped into a Command \lower already defined error. I looked \lower up, and found out about the existence of something called \mathchoice. Problem is, what I found seems to be only troubleshooting, and to require a minimal knowledge of what \mathchoice is, or at least of what certain lines of code (as \def\xfb#1#2{\fbox{$#1#2$}}) do, which I don't have; or maybe what I've found (i.e. Proper use of \mathchoice) is meant to only give practical advice, i.e. to give you code that works, rather than explaining what one is doing with the recommended code.

To make an example unrelated to \mathchoice, what I mean is what I found seems to either require the reader to know \newcommand{\norm}[1]{\left\|#1\right\|} is defining a new command, which is invocated by \norm, has one parameter, and places it between \left\| and \right\|, which are automatically resized double vertical bars, or to only give the reader the code without any intention of explaining it does that. \mathpalette seems to be closely related to \mathchoice, so it wouldn't be inappropriate to ask for help on both, I presume.

9
  • 2
    Can you give some code in form of a mwe and perhaps make your question better understandable? What exactly is your question, to begin with?
    – Johannes_B
    Commented Dec 6, 2013 at 20:00
  • Why is the title of your question only related to the 'extra' question at the end, and not to what is the main question? (And see if tex.stackexchange.com/questions/35065/… helps.) Commented Dec 6, 2013 at 20:05
  • 3
    It's best not to ask two questions in one, I'd delete the last comment and change the title to be about mathchoice, about which you could look here tex.stackexchange.com/questions/43978/proper-use-of-mathchoice/… Commented Dec 6, 2013 at 20:17
  • 1
    Johannes_B, I don't know what an mwe is, and how can I give code if I'm asking to get started on learning how to use something totally unknown to me? My question is I'd like to be introduced to \mathchoice, what it is, what it does, its syntax and the meaning of what I write to use it. So when you tell me to write something like \def\fb{\mathpalette\xfb} I'd like to know what I'm telling LaTeX to do. Torbjørn T., you're absolutely right :). The fact is, when I asked the question, I only saw the title field because it said something I only partly read. How do I correct that?
    – MickG
    Commented Dec 6, 2013 at 21:13
  • 2
    @user42315 your question is not answerable as written (and probably would be closed as "unclear what you are asking" on most sites, but we tend to try not to close questions here for new users:-) the title asks for xelatex/latex distinction, most of the question asks for detail on \mathchoice but hints (and the tag hints) of a real question about math sub/super scripts. You need to decide if you want to ask about your positioning problem (and don't mention \mathchoice which may well not be relevant) or ask a technical question about how \mathchoice works. Commented Dec 6, 2013 at 21:22

2 Answers 2

39

\mathchoice aims to solve the problem of defining commands for math mode that behave differently in the four main math styles, that is, display, text, script and scriptscript. I'll gloss over the four secondary (or “cramped”) styles, which are not of a concern with \mathchoice.

When TeX is in a display it uses display style; in an inline formula it uses text style. But subscripts or superscripts use, at the first level, script style, and from the second level up, scriptscript style.

However, when typesetting a fraction in display style, TeX uses text style for the numerator and the denominator. But it will use script style when the fraction starts out in text style.

Let's make an example. In amsmath there is \iint that typesets a double integral; it is built by typesetting two integral signs, but they must be closer to each other than what would result by \int\int. However, the amount of back up must be bigger in display style than in the other styles, so amsmath uses

\mkern-7mu\mathchoice{\mkern-2mu}{}{}{}

which means that the back up will be -9mu in display style, but only -7mu in the other styles.

Another important example is \text@, which is amsmath internal for \text:

\def\text@#1{{\mathchoice
  {\textdef@\displaystyle\f@size{#1}}%
  {\textdef@\textstyle\f@size{\firstchoice@false #1}}%
  {\textdef@\textstyle\sf@size{\firstchoice@false #1}}%
  {\textdef@\textstyle \ssf@size{\firstchoice@false #1}}%
  \check@mathfonts
  }%
}

Apart from minor details, this achieves the purpose by typesetting four boxes using different fonts: \f@size is LaTeX internal for the current font size, \sf@size and \ssf@size are the internals for the font sizes corresponding to sub/superscripts of first and second level. When finally TeX makes a decision to what style the subformula containing \text is to be typeset, it will use one of the four boxes.

Those boxes are constructed before TeX know which one it has to use, because of what's perhaps the biggest flaw in TeX's design, which is how it manages fractions. But describing this would take too far.

The primitive \mathchoice has four arguments that should by math material; they are typeset in the corresponding style and stored away:

\mathchoice{<material for display style>}
           {<material for text style>}
           {<material for script style>}
           {<material for scriptscript style>}

(in the definition of \text@ we see \textdef@ that calls \hbox to switch back to text mode).

The most frequent use of \mathchoice is through \mathpalette, of which you can find a description in The mysteries of \mathpalette

3
  • 2
    Changed \mkern-7mu\mathchoice{-2mu}{}{}{} to \mkern-7mu\mathchoice{\mkern-2mu}{}{}{}
    – Dan
    Commented Dec 7, 2013 at 4:56
  • If \mathchoice already distinguishes between display style and text style, why is \if@display sometimes inserted in the first two options? Isn't the question of whether or not we're in display style already answered by \mathchoice?
    – justin
    Commented Aug 3, 2015 at 20:25
  • @justin \if@display is a completely different thing; it's used by amsmath for different purposes.
    – egreg
    Commented Aug 3, 2015 at 20:28
8

The best answer to this question (and many other questions) is TeX By Topic by Victor Eijkhout. Try texdoc texbytopic for a free copy of this great book.

Anyway, it tells you that there are eight math modes in TeX:

Math formulas are set in any of eight styles: D display style, T text style, S script style, SS scriptscript style, and the four ‘cramped’ variants D', T', S', SS' of these. The cramped styles differ mainly in the fact that superscripts are not raised as far as in the original styles.

and further.

The primitive command

  \mathchoice{D}{T}{S}{SS}

lets the user specify four variants of a formula for the four styles. TEX constructs all four and inserts the appropriate one.

You must log in to answer this question.

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