TransWikia.com

Mathtools and Tikz are incompatible because of colon

TeX - LaTeX Asked on November 26, 2021

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{
    ifnummathcode`:=string"8000
    begingrouplccode`~=`:
    lowercase{endgroupletmath@colon@meaning~}
    else
    expandafterletexpandaftermath@colon@meaningstring:
    fi
}
AtBeginDocument{
    ifnumcatcode`:=active
    letcstext@colon@meaning{active@charstring:}
    else
    expandafterletexpandaftertext@colon@meaningstring:
    fi
    protecteddeftikz@nonactivecolon{%
        ifmmode
        expandaftermath@colon@meaning
        else
        expandaftertext@colon@meaning
        fi} 
    begingrouplccode`~=`:
    lowercase{endgrouplet~tikz@nonactivecolon}
}
makeatother


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

usetikzlibrary{decorations.markings}

newlengthmylen

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] {globalsetlengthmylen{pgfdecoratedpathlength}};
            },
        },
        draw=#1,
        dash pattern=on 0.5mylen off 0.5mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5mylen off 0.5mylen,dash phase=0.5mylen
        },
    }
}



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

One Answer

The tikz code is faulty. It uses globalsetlength. This relies on a side effect which breaks if the calc package is loaded. If you use globalmylen 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}

newlengthmylen

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] {globalmylenpgfdecoratedpathlength};
            },
        },
        draw=#1,
        dash pattern=on 0.5mylen off 0.5mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5mylen off 0.5mylen,dash phase=0.5mylen
        },
    }
}



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}

Answered by Ulrike Fischer on November 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP