5

enter image description hereI have drawn a diagram using tikzpicture, but it has arrows crossing over each other. How do I keep the Horizontal arrows in each level intact and break the other arrows at the point of intersection?

Here is my code:

%\documentclass[reqno]{amsart}
\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm,amsxtra}
\usepackage{cite}
\usepackage{enumerate}
\usepackage[mathscr]{eucal}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{tikz-cd}
\usepackage{tkz-graph}
\usepackage{tkz-berge}
\usetikzlibrary{positioning,arrows,shapes.geometric,trees,matrix}
\usepackage{graphicx}
\usepackage{mathtools}
\usetikzlibrary{positioning}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{caption}



\begin{document}






\begin{tikzpicture}
  \matrix (m) [matrix of math nodes, row sep=1.6cm,
    column sep=0.4cm]{
    &&Level(1,3)&&&&\\
     &Level(2,2)&Level(2,3)&& Level(2,5)&&Level(2,7)\\
     &&Level(3,3)&& Level(3,5)&&Level(3,7)\\
      Level(4,1)&Level(4,2)&& Level(4,4)&&Level(4,6)&  \\
      &Level(5,2)&& Level(5,4)&&Level(5,6)& \\
       Level(6,1)&& Level(6,3)&& Level(6,5)&&\\
        Level(7,1) && Level(7,3)&& Level(7,5)&&\\};
  \path[-stealth]
(m-7-1) edge (m-7-3) edge (m-6-1)

(m-7-3) edge (m-7-5) 
        edge (m-6-3)

(m-7-5) edge (m-6-5) 
        edge (m-5-6)

(m-6-1) edge (m-6-3) 
        edge (m-4-1)

(m-6-3) edge (m-6-5) 
        edge (m-4-4)

(m-6-5) edge (m-4-6)
(m-5-2) edge (m-5-4) 
        edge (m-4-2)
        edge (m-3-3)

(m-5-4) edge (m-4-4) 
        edge (m-5-6)
        edge (m-3-5)
(m-5-6) edge (m-3-7)
(m-4-2) edge (m-4-4) 
        edge (m-2-2) 
        edge (m-2-3)

(m-4-4) edge (m-4-6) 
        edge (m-2-5)

(m-3-3) edge (m-2-3) 
        edge (m-3-5)

 (m-3-5) edge (m-3-7)
 (m-2-2) edge (m-1-3)
 (m-2-3) edge (m-1-3)
 (m-2-5) edge (m-2-7)
 ;

\path[>=stealth,-,thick]
(m-7-1) edge [double, minimum width=3cm] (m-5-2)
(m-6-1) edge [double, minimum width=3cm] (m-4-2)
(m-5-6) edge [double, minimum width=3cm] (m-4-6)
(m-4-1) edge [double, minimum width=3cm] (m-2-2)
(m-4-6) edge [double, minimum width=3cm] (m-2-7)
(m-3-5) edge [double, minimum width=3cm] (m-2-5)
(m-3-7) edge [double, minimum width=3cm] (m-2-7)
(m-2-3) edge [double, minimum width=3cm] (m-2-5)
;
\end{tikzpicture}


\end{document} 

2 Answers2

6

With crossing over, arrows that are drawn later, erase part of the arrows they cross.

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}

\usepackage{tikz-cd}

\newcommand{\level}{\mathrm{Level}}

\begin{document}

\begin{tikzcd}[column sep=1.5em,row sep=large,arrows={crossing over}]
&&
\level(1,3)
\\ \\
&
\level(2,2) \arrow[uur] &
\level(2,3) \arrow[uu] \arrow[rr,thick,equals] &&
\level(2,5) \arrow[rr] \arrow[ddl,leftarrow] &&
\level(2,7) \arrow[ddl,thick,equals]
\\
&&
\level(3,3) \arrow[u] \arrow[rr] \arrow[ddl,leftarrow] &&
\level(3,5) \arrow[u,thick,equals] \arrow[rr] \arrow[ddl,leftarrow] &&
\level(3,7) \arrow[u,thick,equals]
\\
\level(4,1) \arrow[uur,thick,equals] &
\level(4,2) \arrow[uu] \arrow[rr] \arrow[uur] &&
\level(4,4) \arrow[rr] \arrow[ddl,leftarrow] &&
\level(4,6) \arrow[ddl,leftarrow]
\\
&
\level(5,2) \arrow[u] \arrow[rr] \arrow[ddl,leftarrow] &&
\level(5,4) \arrow[u] \arrow[rr] &&
\level(5,6) \arrow[u,thick,equals] \arrow[uur]
\\
\level(6,1) \arrow[uu] \arrow[uur,thick,equals] \arrow[rr] &&
\level(6,3) \arrow[rr] &&
\level(6,5)
\\
\level(7,1) \arrow[u] \arrow[rr] &&
\level(7,3) \arrow[u] \arrow[rr] &&
\level(7,5) \arrow[u] \arrow[uur]
\end{tikzcd}

\end{document} 

enter image description here

egreg
  • 1,121,712
  • can this be achieved in tikzpicture environment ? Also the figure is going outside the specified area of the generated pdf (using amsart documentclass), what to do? – user149418 Jun 23 '19 at 14:44
  • 1
    @user149418 Using matrix of math nodes results in worse quality. About the size, I'm afraid that the only option is scaling. – egreg Jun 23 '19 at 14:47
  • I think the connection between (3,5) and (3,7) should be below (4,6) -- (2,7), likewise for (5,4) -- (5,6) vs. (6,5) -- (4,6). Also I do not think "matrix of math nodes esults in worse quality" is a true statement because tikz-cd precisely uses such a matrix. –  Jun 23 '19 at 19:22
  • @marmot In my experience the arrows are better directed with \arrow syntax. About the connections you mention, I followed the OP's preference, but I agree that it would be better as you propose. – egreg Jun 23 '19 at 20:37
  • 1
    "better directed" as compared to what? The \arrow command can be certainly looked up and used in "plain" TikZ, too. But of course I agree that for many purposes tikz-cd is more convenient (but this convenience backfires once one wants to do some "nonstandard" thing). –  Jun 23 '19 at 20:41
  • @marmot The rectangle shape for nodes is different. Here the nodes have the same height and width; the difference is apparent when there are different height, depth and width. – egreg Jun 23 '19 at 20:52
  • @egreg Sure, but you can always load the cd library which does have the asymmetric rectangle shape. I believe that it is mainly a matter of wording. Since tikz-cd is entirely based on tikz, there is nothing that you can't only do in tikz-cd but not in tikz. Similar comments apply e.g. to smartdiagram, like tikz-cd this is a great package, and really super convenient to use. However, everything that can be done with either of these packages can be done with "plain" TikZ, too, yet may require more keystrokes. –  Jun 23 '19 at 20:58
  • @marmot While you are right about the library, I find the syntax of tikz-cd not only “more convenient”. Your code is awkward and really difficult to maintain. – egreg Jun 23 '19 at 21:04
  • @egreg I find "awkward" a bit out of the place, this is not the wording that we should use, and this is not "my code". "My code" is the on top style, which is not awkward at all, and the above example allows one to retain full control over the outcome, something your suggestion can only achieve with much more efforts because the arrows get drawn in the order you place the commands. The code that you call "awkward", on the other hand, allows you to retain full control without any hack like execute at end picture. –  Jun 23 '19 at 21:10
4

The standard trick for this is to draw a thick white line underneath, which can be achieved with preaction. This answer comes with a style on top, which can be used like (m-6-5) edge[on top] (m-4-6). You need to draw the topmost connections last. At the moment, the default line widths of the white lines is 4pt but you can adjust them to your needs.

\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[on top/.style={preaction={draw=white,-,line width=#1}},
on top/.default=4pt]
  \matrix (m) [matrix of math nodes, row sep=1.6cm,
    column sep=0.4cm]{
    &&Level(1,3)&&&&\\
     &Level(2,2)&Level(2,3)&& Level(2,5)&&Level(2,7)\\
     &&Level(3,3)&& Level(3,5)&&Level(3,7)\\
      Level(4,1)&Level(4,2)&& Level(4,4)&&Level(4,6)&  \\
      &Level(5,2)&& Level(5,4)&&Level(5,6)& \\
       Level(6,1)&& Level(6,3)&& Level(6,5)&&\\
        Level(7,1) && Level(7,3)&& Level(7,5)&&\\};
  \path[-stealth]
(m-7-1) edge (m-7-3) edge (m-6-1)

(m-7-3) edge (m-7-5) 
        edge (m-6-3)

(m-7-5) edge (m-6-5) 
        edge (m-5-6)

(m-6-1) edge (m-4-1)

(m-6-3) edge (m-6-5) 

(m-5-4) edge (m-4-4) 

(m-5-2) edge (m-5-4) 
        edge (m-4-2)
        edge (m-3-3)

(m-5-4)   edge (m-5-6)
        edge (m-3-5)
(m-5-6) edge (m-3-7)
(m-4-2)  edge (m-2-2) 
        edge (m-2-3)



(m-3-3) edge (m-2-3) 
        edge (m-3-5)

 (m-3-5) edge (m-3-7)
 (m-2-2) edge (m-1-3)
 (m-2-3) edge (m-1-3)
 (m-2-5) edge (m-2-7)
(m-4-2) edge[on top] (m-4-4) 
(m-4-4) edge[on top] (m-4-6) 
(m-6-5) edge[on top] (m-4-6)
(m-4-4) edge[on top] (m-2-5)
(m-6-3) edge[on top] (m-4-4)
 ;

\path[>=stealth,-,thick]
(m-7-1) edge [double] (m-5-2)
(m-6-1) edge [double] (m-4-2)
(m-5-6) edge [double] (m-4-6)
(m-4-1) edge [double] (m-2-2)
(m-3-5) edge [double] (m-2-5)
(m-3-7) edge [double] (m-2-7)
(m-2-3) edge [double] (m-2-5)
(m-4-6) edge [double,on top=5pt] (m-2-7)
;
\path[-stealth] (m-6-1) edge[on top] (m-6-3) ;
\end{tikzpicture}
\end{document} 

enter image description here

@egreg called this code "awkward". The IMHO most redundant thing, however, is IMHO that one has to add the position of the entry by hand (but I do not like to use words like "awkward"). In any case, this can be made as easily as defining a style

level/.style={
execute at begin node={\text{Level}(\the\pgfmatrixcurrentrow,\the\pgfmatrixcurrentcolumn)}
}

which gets used in the following code, which also has some loops to draw the arrows.

\documentclass[border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[on top/.style={preaction={draw=white,-,line width=#1}},
on top/.default=4pt,level/.style={
execute at begin node={\text{Level}(\the\pgfmatrixcurrentrow,\the\pgfmatrixcurrentcolumn)}
}]
  \matrix (m) [matrix of math nodes,nodes=level,row sep=1.6cm,
    column sep=0.4cm]{
    &&{}&&&&\\
     &{}&{}&& {}&&{}\\
     &&{}&& {}&&{}\\
      {}&{}&& {}&&{}&  \\
      &{}&& {}&&{}& \\
       {}&& {}&& {}&&\\
        {} && {}&& {}&&\\};
\path[-stealth] 
 (m-6-1) edge (m-4-1) (m-4-2) edge (m-2-2) (m-2-3) edge (m-1-3)
 (m-4-2) edge (m-2-3) (m-5-2) edge (m-3-3) 
 (m-5-4) edge (m-3-5) (m-7-5) edge (m-5-6) (m-5-6) edge (m-3-7)
 (m-2-2) edge (m-1-3);
\path[-stealth,every edge/.append style={on top}]  
foreach \Y [count=\Z] in {3,5,7}{ 
  foreach \X in {1,3,5}
 {(m-\Y-\the\numexpr\X+3-\Z\relax) edge (m-\the\numexpr\Y-1\relax-\the\numexpr\X+3-\Z\relax)
 \ifnum\X<5
 (m-\Y-\the\numexpr\X+3-\Z\relax) edge (m-\Y-\the\numexpr\X+5-\Z\relax)
 \ifnum\the\numexpr\Y*10+\X=31
 \else
 (m-\the\numexpr\Y-1\relax-\the\numexpr\X+3-\Z\relax) edge (m-\the\numexpr\Y-1\relax-\the\numexpr\X+5-\Z\relax)
 \fi
 \fi
 }} (m-4-4) edge (m-2-5) (m-6-3) edge (m-4-4);
 \path[thick,every edge/.append style={double,on top=5pt}] 
  (m-4-1) edge (m-2-2) (m-6-1) edge (m-4-2) (m-7-1) edge (m-5-2)
  (m-2-3) edge (m-2-5) (m-2-7) edge (m-3-7)
  (m-2-7) edge (m-4-6) (m-4-6) edge (m-6-5);
\path[-stealth,every edge/.append style={on top}]  (m-6-1) edge (m-6-3); 
\end{tikzpicture}
\end{document} 

This information, together with this answer, allows one to simplify egreg's answer and to correct the arrows there (such that there is a 3D impression).

\documentclass{article}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{tikz-cd}
\usetikzlibrary{backgrounds}

\newcommand{\level}{\text{level}(\the\pgfmatrixcurrentrow,\the\pgfmatrixcurrentcolumn)}

\begin{document}

\begin{tikzcd}[column sep=1.5em,row sep=large,arrows={crossing over},
execute at end picture={
\begin{scope}[on background layer]
 \path[->] (\tikzcdmatrixname-6-2) edge (\tikzcdmatrixname-6-4)
 (\tikzcdmatrixname-6-4) edge (\tikzcdmatrixname-6-6)
 (\tikzcdmatrixname-4-5) edge (\tikzcdmatrixname-4-7);
\end{scope}}]
&&
\level
\\ \\
&
\level \arrow[uur] &
\level \arrow[uu] \arrow[rr,thick,equals] &&
\level \arrow[rr] \arrow[ddl,leftarrow] &&
\level \arrow[ddl,thick,equals]
\\
&&
\level \arrow[u] \arrow[ddl,leftarrow] &&
\level \arrow[u,thick,equals]  \arrow[ddl,leftarrow] &&
\level \arrow[u,thick,equals]
\\
\level \arrow[uur,thick,equals] &
\level \arrow[uu] \arrow[rr] \arrow[uur] &&
\level \arrow[rr] \arrow[ddl,leftarrow] &&
\level \arrow[ddl,leftarrow]
\\
&
\level \arrow[u] \arrow[ddl,leftarrow] &&
\level \arrow[u]  &&
\level \arrow[u,thick,equals] \arrow[uur]
\\
\level \arrow[uu] \arrow[uur,thick,equals] \arrow[rr] &&
\level \arrow[rr] &&
\level
\\
\level \arrow[u] \arrow[rr] &&
\level \arrow[u] \arrow[rr] &&
\level \arrow[u] \arrow[uur]
\end{tikzcd}
\end{document} 

enter image description here