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?

2
  • 3
    Possible duplicate: What is \mathchoice?
    – Werner
    Commented Sep 11, 2018 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
    Commented Sep 11, 2018 at 19:28

1 Answer 1

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} 

You must log in to answer this question.

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