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.

MickG
  • 5,426
  • 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 Dec 06 '13 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 http://tex.stackexchange.com/questions/35065/xetex-and-xelatex-in-texworks-editor/35067#35067 helps.) – Torbjørn T. Dec 06 '13 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 http://tex.stackexchange.com/questions/43978/proper-use-of-mathchoice/43982#43982 – David Carlisle Dec 06 '13 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 Dec 06 '13 at 21:13
  • @user42315 you just click the edit link below the question and then you can edit the title and text of your question. – David Carlisle Dec 06 '13 at 21:16
  • As for that link, the problem is it looks like troubleshooting, not like an introduction. For example, it gives lots of code and (to my eyes) seems to assume the reader has a clue what that code does, which I don't. The original purpose of the question was to find an easier way to do what I later found out to be \overset and \underset, if you want to know. – MickG Dec 06 '13 at 21:16
  • 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. – David Carlisle Dec 06 '13 at 21:22
  • Thanks D.C., I just hadn't seen the link :). Is the question clear now? P.S. the tag is there only because I'm forced to give at least one, couldn't create a new tag and I didn't know what to tag, and because the original purpose of the question was basically to discover the (then unknown) commands \overset and \underset the best possible existing tag seemed to be vertical-alignment. If you have a suggestion on something better to tag, just tell me :). – MickG Dec 06 '13 at 21:34
  • I marked up the code sections, but still I think the question should be closed as a duplicate, perhaps of this one http://tex.stackexchange.com/questions/110631/command-for-fontsize-dependent-code-selection/110637#110637 \mathchoice is a very low level command and definitely not for beginners. If you post a small document using it that doesn't do what you expect then perhaps an answer helping you could be constructed. – David Carlisle Dec 06 '13 at 21:51

2 Answers2

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

egreg
  • 1,121,712
  • 2
    Changed \mkern-7mu\mathchoice{-2mu}{}{}{} to \mkern-7mu\mathchoice{\mkern-2mu}{}{}{} – Dan Dec 07 '13 at 04: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 Aug 03 '15 at 20:25
  • @justin \if@display is a completely different thing; it's used by amsmath for different purposes. – egreg Aug 03 '15 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.

Boris
  • 38,129