2

I've written some somewhat complex tikz macros which use a length to set their scale. I've realised I want them to have a different scale when used inside display math versus inline math mode, and I thought I would be able to achieve this using \mathchoice and \setlength. However, I can't seem to get it to work, and I'm not sure why.

Here's a MWE demonstrating the issue:

\documentclass{article}

\newlength{\mylength}
\setlength{\mylength}{0.4em}

\newcommand{\autoscale} {%
    \mathchoice%
        {\setlength{\mylength}{0.4em}}%
        {\setlength{\mylength}{0.2em}}%
        {\setlength{\mylength}{0.2em}}%
        {\setlength{\mylength}{0.2em}}%
}

\newcommand{\mycirc}{%
    \autoscale
    \begin{tikzpicture}
        \draw (0,0) circle (\mylength);
    \end{tikzpicture}%
}

\usepackage{tikz}

\begin{document}

A circle in a display equation:
\[
    x = \mycirc
\]

An inline one, which should be half the size: $y = \mycirc$.

\end{document}

The output looks like this: enter image description here

By trying variations on this code, it seems that \mathchoice is working as I expect it to (running its first argument when called in display mode and its second in inline math mode). If I insert the \setlength commands directly into the equations that works as expected too. So it seems to be some combination of \setlength and \mathchoice that's causing the issue.

Of course, I'm open to other ways of making \mylength have a different value depending on whether we're in inline or display math mode.

2 Answers 2

5

There are two problems with assigning a length inside \mathchoice:

  • assignments made inside \mathchoice are only valid within the scope of this \mathchoice unless they're \global;

  • \mathchoice actually typesets/runs all four of its arguments; which one should be shown in your document is only determined later. (Try incrementing a counter inside all four arguments of a \mathchoice, you'll be surprised!)

You thus can't use \mathchoice to make a variable dependent on the type of equation you are in. If you want to use \mathchoice to detect whether your symbol is used in \displaystyle, \textstyle or in a (nested) sub-/superscript you should put the drawing command in each of its four arguments (see e.g. the answers to this question for a detailed description of the command). You may also want to take a look at \mathpalette, which is often more convenient.


Alternative

If you're using amsmath (which you likely already are), you can use the conditional \if@display to test whether you are in a display equation or an inline equation. Since this command contains an @, you'll need to use \makeatletter.

Note, however, that this will still tell you you're in a display equation if you are, for instance, in a sub/-superscript within that equation.

Here's how it would work:

\documentclass{article}

\newlength{\mylength}
\setlength{\mylength}{0.4em}

\usepackage{amsmath}

\makeatletter %% <- make @ usable in command sequences
\newcommand{\autoscale}{%
    \if@display
        \setlength{\mylength}{0.4em}%
    \else
        \setlength{\mylength}{0.2em}%
    \fi
}
\makeatother %% <- revert @

\newcommand{\mycirc}{%
    \autoscale
    \begin{tikzpicture}
        \draw (0,0) circle (\mylength);
    \end{tikzpicture}%
}

\usepackage{tikz}

\begin{document}

A circle in a display equation:
\[
    x = \mycirc
\]

An inline one, which should be half the size: $y = \mycirc$.

\end{document}

output

2
  • Great, thank you. I had just worked out about all the arguments being expanded and was in the middle of updating my question. (I guess I'll leave it as it is now.) I'm very grateful for the alternative solution!
    – N. Virgo
    May 19, 2020 at 13:09
  • You're welcome. May 19, 2020 at 13:13
3

You can not make that work, each choice is a group and all four groups are always evaluated, in the same order so from within TeX the execution is exactly the same as

        \hbox{\setlength{\mylength}{0.4em}}%
        \hbox{\setlength{\mylength}{0.2em}}%
        \hbox{\setlength{\mylength}{0.2em}}%
        \hbox{\setlength{\mylength}{0.2em}}%

so even if you did global assignments you'd always get the same one. The choice of which box is used is in the mapping from a math list to a horizontal list but not accessible from TeX code.

4
  • Not directly, but it can use a \label / \ref mechanism to make it work in a multi-run way. May 20, 2020 at 0:47
  • @DonaldArseneau if you were really desperate:-) but usually you can keep track of things by hand to know the current style. (since you can't really use the infix \over form in latex) May 20, 2020 at 0:49
  • One important thing that escapes from \mathchoice is a math style assignment, so you can do \mathchoice{\scriptstyle}{\scriptstyle}{\scriptstyle}{\scriptscriptstyle}. I had thought about amsmath or LaTeX overloading those settings to do bookkeeping and know the current style, but the bookkeeping records will not get out. May 20, 2020 at 1:30
  • @DonaldArseneau hmm I guess I'll have to adjust my answer later: thanks for the comment:-) May 20, 2020 at 8:38

You must log in to answer this question.

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