TikZ-CD Fontsize – Resolving Arrow Label Size Issues in TikZ-CD

fontsizemathtoolstikz-cd

I am using the mathtools package in combination with the tikz-cd package to decorate an arrow label with an overbracket. However, using the \overbracket within the arrow labeling messes up the automatic vertical-height adjustment of tikz-cd.

enter image description here

The above comes from the following minimal working example:

\documentclass{standalone}

\usepackage{tikz-cd,mathtools}

\newcommand{\CompB}[1]{\overbracket[.5pt][1pt]{#1}}

\begin{document}

\begin{tikzcd}
\CompB{M} \ar[rr,"{\CompB{f}}" near start]\ar[rr,"\overbracket{f}"]\ar[rr,"f" near end] && N
\\
& M \ar[ul,"b"]\ar[ur,swap,"f"] &
\end{tikzcd}

\end{document}

In included two labels for the upper arrow to highlight the problem (and to test whether the problem is my slight redefinition of the overbracket). I was not able to get something helpful out of the tikz-cd documentation, but I might have overlooked something.

How can I readjust the height of \overbracket{f} in the tikz-cd arrow label correctly? And why is this happening?

Thanks in advance!

Best Answer

The problem is that \overbracket puts the contents in a box, forgetting the style on the outside. So you need to capture the external math style and place it inside the box. There are several ways to do this, the easiest possibly being to use the scalerel package with its \ThisStyle and \SavedStyle commands.

enter image description here

\documentclass{standalone}

\usepackage{tikz-cd,mathtools,scalerel}

\newcommand{\CompB}[1]{\ThisStyle{\overbracket[.5pt][1pt]{\SavedStyle{#1}}}}

\begin{document}

\begin{tikzcd}
\CompB{M} \ar[rr,"{\CompB{f}}"] && N \\
& M \ar[ul,"b"]\ar[ur,swap,"f"] &
\end{tikzcd}

\end{document}