1

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?

2
  • 2
    Possible duplicate: What is \mathchoice?
    – Werner
    Sep 11 '18 at 18:24
  • 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
3

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} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.