2

I define Tikz pictures within each other. The typesetting fails in a way that the edges are not drawn properly: the edges are not drawn from the centers of vertices in the lattice subgraph structure, instead on the side of the vertices like below

enter image description here

and the lattice subgraph structure is in 2. MWE while simplified example without the issue in 1. MWE: the issue somehow occurs only when having many tikz environments.

1. MWE draws the edges nicely between the centers of vertices

\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=4pt,minimum size=3pt] 
\draw (0,0)node{};
\draw (1,0)node{};
\draw (0.5,1)node{};
\draw (0,0)--(1,0);
\end{tikzpicture}

enter image description here

2. MWE not drawing the edges nicely between the centers of vertices, why?

\begin{tikzpicture}[scale=1.2]
\tikzstyle{every node}=[draw, circle]
\node(0) {
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=0pt,minimum size=3pt] 
\draw (0,0)node{};
\draw (1,0)node{};
\draw (0.5,1)node{};
\end{tikzpicture}
};

\node[above =of 0](1){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt] 
\draw (0,0)node{} -- (1,0)node{};
\draw (0.5,1)node{};
\end{tikzpicture}
};


\node[above right =of 1](21){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt] 
\draw (0,0)node{} -- (1,0)node{} -- (0.5,1)node{};
\end{tikzpicture}
};

\node[above left =of 1](22){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt] 
\draw (0,0)node{} -- (0.5,1)node{} -- (1,0)node{};
\end{tikzpicture}
};


\node[above left =of 21](30){
\begin{tikzpicture}
\tikzstyle{every node}=[draw, circle, fill=black, inner sep=1pt,minimum size=3pt] 
\draw (0,0)node{} -- (1,0)node{} -- (0.5,1)node{};
\draw (0,0)node{} -- (0.5,1)node{};
\end{tikzpicture}
};


\draw (0) -- (1);
\draw (1) -- (21);
\draw (1) -- (22);
\draw (21) -- (30);
\draw (22) -- (30);
%\draw (21) -- (31);
%\draw (21) -- (32);
%\draw (21) -- (33);
%\draw (31) -- (41);
%\draw (31) -- (42);
%\draw (32) -- (42);
%\draw (33) -- (41);
%\draw (22) -- (32);

\end{tikzpicture}

enter image description here

where the lattice subgraph picture has the failed typesetting, the edges not drawn between the centers. So

How to draw edges properly from the centers of vertices in the lattice subgraph with many tikz environments?

Perhaps relevant

  1. Nesting tikz pictures
  2. Joining different TikZ pictures
  3. What are most important variables set at the beginning of a tikzpicture? of a scope?
1

1 Answer 1

6

There are several ways of achieving that without nesting tikzpictures. You could for example first draw the five circles as nodes, then draw the dots and lines relative to those circles, and finally draw lines between the circles.

In the code below I define a macro \drawdots that draws the three dots in each circle, for convenience. It takes one argument, which is the name of a node, and has three lines of this form:

\path (#1.center) ++(90:0.7cm) node [dotnode] (#1-top) {};

#1 represents the node name passed to the macro, so (#1.center) is the center anchor of the node. ++(90:0.7cm) means roughly "move the active point to the point that is 0.7cm away from the previous point, at an angle of 90 degrees". At this point a new node is declared with node [dotnode] (#1-top) {};. dotnode is style defined as

dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle}

so it will create a small circle, filled with black, and that node has the name #1-top.

\documentclass[border=4mm]{standalone} 
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle},
bigcircle/.style={draw,circle,minimum size=2cm}
}
\newcommand{\drawdots}[1]{
  \path (#1.center) ++(90:0.7cm) node [dotnode] (#1-top) {};
  \path (#1.center) ++(-30:0.7cm) node [dotnode] (#1-right) {};
  \path (#1.center) ++(210:0.7cm) node [dotnode] (#1-left) {};
}
\begin{document}              
\begin{tikzpicture}
% place circles
\node [bigcircle] (A) {};
\node [bigcircle,above=of A] (B) {};
\node [bigcircle,above right=of B] (C) {};
\node [bigcircle,above left=of B] (D) {};
\node [bigcircle,above right=of D] (E) {};

% draw dots
\foreach \A in {A,...,E}
   \drawdots{\A};

% draw edges between dots
\draw (B-left) -- (B-right);
\draw (C-left) -- (C-right) -- (C-top);
\draw (D-left) -- (D-top) -- (D-right);
\draw (E-left) -- (E-right) -- (E-top) -- (E-left);

% draw edges between circles
\draw (A) -- (B) -- (C) -- (E) -- (D) -- (B);
\end{tikzpicture}
\end{document}

Another option would be to use a pic, an example is in the code below.

\documentclass[border=4mm]{standalone} 
\usepackage{tikz}
\pgfmathsetmacro{\circrad}{0.7}
\tikzset{
dotnode/.style={inner sep=0pt,outer sep=0pt,minimum size=3pt,fill,circle},
pics/dotcircle/.style args={#1#2#3}{
code={
 \node [circle,draw,inner sep=0pt,outer sep=0pt,minimum size=2*\circrad cm] (-circ) at (0,0) {};
 \node [dotnode] (-top) at (90:0.7*\circrad cm) {};
 \node [dotnode] (-right) at (-30:0.7*\circrad cm) {};
 \node [dotnode] (-left) at (210:0.7*\circrad cm) {};
\ifnum #1>0
  \draw (-left) -- (-right);
\fi
\ifnum #2 > 0
   \draw (-right) -- (-top);
\fi
\ifnum #3 > 0
   \draw (-left) -- (-top);
\fi
}}}

\begin{document}              
\begin{tikzpicture}
\pic (A) at (0,0) {dotcircle={000}};
\pic (B) at (0,2) {dotcircle={100}};
\pic (C) at (2,4) {dotcircle={110}};
\pic (D) at (-2,4) {dotcircle={011}};
\pic (E) at (0,6) {dotcircle={111}};

\foreach \A/\B in {A/B,B/C,B/D,D/E,C/E}
  \draw (\A-circ) -- (\B-circ);
\end{tikzpicture}
\end{document}

enter image description here

3
  • Can you elaborate on the \drawdots command in the first method? Consider \path (#1.center) ++(90:0.7cm) node [dotnode] (#1-top) {}: draw path from the center to new node where ++ means to add new node? (90:0.7cm) means 90 degrees while 0.7cm is? Dotnode is?
    – hhh
    Apr 17, 2016 at 10:00
  • 1
    @hhh Was that any clearer? Too verbose, perhaps. (90:0.7cm) is a -polar coordinate, and the ++ means that the coordinate is relative to the previous one. Apr 17, 2016 at 12:12
  • Is there a command in tikz to check whether the vertices in the graphs are adjacent like the subgraphs of the lattice above? I moved this question here, I feel you probably know how to do this.
    – hhh
    Apr 17, 2016 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .