10

The commath package use the TeX command \ifinner to select between inline math mode and display math mode. Because of this, it fail to work properly with some display math environments.

In the breqn package, we find:

The test \ifinner is unreliable for distinguishing whether we are in a displayed formula or an inline formula: (...) So we provide a more reliable test. But it might have been provided already by the amsmath package.

The more reliable test should be \if@display but only replace \ifinner with \if@display in commath.sty not work. Any suggestion?

jub0bs
  • 58,916

1 Answers1

27

Let's see how commath should define its macros.

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{amsmath}

%%% Here start the definitions
\newcommand{\dif}{\mathop{}\!\mathrm{d}}
\newcommand{\Dif}{\mathop{}\!\mathrm{D}}

\makeatletter
\newcommand{\spx}[1]{%
  \if\relax\detokenize{#1}\relax
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {^{#1}}%
}
\makeatother

\newcommand\pd[3][]{\frac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
\newcommand\tpd[3][]{\tfrac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}
\newcommand\dpd[3][]{\dfrac{\partial\spx{#1}#2}{\partial#3\spx{#1}}}

\newcommand{\md}[6]{\frac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}
\newcommand{\tmd}[6]{\tfrac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}
\newcommand{\dmd}[6]{\dfrac{\partial\spx{#2}#1}{\partial#3\spx{#4}\partial#5\spx{#6}}}

\newcommand{\od}[3][]{\frac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}
\newcommand{\tod}[3][]{\tfrac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}
\newcommand{\dod}[3][]{\dfrac{\dif\spx{#1}#2}{\dif#3\spx{#1}}}

\newcommand{\genericdel}[4]{%
  \ifcase#3\relax
  \ifx#1.\else#1\fi#4\ifx#2.\else#2\fi\or
  \bigl#1#4\bigr#2\or
  \Bigl#1#4\Bigr#2\or
  \biggl#1#4\biggr#2\or
  \Biggl#1#4\Biggr#2\else
  \left#1#4\right#2\fi
}
\newcommand{\del}[2][-1]{\genericdel(){#1}{#2}}
\newcommand{\set}[2][-1]{\genericdel\{\}{#1}{#2}}
\let\cbr\set
\newcommand{\sbr}[2][-1]{\genericdel[]{#1}{#2}}
\let\intoo\del
\let\intcc\sbr
\newcommand{\intoc}[2][-1]{\genericdel(]{#1}{#2}}
\newcommand{\intco}[2][-1]{\genericdel[){#1}{#2}}
\newcommand{\eval}[2][-1]{\genericdel.|{#1}{#2}}
\newcommand{\envert}[2][-1]{\genericdel||{#1}{#2}}
\let\abs\envert
\newcommand{\sVert}[1][0]{%
  \ifcase#1\relax
  \rvert\or\bigr|\or\Bigr|\or\biggr|\or\Biggr
  \fi
}
\newcommand{\enVert}[2][-1]{\genericdel\|\|{#1}{#2}}
\let\norm\enVert
\newcommand{\fullfunction}[5]{%
  \begin{array}{@{}r@{}r@{}c@{}l@{}}
  #1 \colon & #2 & {}\longrightarrow{} & #3 \\
            & #4 & {}\longmapsto{}     & #5
  \end{array}
}
%%% end of the definitions

\linespread{2} % just for this test file

\begin{document}

$f(x)\dif x\quad f(x)\Dif x$

$\pd{f}{x}\displaystyle\pd[2]{f}{x}$

$\tpd{f}{x}\displaystyle\tpd[2]{f}{x}$

$\dpd{f}{x}\displaystyle\dpd[2]{f}{x}$

$\od{f}{x}\displaystyle\od[2]{f}{x}$

$\tod{f}{x}\displaystyle\tod[2]{f}{x}$

$\dod{f}{x}\displaystyle\dod[2]{f}{x}$

$\md{f}{5}{x}{2}{y}{3}\displaystyle\md{f}{5}{x}{2}{y}{3}$

$\tmd{f}{5}{x}{2}{y}{3}\displaystyle\tmd{f}{5}{x}{2}{y}{3}$

$\dmd{f}{5}{x}{2}{y}{3}\displaystyle\dmd{f}{5}{x}{2}{y}{3}$

$\del{\dfrac{1}{2}}\del[0]{x}\del[1]{x}\del[2]{x}\del[3]{x}\del[4]{x}$

$\sbr{\dfrac{1}{2}}\sbr[0]{x}\sbr[1]{x}\sbr[2]{x}\sbr[3]{x}\sbr[4]{x}$

$\set{\dfrac{1}{2}}\set[0]{x}\set[1]{x}\set[2]{x}\set[3]{x}\set[4]{x}$

$\intoo{a,b}\intoo[0]{a,b}\intoo[1]{a,b}\intoo[2]{a,b}\intoo[3]{a,b}\intoo[4]{a,b}$

$\intcc{a,b}\intcc[0]{a,b}\intcc[1]{a,b}\intcc[2]{a,b}\intcc[3]{a,b}\intcc[4]{a,b}$

$\intoc{a,b}\intoc[0]{a,b}\intoc[1]{a,b}\intoc[2]{a,b}\intoc[3]{a,b}\intoc[4]{a,b}$

$\intco{a,b}\intco[0]{a,b}\intco[1]{a,b}\intco[2]{a,b}\intco[3]{a,b}\intco[4]{a,b}$

$
\eval{f(x)}_a^b
\eval[0]{f(x)}_a^b
\eval[1]{f(x)}_a^b
\eval[2]{f(x)}_a^b
\eval[3]{f(x)}_a^b
\eval[4]{f(x)}_a^b
$

$\abs{x}\abs[0]{x}\abs[1]{x}\abs[2]{x}\abs[3]{x}\abs[4]{x}$

$\norm{x}\norm[0]{x}\norm[1]{x}\norm[2]{x}\norm[3]{x}\norm[4]{x}$

\linespread{1}\selectfont % return to normal

$\fullfunction{f}{A}{B}{x}{y}$
\end{document}

I have omitted the \...ref macros that are better managed with cleveref.

As you see, it's only a collection of dubiously useful macros.

Main errors in commath: the definitions of \dif and \Dif are plainly wrong. The usage of \ifinner is completely wrong; what the author intends to do by \ifinner with \tfrac and \dfrac is already done (better) by the standard \frac macro.

The "delimiters" macros are wrong in that they use by default \left and \right, which is disputable; I've left them as in the original, but defining them in terms of a generic macro.

enter image description here

egreg
  • 1,121,712
  • Looking at the rendering of \pd{f}{x} or \od{f}{x}, there seems to be a bigger horizontal space between \partial and f in the numerator, than there is between \partial and x in the denominator. Is that due to the "empty" superscript? – Michael May 05 '15 at 08:49
  • @Michael I added the check that the argument is empty – egreg May 05 '15 at 11:10
  • I'm supposed to avoid such comments, but anyway thanks. – Michael May 05 '15 at 11:20
  • There's a problem in the \eval (and in general, when someone uses . in \genericdel) in the “base” height. There should be a check to see if the #1 or #2 are equal to ., and, in that case, gobble it. – Manuel May 05 '15 at 12:26
  • I think this should be released on CTAN in replacement of commath. – Heliton Martins May 30 '21 at 14:53
  • 1
    @HélitonMartins Actually, I'm not keen on packages that are just collections of macros, which are frequently just a cause of name conflicts. – egreg May 30 '21 at 14:55
  • @egreg Yeah, it makes sense. But, as a user, I usually prefer \usepackage{anything} than copy-pasting the same macros everywhere. In that respect, commath makes sense to me. Anyway, I'm gonna replace commath.sty code on my machine with yours, so thanks for that! – Heliton Martins May 30 '21 at 15:18