0

I tried to use tikz and https://tex.stackexchange.com/a/270007/161413 to color arrows with several colors. But I was also using mathtools, and found out that tikz and mathtools are not compatible as they both change the definition of :.

I applied the solution in https://tex.stackexchange.com/a/89470/161413 but still it does not work.

Here is my MWE (I am using 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}

When mathtools is used

When mathtools is commented

3

The tikz code is faulty. It uses \global\setlength. This relies on a side effect which breaks if the calc package is loaded. If you use \global\mylen instead it works:

\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}
2
  • Thank you very much! So the problem was not : but compatibility with calc package? – The Great Seo Jul 23 '20 at 14:05
  • it was wrong code. \global\setlength is faulty. It works only by luck in some cases. – Ulrike Fischer Jul 23 '20 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.