14

The command \tikzcdset{arrow style=math font} seems to break the rendering of arrows with the package tikz-cd, whatever math font is chosen: enter image description here

\documentclass{article}
\usepackage{unicode-math}
\usepackage{tikz-cd}
\tikzcdset{arrow style=math font}

\begin{document}
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}

    \setmathfont{Libertinus Math}
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}

    \setmathfont{Latin Modern Math}
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}

    \setmathfont{STIX Two Math}
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}
\end{document}

What can be done about this?

If this is not fixable, I would also be interested in other ways to make the arrow of tikz-cd match the math font.

2 Answers2

10

tikz-cd assumes two things when drawing the arrows:

  1. That the line width of the arrow is the same as the width of a fraction rule, and
  2. that the arrow goes along the math axis.

Both is not true for every math font. Imho it is not possible from inside luatex to get the correct values. You would have to look at the glyph definition with e.g. font forge. The following is not perfect but shows how one can adjust the values:

\documentclass{article}
\usepackage{unicode-math}
\usepackage{tikz-cd}
\tikzcdset{arrow style=math font}

\begin{document}

    \setmathfont{Libertinus Math}

    $\frac{1}{b}\rightarrow xxx$

    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}

\tikzcdset{every arrow/.append style={line width=0.52pt}}%smaller    
\pgfmathdeclarefunction*{axis_height}{0}{%
    \begingroup%      
      \pgfmathreturn2.65pt % smaller than the original
    \endgroup}% 
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}


\end{document}

enter image description here

red_trumpet
  • 995
  • 1
  • 6
  • 17
Ulrike Fischer
  • 327,261
  • Thank you very much. I also tried down arrows, they also look very good now. Could you maybe add, which the values from the glyph definition are needed, and how they must be used? –  Mar 16 '18 at 11:27
  • 1
    You need the line width and the position of the line relative to the baseline. Btw: The values naturally depend on the font size. So the code above is not meant as usable directly, only as proof of concept. – Ulrike Fischer Mar 16 '18 at 12:06
  • 1
    @UlrikeFischer I really appreciated your answer. +1 – Sebastiano Mar 17 '18 at 00:00
  • I just realized that this might be one of the situations where using em is better than pt, making the solution independent of global font size. – red_trumpet Mar 26 '24 at 14:17
7

Based on the answer by Ulrike Fischer I wrote a small package which stores the values for a few commonly used fonts, namely: Libertinus Math, TeX Gyre Pagella Math, TeX Gyre Schola Math, TeX Gyre Bonum Math, STIX Two Math, Cambria Math. I thought I would share them here in case someone encounters the same problem. Maybe I will also updated the answer in case I add more fonts.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mycd}

\RequirePackage{tikz-cd}

\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{tikz-cd}}

\newcommand{\tikzcdarrowdimens}[2]{%
    \tikzcdset{every arrow/.append style={line width=#1}}%
    \pgfmathdeclarefunction*{axis_height}{0}{%
        \begingroup%      
        \pgfmathreturn#2%
        \endgroup%
    }%
}

\DeclareOption{Libertinus}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.052em}{0.250em}%
}

\DeclareOption{Pagella}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.060em}{0.250em}%
}

\DeclareOption{Schola}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.070em}{0.260em}%
}

\DeclareOption{Bonum}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.072em}{0.260em}%
}

\DeclareOption{STIX}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.068em}{0.259em}%
}

\DeclareOption{Cambria}{%
    \tikzcdset{arrow style=math font}%
    \tikzcdarrowdimens{0.06494140625em}{0.285888671875em}%
}

\ProcessOptions\relax

Basic usage is as follows:

\documentclass{article}

\usepackage{unicode-math}
\setmathfont{Libertinus Math}

\usepackage[Libertinus]{mycd}

\begin{document}
    \begin{tikzcd}
        a\arrow[r]&b
    \end{tikzcd}
\end{document}