1

When I draw my graph, the edges overlap with some of the nodes. How do I make the nodes on top of the edges instead of the edges on top of the nodes? I tried using the backgrounds package, but it doesn't seem to work. Here is my code for reference.

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary[topaths]
\begin{document}
\begin{tikzpicture}[transform shape]
  \foreach \x in {1,...,16}{%
    \pgfmathparse{(\x-1)*360/16}
    \node[draw,circle,inner sep=0.05cm,minimum width = 13pt,font=\scriptsize] (N-\x) at (\pgfmathresult:1.4cm) {$\x$};
  } 
  \foreach \x [count=\xi from 1] in {1,...,15}{%
    \foreach \y in {\x,...,16}{%
    \begin{scope}[on background layer]
      \path (N-\x) edge[ultra thin,-] (N-\y);
    \end{scope}
  }
}
\end{tikzpicture}
\end{document}
  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass and ending with \end{document}. – Bobyandbob Oct 20 '17 at 15:04
  • Just add fill=white. -> \node[draw,circle,inner sep=0.05cm,minimum width = 13pt,font=\scriptsize,fill=white] (N-\x) at (\pgfmathresult:1.4cm) {$\x$}; – Bobyandbob Oct 20 '17 at 15:20
1

In your example, the nodes are already in the foreground and the edges on the background (layer). But the nodes themselves have a transparent background. So you can see the edge background behind the nodes. You just have to fill the nodes white to make the background edge invisible:

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary[topaths]
\begin{document}
\begin{tikzpicture}[transform shape]
  \foreach \x in {1,...,16}{%
    \pgfmathparse{(\x-1)*360/16}
    \node[draw,circle,fill=white,inner sep=0.05cm,minimum width = 13pt,font=\scriptsize] (N-\x) at (\pgfmathresult:1.4cm) {$\x$};
  } 
  \foreach \x [count=\xi from 1] in {1,...,15}{%
    \foreach \y in {\x,...,16}{%
    \begin{scope}[on background layer]
      \path (N-\x) edge[ultra thin,-] (N-\y);
    \end{scope}
  }
}
\end{tikzpicture}
\end{document}

to get

nodes above edges

  • Thanks! I have been trying to figure this out forever, and the solution was so simple. Edit: It seems like the nodes aren't in the foreground though because if I remove the edges from the background layer, the edges still overlap the nodes. If I leave the edges in the background layer, the solution seems to work. – Ralff Oct 20 '17 at 15:21
  • Oh. I misunderstood what you meant. I get it now. – Ralff Oct 20 '17 at 15:28
  • @Ralff Added additional explanation to (hopefully) make it more clear. – Schweinebacke Oct 20 '17 at 15:29

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.