# How can I get an upright \hbar using unicode-math?

I would like to have an upright \hbar glyph in my documents and I'm using unicode-math. The example in https://tex.stackexchange.com/a/527104/218142 doesn't seem to work when I load unicode-math. The bar obviously moves relative to the h. Why does this happen, and how can I get the upright \hbar when loading unicode-math?

My MWE:

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

\documentclass{article}
\usepackage{unicode-math}

\begin{document}
\renewcommand*{\hbar}{{\mathpalette\hbaraux\relax\symup{h}}}
\newcommand*{\hbaraux}[2]{\sbox0{\mathsurround=0pt$#1\mathchar"AF$}\mkern-1mu\lower.07\ht0\box0\mkern-8mu}

$$\hbar \scriptstyle\hbar \scriptscriptstyle\hbar$$
\end{document}


This MWE produces the following output:

You don't need to construct things by hand, you want U+0127 ħ

\documentclass{article}
\usepackage{unicode-math}

\begin{document}
\renewcommand*{\hbar}{\mathrm{^^^^0127}}

$$\hbar \scriptstyle\hbar \scriptscriptstyle\hbar$$
\end{document}

• Blindingly simple. Aug 27 '21 at 19:44
• It doesn't work if I change \mathrm to \symup. Why is that? Aug 27 '21 at 19:47
• the latin modern math font hasn't got a glyph in slot 0127 you need to get it from the text font. @LaTeXereXeTaL Aug 27 '21 at 19:48

You can compose the character, but you have to do it the proper way:

\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-1mu\mathchar"AF$}}%
}
\makeatother

\begin{document}

$$\hbar \scriptstyle\hbar \scriptscriptstyle\hbar$$

\end{document}


• The box issue was confusing me. I still do not entirely understand it and need to study it more. I have never seen \m@th before. Where is it documented? Aug 27 '21 at 20:47
• @LaTeXereXeTaL \m@th is shorthand for \mathsurround=0pt. The idea here is to make a zero width box that protrudes left 1mu. Aug 27 '21 at 20:57