Why does making a d with a bar through it not behave like an h with a bar through it

math-fontsmath-modeunicodeunicode-math

This is a follow-up to my previous question How can I get an upright \hbar using unicode-math?. I am experimenting with making a d with a bar through it like one would use in physics to denote an inexact differential (see also How to enter symbol for inexact differential in xelatex? and How can I call unicode characters in LaTeX or LyX? and d with a little line through the top of it). I see the bar on the d glyph seems to shift relative to the letter whereas I don't see that behavior with the h character. Is this behavior real and, if so, what causes it? Does it vary with different fonts? Can it be overcome?

Experimenting with the values of \mkern did not make the shift go away.

Here is my MWE:

% !TEX program = lualatexmk
% !TEX encoding = UTF-8 Unicode

\documentclass{article}
\usepackage{unicode-math}
\makeatletter
\AtBeginDocument{%
  \DeclareRobustCommand{\hbar}{{\mathpalette\hbar@\relax\symup{h}}}%
}
\newcommand*{\hbar@}[2]{%
  \makebox[0pt][l]{\raisebox{-0.07\height}{\(\m@th#1\mkern-2mu\mathchar"AF\)}}%
}
\makeatother
% Let's try a \dbar glyph for inexact differentials.
\makeatletter
\AtBeginDocument{%
  \DeclareRobustCommand{\dbar}{{\mathpalette\dbar@{\relax}{\symsfup{d}}}}%
}
\newcommand*{\dbar@}[2]{%
  \makebox[0pt][l]{\raisebox{-0.09\height}{\(\m@th#1\mkern+3mu\mathchar"AF\)}}%
}
\makeatother

\begin{document}
\( \hbar \scriptstyle\hbar \scriptscriptstyle\hbar \)

\( \dbar \scriptstyle\dbar \scriptscriptstyle\dbar \)
\end{document}

Here is the MWE output:

Output showing the difference between hbar and dbar

UPDATE: After much experimentation, the following seems to work but I can't completely explain why. If I change [c] to [r] I notice that the bar doesn't move regardless of what I provide for \mkern.

\makeatletter
\AtBeginDocument{%
  \DeclareRobustCommand{\dbar}{{\symsfup{d}\mathpalette\dbar@\relax\relax}}%
}
\newcommand*{\dbar@}[2]{%
  \makebox[0pt][c]{\raisebox{-0.07\height}{\(\m@th#1\mkern-6mu\mathchar"AF\)}}%
}
\makeatother

Best Answer

If you are using unicode-math, your best option is to insert đ (U+0111, Latin small letter d with stroke) from a text font that supports it. If your main font does not, you can use \setmathrm or \setmathsf to change your \mathrm or \mathsf alphabet to one that does, or define a new math alphabet that does with \setmathfontface.

\documentclass{article}
\tracinglostchars=3
\usepackage{newcomputermodern} % Or your font package of choice.

\newcommand\dbar{\mathsf{đ}}
\newcommand\vardbar{\text{\sffamily\ulcshape\upshape đ}}
\newcommand\mitdbar{\text{\sffamily\ulcshape\slshape đ}}

\begin{document}
\[ \dbar \vardbar \mitdbar
\]
\end{document}

New Computer Modern Sample

The \text commands I use are somewhat complicated, because they leave the weight unchanged but set every other axis, but you can probably get away with \textnormal{đ}

If you are compiling with legacy TeX instead, you can technically use the same approach there too, because the 8-bit T3 encoding supports this character. You can select a font family (Computer Modern Roman, Computer Modern Sans Serif, Times, Helvetica, Junicode or Linguistics Pro), and some of these support slanted shapes.

\documentclass{article}
\tracinglostchars=3
\usepackage[T3,T1]{fontenc}
\usepackage[utf8]{inputenc} % The default since 2018
\usepackage{amsmath}

\DeclareTextSymbol{\textdbar}{T3}{"A1}
\DeclareTextSymbolDefault{\textdbar}{T3}
\newcommand\dbar{\textnormal{\textdbar}}
\newcommand\vardbar{\textnormal{\fontfamily{cmss}\selectfont\textdbar}}
\newcommand\mitdbar{\textnormal{\fontfamily{cmss}\slshape\selectfont\textdbar}}

\begin{document}
\[ \dbar\vardbar\mitdbar
\]
\end{document}

Tipa sample

Related Question