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

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:

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

• On the 'd', the bars are all the same length, independent of the font size. Aug 29, 2021 at 21:59
• for h the bar is placed first in a zero width box sticking out on teh right but for d you want to place the bar after in a zero width box sticking out on the left, Aug 29, 2021 at 22:08
• or as for h bar, you could use U+0111 đ Aug 29, 2021 at 22:10
• @DavidCarlisle I knew about that, but in this case I really want a sans d and a bold sans variant (I didn't include those in the MWE). Aug 29, 2021 at 22:13
• bold and sans like this? \documentclass{article}\begin{document}\Large^^^^0111 \textsf{^^^^0111} \textsf{\textbf{^^^^0111}} \textbf{^^^^0111}\end{document} Aug 29, 2021 at 22:16

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}


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}


Here is a way with stacks.

\documentclass{article}
\usepackage{unicode-math}
\usepackage{scalerel,stackengine}
\newcommand\dbar{\ThisStyle{\ensurestackMath{%
\stackengine{-.7\LMpt}{\SavedStyle\mathsf{d}}{\SavedStyle\bar{}%
\mkern2.25mu}{O}{r}{F}{F}{L}}}}
\begin{document}
$$\dbar \scriptstyle\dbar \scriptscriptstyle\dbar$$
\end{document}


Changing the \mathsf to, for example, \mathit, results in

The horizontal placement of the bar can be changed by tweaking the 2.25mu. The vertical placement by adjusting the -.7\LMpt.