4

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!

1

3 Answers 3

4

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}
1
  • This is very elegant! It does not seem to create problems elsewhere (in fact, it fixed a different slight problem which just came up).
    – mrtaurho
    Feb 28 at 16:32
4

Quick and dirty hack:

\documentclass{standalone}

\usepackage{tikz-cd,mathtools}

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

\begin{document}

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

\end{document}

enter image description here

4
  • Nice. (+1) Maybe better to put it into the macro? \newcommand{\CompB}[1]{\text{$\overbracket[.5pt][1pt]{#1}$}}
    – Sandy G
    Feb 28 at 16:26
  • @SandyG I don't think this would be a good idea in case the users also uses the macro elsewhere Feb 28 at 16:29
  • @samcarter_is_at_topanswers.xyz The macro is used throughout the document I am writing, not just in the diagram. Thanks for the solution (although this feels dirty :D).
    – mrtaurho
    Feb 28 at 16:30
  • 2
    @mrtaurho Oh, it does not only feel dirty, it is dirty :) Feb 28 at 16:32
3

Teach \CompB to preserve the current math style, using \mathpalette.

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz-cd}

\makeatletter
\NewDocumentCommand{\CompB}{m}{{% the inner braces are important
  \mathpalette\CompB@{#1}%
}}
\NewDocumentCommand{\CompB@}{mm}{\overbracket[0.4pt][1pt]{#1#2}}
\makeatletter

\begin{document}

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

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .