Mathtools 和 Tikz 不兼容,因为冒号

Mathtools 和 Tikz 不兼容,因为冒号

我尝试使用 tikz 和https://tex.stackexchange.com/a/270007/161413用几种颜色给箭头上色。但我也使用 mathtools,发现 tikz 和 mathtools 不兼容,因为它们都改变了 的定义:

我应用了解决方案https://tex.stackexchange.com/a/89470/161413但它仍然不起作用。

这是我的 MWE(我使用 XeLaTeX):

\documentclass{article}

\usepackage[
a4paper,
margin=30mm
]{geometry}

\usepackage{amsmath, amssymb, amsthm}

\usepackage{tikz}


\usepackage{mathtools} % commenting this line works

\usepackage{etoolbox}

\makeatletter
\AtEndPreamble{
    \ifnum\mathcode`\:=\string"8000
    \begingroup\lccode`\~=`\:
    \lowercase{\endgroup\let\math@colon@meaning~}
    \else
    \expandafter\let\expandafter\math@colon@meaning\string:
    \fi
}
\AtBeginDocument{
    \ifnum\catcode`\:=\active
    \letcs\text@colon@meaning{active@char\string:}
    \else
    \expandafter\let\expandafter\text@colon@meaning\string:
    \fi
    \protected\def\tikz@nonactivecolon{%
        \ifmmode
        \expandafter\math@colon@meaning
        \else
        \expandafter\text@colon@meaning
        \fi} 
    \begingroup\lccode`\~=`\:
    \lowercase{\endgroup\let~\tikz@nonactivecolon}
}
\makeatother


\usetikzlibrary{graphs}
\usetikzlibrary{positioning}
\usetikzlibrary{intersections}

\usetikzlibrary{decorations.markings}

\newlength\mylen

\tikzset{
    bicolor/.style n args={2}{
        decoration={
            markings,
            mark=at position 0.5 with {
                \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\setlength\mylen{\pgfdecoratedpathlength}};
            },
        },
        draw=#1,
        dash pattern=on 0.5\mylen off 0.5\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}



\begin{document}

\begin{tikzpicture}
\node (bbb) at (0,0) {bbb};
\node (baa) at (150:3) {baa};

\draw[->, bicolor={cyan}{red!80!black}]
  (baa) to[out=-80,in=150] (bbb);
\end{tikzpicture}

\end{document}

当使用 mathtools 时

当 mathtools 被注释时

答案1

tikz 代码有缺陷。它使用了\global\setlength。这依赖于一个副作用,如果calc加载包,该副作用就会中断。如果你改用\global\mylen它,它会起作用:

\documentclass{article}

\usepackage[
a4paper,
margin=30mm
]{geometry}

\usepackage{amsmath, amssymb, amsthm}
\usepackage{tikz}


%

\usepackage{etoolbox}

\usepackage{mathtools}


\usetikzlibrary{graphs}
\usetikzlibrary{positioning}
\usetikzlibrary{intersections}

\usetikzlibrary{decorations.markings}

\newlength\mylen

\tikzset{
    bicolor/.style n args={2}{
        decoration={
            markings,
            mark=at position 0.5 with {
                \node[draw=none,inner sep=0pt,fill=none,text width=0pt,minimum size=0pt] {\global\mylen\pgfdecoratedpathlength};
            },
        },
        draw=#1,
        dash pattern=on 0.5\mylen off 0.5\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}



\begin{document}

\begin{tikzpicture}
\node (bbb) at (0,0) {bbb};
\node (baa) at (150:3) {baa};
\tracingmacros=1
\draw[->, bicolor={cyan}{red!80!black}]
  (baa) to[out=-80,in=150] (bbb);
\end{tikzpicture}

\end{document}

相关内容