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}
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.