5

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:

MWE output showing the bar doesn't stay put relative to the h

David Carlisle
  • 757,742

2 Answers2

8

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

enter image description here

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

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

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

David Carlisle
  • 757,742
3

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}

enter image description here

egreg
  • 1,121,712
  • 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? – LaTeXereXeTaL Aug 27 '21 at 20:47
  • 1
    @LaTeXereXeTaL \m@th is shorthand for \mathsurround=0pt. The idea here is to make a zero width box that protrudes left 1mu. – egreg Aug 27 '21 at 20:57