4

I want to make some labels (defined as a diamond) on top of some edges, see the following figure:

correct figure

I am new to Tikz and use the following latex code:

\documentclass[border=10pt]{standalone}


\usepackage{tikz}
\usepackage{verbatim}

\usetikzlibrary{decorations.markings}
\usetikzlibrary{positioning,shapes.geometric} 

\begin{document}
\pagestyle{empty}

\definecolor{vertexcol}{RGB}{250,250,250}\definecolor{col0}{RGB}{0,0,204}\definecolor{col1}{RGB}{204,0,0}\definecolor{col2}{RGB}{0,204,0}\definecolor{col3}{RGB}{255, 165, 0}\definecolor{col4}{RGB}{128,0,128}\definecolor{col5}{RGB}{255, 255, 0}\definecolor{fontcolor}{RGB}{0,0,0}

\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 1.0\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}
\tikzset{
    bicolor_neg/.style n args={2}{
        decoration={
            markings,
            mark=at position 0.5 with {
                \node[diamond, draw,minimum width=10pt]{\global\setlength\mylen{\pgfdecoratedpathlength}};
            },
        },
        draw=#1,
        dash pattern=on 0.5\mylen off 1.0\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}

\begin{tikzpicture}
    \tikzstyle{vertex}=[circle, draw=black, ultra thick ,fill=vertexcol!80,minimum size=15pt]\textbf{}
    
    \node[vertex] (0) at (3.0,0.0) {\color{fontcolor}0};
    \node[vertex] (1) at (1.5000000000000004,2.598076211353316) {\color{fontcolor}1};
    \node[vertex] (2) at (-1.4999999999999993,2.5980762113533165) {\color{fontcolor}2};
    \node[vertex] (3) at (-3.0,3.6739403974420594e-16) {\color{fontcolor}3};
    \node[vertex] (4) at (-1.5000000000000013,-2.598076211353315) {\color{fontcolor}4};
    \node[vertex] (5) at (1.5,-2.598076211353316) {\color{fontcolor}5};
    \path (0) edge[line width=4.0,bicolor={col1}{col0}, bend right=-11.25, opacity=1.0] (5);
    \path (0) edge[line width=4.0,bicolor_neg={col1}{col1}, bend right=11.25, opacity=1.0] (5);
    \path (1) edge[line width=4.0,bicolor_neg={col1}{col1}, bend right=0.0, opacity=1.0] (3);
    \path (2) edge[line width=4.0,bicolor_neg={col2}{col1}, bend right=11.25, opacity=1.0] (4);
    \path (2) edge[line width=4.0,bicolor={col0}{col3}, bend right=-11.25, opacity=1.0] (5);
    
\end{tikzpicture}

\end{document}


The produced figure is not I want as the labels (diamonds) are under the edges. I also play with some properties (e.g.,"below',"above"...) in the tikzset but all failed.

wrong figure

Do you know how to make the labels on top of the edges automatically? Thank you in advance!

New contributor
Xuemei is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 2
    tikzstyle is deprecated, and should never be used. Sep 21 at 11:58

2 Answers 2

7

It seem that your code is generated from some program and is therefore very convoluted and hard to read. Here is an alternative way:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.markings, shapes.geometric} 
\begin{document}
\tikzset{
bicolor/.style 2 args={
postaction={draw=#1, decoration={curveto, post=moveto, post length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={draw=#2, decoration={curveto, pre=moveto, pre length=0.5*\pgfmetadecoratedpathlength}, decorate},
},
bicolor_neg/.style 2 args={
postaction={draw=#1, decoration={curveto, pre=moveto, pre length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={draw=#2, decoration={curveto, post=moveto, post length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={decoration={markings, mark=at position 0.5 with {\node[diamond, draw, thin, minimum width=10pt] {};}}, decorate},
},
vertex/.style={circle, draw, ultra thick, minimum size=15pt},
}
\begin{tikzpicture}[line width=4pt]
\node[vertex] (0) at (3.0,0.0) {0};
\node[vertex] (1) at (1.5,2.6) {1};
\node[vertex] (2) at (-1.5,2.6) {2};
\node[vertex] (3) at (-3.0,0.0) {3};
\node[vertex] (4) at (-1.5,-2.6) {4};
\node[vertex] (5) at (1.5,-2.6) {5};
\path[bicolor={red}{blue}] (0) to[bend right=-11.25] (5);
\path[bicolor_neg={red}{red}] (0) to[bend right=11.25] (5);
\path[bicolor_neg={red}{red}] (1) to (3);
\path[bicolor_neg={green}{red}] (2) to[bend right=11.25] (4);
\path[bicolor={blue}{orange}] (2) to[bend right=-11.25] (5);
\end{tikzpicture}
\end{document}

Automata diagram with dual color lines

Edit: The same code with opacity=0.5 on one edge

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{decorations.markings, shapes.geometric} 
\begin{document}
\tikzset{
bicolor/.style 2 args={
postaction={draw=#1, decoration={curveto, post=moveto, post length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={draw=#2, decoration={curveto, pre=moveto, pre length=0.5*\pgfmetadecoratedpathlength}, decorate},
},
bicolor_neg/.style 2 args={
postaction={draw=#1, decoration={curveto, pre=moveto, pre length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={draw=#2, decoration={curveto, post=moveto, post length=0.5*\pgfmetadecoratedpathlength}, decorate},
postaction={decoration={markings, mark=at position 0.5 with {\node[diamond, draw, thin, minimum width=10pt, opacity=1] {};}}, decorate}, %new
},
vertex/.style={circle, draw, ultra thick, minimum size=15pt},
}
\begin{tikzpicture}[line width=4pt]
\node[vertex] (0) at (3.0,0.0) {0};
\node[vertex] (1) at (1.5,2.6) {1};
\node[vertex] (2) at (-1.5,2.6) {2};
\node[vertex] (3) at (-3.0,0.0) {3};
\node[vertex] (4) at (-1.5,-2.6) {4};
\node[vertex] (5) at (1.5,-2.6) {5};
\path[bicolor={red}{blue}] (0) to[bend right=-11.25] (5);
%\path[bicolor_neg={red}{red}] (0) to[bend right=11.25] (5);
\path[bicolor_neg={red}{red}] (1) to (3);
\path[bicolor_neg={green}{red}] (2) to[bend right=11.25] (4);
\path[bicolor={blue}{orange}] (2) to[bend right=-11.25] (5);
\path[bicolor_neg={red}{red}, bend right=11.25, opacity=0.5] (0) to (5); %new
\end{tikzpicture}
\end{document}

One edge semi transparent

7
  • your solution is so amazing, thank you so much! also you are totally right. The latex is generated from a code.
    – Xuemei
    Sep 21 at 12:50
  • I add the opacity to have make edges transparent (e.g., edge05) and fill the diamond in white(should have no transparency). \path[bicolor_neg={red}{red}, bend right=11.25, opacity=0.5] (0) to (5); there is no transparency for the edge but the diamond. it seems to only apply opacity on the diamond layer.
    – Xuemei
    Sep 21 at 16:39
  • You need to add the opacity to the action that do the drawing e.g. postaction={draw=#1, opacity=0.5, decoration={curveto, post=moveto, post length=0.5*\pgfmetadecoratedpathlength}, decorate}, Sep 21 at 16:56
  • \path[bicolor_neg={red}{red}, bend right=11.25, opacity=0.5] (0) to (5); is also working, as the opacity option is inherited by all actions. To prevent the diamond being transparent, you need node[diamond, draw, thin, minimum width=10pt, opacity=1] {}; Sep 21 at 17:04
  • thanks! if one sets the opacity in the postaction, all edges will have the same opacity but each edge can have its own opacity.
    – Xuemei
    Sep 21 at 17:04
4

You could use the pgfonlayer environment to bring the black nodes towards the front:

\documentclass[border=10pt]{standalone}


\usepackage{tikz}
\usepackage{verbatim}

\usetikzlibrary{decorations.markings}
\usetikzlibrary{positioning,shapes.geometric} 

\pgfdeclarelayer{foreground layer}
\pgfsetlayers{main,foreground layer}

\begin{document}
\pagestyle{empty}

\definecolor{vertexcol}{RGB}{250,250,250}\definecolor{col0}{RGB}{0,0,204}\definecolor{col1}{RGB}{204,0,0}\definecolor{col2}{RGB}{0,204,0}\definecolor{col3}{RGB}{255, 165, 0}\definecolor{col4}{RGB}{128,0,128}\definecolor{col5}{RGB}{255, 255, 0}\definecolor{fontcolor}{RGB}{0,0,0}

\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 1.0\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}
\tikzset{
    bicolor_neg/.style n args={2}{
        decoration={
            markings,
            mark=at position 0.5 with {
                \begin{pgfonlayer}{foreground layer}
                \node[diamond, draw,minimum width=10pt]{\global\setlength\mylen{\pgfdecoratedpathlength}};
                \end{pgfonlayer}
            },
        },
        draw=#1,
        dash pattern=on 0.5\mylen off 1.0\mylen,
        preaction={decorate},
        postaction={
            draw=#2,
            dash pattern=on 0.5\mylen off 0.5\mylen,dash phase=0.5\mylen
        },
    }
}



\begin{tikzpicture}
    \tikzstyle{vertex}=[circle, draw=black, ultra thick ,fill=vertexcol!80,minimum size=15pt]\textbf{}
    \node[vertex] (0) at (3.0,0.0) {\color{fontcolor}0};
    \node[vertex] (1) at (1.5000000000000004,2.598076211353316) {\color{fontcolor}1};
    \node[vertex] (2) at (-1.4999999999999993,2.5980762113533165) {\color{fontcolor}2};
    \node[vertex] (3) at (-3.0,3.6739403974420594e-16) {\color{fontcolor}3};
    \node[vertex] (4) at (-1.5000000000000013,-2.598076211353315) {\color{fontcolor}4};
    \node[vertex] (5) at (1.5,-2.598076211353316) {\color{fontcolor}5};
    \path (0) edge[line width=4.0,bicolor={col1}{col0}, bend right=-11.25, opacity=1.0] (5);
    \path (0) edge[line width=4.0,bicolor_neg={col1}{col1}, bend right=11.25, opacity=1.0] (5);
    \path (1) edge[line width=4.0,bicolor_neg={col1}{col1}, bend right=0.0, opacity=1.0] (3);
    \path (2) edge[line width=4.0,bicolor_neg={col2}{col1}, bend right=11.25, opacity=1.0] (4);
    \path (2) edge[line width=4.0,bicolor={col0}{col3}, bend right=-11.25, opacity=1.0] (5);
\end{tikzpicture}

\end{document}

enter image description here

2

Your Answer

Xuemei is a new contributor. Be nice, and check out our Code of Conduct.

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.