6

I have a matrix of numbers with arrows and loops. The trouble i'm having is some loops are of different sizes depending on which way the loop is oriented. Loops are defined with 60 degrees difference between the in and out position (with midpoint of the loop corresponding to 0,90,180,270).

In the image included, the loop size differences can be seen from those in the middle column where loops above the matrix element are larger than the loops below the matrix element. Also the loops of corner elements are smaller than those of the side elements.

Can anyone point out what I'm doing wrong, or how to make the loops look the same size but `rotated'? MWE and its output given below.

\documentclass[tikz]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{fit,backgrounds}

\begin{document}

\newcommand{\aU}{\arrow[bend left]{u}{}}
\newcommand{\aD}{\arrow[bend left]{d}{}}
\newcommand{\aL}{\arrow{l}{}}
\newcommand{\aR}{\arrow{r}{}}
\newcommand{\aS}[2]{\arrow[loop, out=#1, in=#2,looseness=4]{}{}}
\newcommand{\aSc}[1]{\arrow[loop, out=#1+30, in=#1-30,looseness=4]{}{}}%Self, clockwise
\newcommand{\aSa}[1]{\arrow[loop, out=#1-30, in=#1+30,looseness=4]{}{}}%Self, anti-clockwise

\begin{tikzcd}[nodes={inner sep=1pt,minimum size=3ex},cramped,column sep=scriptsize, row sep=scriptsize,
every matrix/.append style={name=m},execute at end picture={
    \begin{scope}[on background layer]
        \node[rounded corners,fill=black!10,minimum width=1cm, fit=(m-1-2) (m-4-2)](c1) {};
    \end{scope};
}]
2\aD\aSc{135}      &  1\aL\aSc{90}    &  0\aL\aD\aSc{45}    \\
6\aU\aD\aSa{180}   &  5\aL\aSc{90}   &  4\aL\aU\aD\aSc{0} \\
10\aU\aD\aSc{180}  &  9\aL\aSa{270}   &  8\aL\aU\aD\aSc{0} \\
14\aU\aSc{225}     &  13\aL\aSa{270}  &  12\aL\aU\aSc{315}
\end{tikzcd}
\end{document}

enter image description here

Dunk the Lunk
  • 600
  • 4
  • 10

1 Answers1

6

It looks like the center of the nodes isn't positioned at the center of the numbers themselves. And the loops are constructed around the centers of the nodes. Also, the rectangular node shape doesn't help in making loops equal. As a workaround you could use the circle node shape (notice that I had to increase the minimum node size to 3.5ex as well to get all circles equal):

\usepackage{tikz-cd}
\usetikzlibrary{fit,backgrounds}

\begin{document}

\newcommand{\aU}{\arrow[bend left]{u}{}}
\newcommand{\aD}{\arrow[bend left]{d}{}}
\newcommand{\aL}{\arrow{l}{}}
\newcommand{\aR}{\arrow{r}{}}
\newcommand{\aS}[2]{\arrow[loop, out=#1, in=#2,looseness=4]{}{}}
\newcommand{\aSc}[1]{\arrow[loop, out=#1+30, in=#1-30,looseness=4]{}{}}%Self, clockwise
\newcommand{\aSa}[1]{\arrow[loop, out=#1-30, in=#1+30,looseness=4]{}{}}%Self, anti-clockwise

\begin{tikzcd}[nodes={inner sep=1pt,minimum size=3.5ex,circle},cramped,column sep=scriptsize, row sep=scriptsize,
every matrix/.append style={name=m},execute at end picture={
    \begin{scope}[on background layer]
        \node[rectangle,rounded corners,fill=black!10,minimum width=1cm, fit=(m-1-2) (m-4-2)](c1) {};
    \end{scope};
}]
2\aD\aSc{135}      &  1\aL\aSc{90}    &  0\aL\aD\aSc{45}    \\
6\aU\aD\aSa{180}   &  5\aL\aSc{90}   &  4\aL\aU\aD\aSc{0} \\
10\aU\aD\aSc{180}  &  9\aL\aSa{270}   &  8\aL\aU\aD\aSc{0} \\
14\aU\aSc{225}     &  13\aL\aSa{270}  &  12\aL\aU\aSc{315}
\end{tikzcd}
\end{document}

The result:

enter image description here