2

I wanted to define some partial derivative commands that would be \partial y/\partial x if it was inside an inline equation and would use \frac{\partial y}{\partial x} if inside an equation environment. I searched around and came up with (honestly, probably from TeX.SE but I don't remember which question/answer it was):

\makeatletter
\newcommand{\pd}[2]{%
\def\@tempa{document}
\ifx\@tempa\@currenvir \partial {#1}/\partial{#2} \else
\def\@tempa{equation}
\ifx\@tempa\@currenvir \frac{\partial {#1}}{\partial {#2}}\else
\fi\fi
}

which was awesome! But then I tried using it inside:

\begin{equation}
   \begin{aligned}
      f &= \pd{y}{x} \\
      g &= \pd{y}{z}
   \end{aligned}
 \end{equation}

and it didn't render correctly because the current environment was aligned and not equation. I could add aligned to my if statement but then I'll need to add branches for all the varieties of *matrix and who knows what else. I could add a default so that if it's not in document it always renders with the \frac{}{} form.

But all of that just doesn't seem like the right way to go. Is there a way to see if the command is issued somewhere inside of an equation environment, even if it is somewhere up the environment tree?

7

The use of \mathchoice{displaystyle}{textstyle}{scriptstyle}{scriptscriptstyle} is the canonical way of providing different math answers as a function of the current math style (regardless of what environment name is invoking the math).

\documentclass{article}
\usepackage{amsmath}
\newcommand\pd[2]{\mathchoice%
  {\frac{\partial #1}{\partial #2}}%
  {\partial #1/\partial #2}%
  {\partial #1/\partial #2}%
  {\partial #1/\partial #2}%
}
\begin{document}
Inline it looks like $f =\pd{y}{x} $
\begin{equation}
   \begin{aligned}
      f &= \pd{y}{x} \\
      g &= \pd{y}{z}
   \end{aligned}
\end{equation}

\end{document}

enter image description here

  • I knew I wasn't doing it in a good way and yet hours of searching never turned this approach up. One day, maybe I'll finally figure out what magic words to use to find answers to LaTeX questions :/ – tpg2114 Mar 16 '15 at 2:50
  • @tpg2114 The basic vocabulary of magic words are 300 TeX primitives. The TeX primitive \matchoice was used here. If you know this vocabulary then you know the answer to potentially all problems and as a bonus: you don't need to use LaTeX, TeX is more simple. – wipet Mar 16 '15 at 8:22
  • Why \makeatletter? – summer Mar 16 '15 at 9:33
  • @summer It was vestigial from a prior edit/copy/paste. It has been removed. Thanks for mentioning. – Steven B. Segletes Mar 16 '15 at 9:57
  • @tpg2114 I recall when \mathchoice was a revelation to me, as well. David's answer at tex.stackexchange.com/questions/43978/proper-use-of-mathchoice is excellent reading. My answer at that same question is also further developed at tex.stackexchange.com/questions/78872/… – Steven B. Segletes Mar 16 '15 at 10:08

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.