6

I need to typeset the derivative of l by z_a. Normally I would do \frac{\diff l} {\diff z_a} with \newcommand{\diff} {\mathop{}\!\mathrm{d}}.

This gives:

enter image description here

which does not look very good imho due to the alignment.

Following what one answer here suggests, I tried \frac{\diff l\hphantom{_a}} {\diff z_a}, which gives:

enter image description here

Now this looks good enough for me, but the whole thing got me wondering what the correct way to typeset derivatives is. Are there any general rules concerning the horizontal alignment? Is it maybe possible to align the d's? I guess that would probably look best and it could be used universally without having to look at every single derivative you typeset.

  • 2
    \frac{\diff l\hfill} {\diff z_a} for left alignment without the phantoms. In general \newcommand\lfrac[2]{\frac{#1\hfill}{#2\hfill}} would work. – Steven B. Segletes Jun 3 '16 at 15:40
  • 3
    It's a matter of opinion, I'm afraid; I'd go for centering in any case. – egreg Jun 3 '16 at 15:41
  • @egreg after playing around a bit I guess you are right in general. But I still think that there are cases where aligned d's look best :-). But then we are at the point again where we have to look at every single derivative we typeset... – user35915 Jun 3 '16 at 16:54
8

As egreg pointed out, the setting of a derivative fraction is a matter of opinion, and left-aligned is not to his taste.

However, if it is still of interest to you, I shared the definition

\newcommand\lfrac[2]{\frac{#1\hfill}{#2\hfill}}

that would produce a left-aligned fraction, without having to manually tweak each use case. For derivatives in particular, the \lfrac can be used in its own macro,

\newcommand\Diff[3][]{\lfrac{\diff^{#1}#2}{\diff\ifx\diff#3\else#3\fi^{#1}}}

What \Diff does is allow two syntaxes to obtain the setting of a derivative, either \Diff{y}{x} or alternately \Diff{y}{\diff x}. The macro determines whether \diff has been specified at the front of the denominator and strips it if necessary.

EDITED to add optional argument to \Diff to allow higher-order derivative setting.

\documentclass{article}
\newcommand{\diff} {\mathop{}\!\mathrm{d}}
\newcommand\lfrac[2]{\frac{#1\hfill}{#2\hfill}}
\newcommand\Diff[3][]{\lfrac{\diff^{#1}#2}{\diff\ifx\diff#3\else#3\fi^{#1}}}
\begin{document}
\[
\frac{\diff l} {\diff z_a}\cdot\frac{\diff Q_\mathrm{ref}}{\diff x}
\textrm{~for~comparison}
\]
\[
\lfrac{\diff l} {\diff z_a}\cdot\lfrac{\diff Q_\mathrm{ref}}{\diff x}
\textrm{~with \textbackslash lfrac}
\]
\[
\Diff{l}{z_a}     \cdot     \Diff{Q_\mathrm{ref}}{\diff x}
\textrm{~with \textbackslash Diff (2 syntaxes)}
\]
\[
\Diff[2]{l}{z_a}     \cdot     \Diff[3]{Q_\mathrm{ref}}{\diff x}
\textrm{~with \textbackslash Diff (and opt. argument)}
\]
\end{document}

enter image description here

Of course, if one likes the syntax of the \Diff macro, but not the left-alignment, it can be changed back to center-alignment by changing the \lfrac to \frac in the definition of \Diff.

  • Thanks, very good! But I don't fully understand how the checking for a duplicate \diff works. Doesn't \ifx\diff#3 compare \diff to argument number three? Or does it only take the first part of the argument and uses the second part as output if the comparison yields true? In that case it would work, I guess. Sorry, I'm not very familiar with latex conditionals... – user35915 Jun 3 '16 at 16:42
  • @user35915 \ifx\diff#3 compares \diff to the unexpanded first token of argument #3 (note that argument 3 is the denominator, even if an optional argument is not specified). If the test fails, the \else code is executed. But if the test succeeds, the first token of #3, the \diff, was absorbed in the comparison test, leaving the rest of the #3 tokens to be executed in case of success. It effectively strips off the leading \diff. – Steven B. Segletes Jun 3 '16 at 16:46
  • @user35915 In the case of the \else code, the complete #3 is regurgitated. However, the else code is only executed if there was no leading \diff, and thus one would want the complete #3 to be executed, in that case. – Steven B. Segletes Jun 3 '16 at 16:49

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.