3

I'm trying to write a macro that returns different things depending on it if is used in a displayed equation or if it is used in an inline equation. Something like this, I assume:

\newcommand{\foo}{%
    \ifdisplaystyle
        This equation is displayed!
    \else
        This equation is inline!
    \fi}

I assume there's a way to do this, as $\sum$ and $$\sum$$ look different. How is it done?

Pi Fisher
  • 559
  • 3
  • 11
  • 3
  • 1
    I think the questions are certainly similar. If your answer had simply been "go read this question", that would have been enough. I'm not very familiar with StackExchange's opinion on how similar things should be to be marked as duplicate, so maybe it's correct to do so.

    I think my question has the advantage of being asked in a way that someone might be able to find it without knowing the solution (figure out how \mathchoice works and then use it).

    – Pi Fisher Sep 11 '18 at 19:28

1 Answers1

6

You can use \mathchoice in the following way to condition:

\mathchoice
  {<display style>}
  {<text style>}
  {<script style>}
  {<script-script style>}

to discern between what is displayed within the respective styles. Here's an example:

enter image description here

\documentclass{article}

\newcommand{\foo}{{%
  \mathchoice
    {D} % \displaystyle
    {T} % \textstyle
    {S} % \scriptstyle
    {s} % \scriptscriptstyle
}}

\begin{document}

See $\foo^{\foo^{\foo}}$. Also see
\[
  \foo^{\foo^{\foo}}
  \quad \mbox{and} \quad
  \textstyle \foo_{\foo_{\foo}}
\]

\end{document} 
Werner
  • 603,163