3

I am a tikz beginner. I'm trying to recreate the picture below (Especially the top-left, top-right, bottom-right chain). I started out like this:

\documentclass[tikz]{standalone}
\usepackage{amsmath}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \tikzstyle{tensor} = [circle, inner sep = 0pt, minimum size = 0.2cm]
  \tikzstyle{QTensor} = [tensor, draw, fill=green!50, minimum size = 0.3cm]
  \tikzstyle{delta} = [tensor, draw, fill=red!50, minimum size = 0.4cm]
  \tikzstyle{ellipsisNode} = [tensor, minimum size = 0.7cm]

  % Draw delta tensors
  \foreach \x in {2, 4}
    \foreach \y in {2, 4}
      \node[delta] (\x-\y) at (\x, \y) {};

  % Draw Q tensors
  \foreach \x in {1, 3, 5}
    \foreach \y in {2, 4}
      {
      \node[QTensor] (\x-\y) at (\x, \y) {};
      \node[QTensor] (\y-\x) at (\y, \x) {};
      }

  % Draw ellipsis nodes
  \foreach \x in {0, 6}
    \foreach \y in {2, 4}
    {
    \node[ellipsisNode] (\x-\y) at (\x, \y) {$\cdots$};
    \node[ellipsisNode, rotate = 90] (\y-\x) at (\y, \x) {$\dots$};
    }

  % Connections
  \foreach \x in {0, ..., 5}
    \foreach \y in {2, 4}
    {
    \pgfmathtruncatemacro{\neighbor}{\x + 1};
    \draw (\x-\y) -- (\neighbor-\y);
    \draw (\y-\x) -- (\y-\neighbor);
    }

\end{tikzpicture}
\end{document}

I find the task daunting. I do not want to keep track of so many coordinates. Do I need to use the positioning library? Can I use coordinate systems relative to different points? Or can I make separate tikz pictures and join them with an arrow?

Since I need to make several more images of this type, I am interested in the most maintainable, idiomatic solution.

tensor networks

My output so far:

what I have so far

4

For bits of tikz code that are reused a lot it is worth defining a pics style for it. The code can then be drawn using \draw (0,0) pic{<pics name>};. You can read more about this in section 18.2 of the tikz manual (version 3.0.1a). Once you have defined pics for the repeating components of your diagrams it is relatively straightforward to place them where you want using \foreach loops.

Doing it this way you can produce the following two images

enter image description here

using the code:

\documentclass{article}
\usepackage{tikz}

\tikzset{% define pic styles
  pics/array/.style={
       code={\draw(-0.6,0)--+(1.2,0);
             \draw(0,-0.6)--+(0,1.2);
             \draw[fill=red!50] circle[red!50,radius=4pt](0,0);
             \foreach \a in {0.5,-0.5} {
                 \draw[fill=green!50] (0,\a) circle[radius=2pt];
                 \draw[fill=green!50] (\a,0) circle[radius=2pt];
             }
            }
       },
  pics/semiarray/.style={
       code={\draw[rounded corners, fill=yellow!20](-0.45,-0.45) rectangle +(0.9,0.9);
             \draw(-0.6,0)--+(1.2,0); % draw the "axes" over the top of the shading
             \draw(0,-0.6)--+(0,1.2); 
             \draw[fill=red!50] circle[red!50,radius=4pt](0,0);
             \draw[fill=green!50](-0.35,-0.1) -- +(0,0.2) arc(90:-90:0.1) -- cycle; % semicircles
             \draw[fill=green!50](0.35,0.1) -- +(0,-0.2) arc(270:90:0.1) -- cycle;
             \draw[fill=green!50](-0.1,0.35) -- +(0.2,0) arc(0:-180:0.1) -- cycle;
             \draw[fill=green!50](-0.1,-0.35) -- +(0.2,0) arc(0:180:0.1) -- cycle;
             }
       },
}

\begin{document}

\begin{tikzpicture}
    \foreach \x in {0,...,4} {
        \draw[dotted](-0.4,\x)--+(-0.5,0);
        \draw[dotted](4.4,\x)--+(0.5,0);
        \draw[dotted](\x,-0.4)--+(0,-0.5);
        \draw[dotted](\x,4.4)--+(0,0.5);
    }
    \foreach \x in {0,...,4} {
        \foreach \y in {0,...,4} {
           \draw(\x,\y) pic{array};
        }
    }
\end{tikzpicture}
\begin{tikzpicture}
    \foreach \x in {0,...,4} {
        \draw[dotted](-0.4,\x)--+(-0.5,0);
        \draw[dotted](4.4,\x)--+(0.5,0);
        \draw[dotted](\x,-0.4)--+(0,-0.5);
        \draw[dotted](\x,4.4)--+(0,0.5);
    }
    \foreach \x in {0,...,4} {
        \foreach \y in {0,...,4} {
           \draw(\x,\y) pic{semiarray};
        }
    }
\end{tikzpicture}

\end{document}

Notice that the coordinates inside the pics commands are relative to where the pic is placed. The pics commands can also take arguments (see, for example, Simple drawing with TikZ).

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.