9

I'm trying to draw a web graph for a paper, using TikZ to draw the individual pages as nodes. When I typeset, however, the graph sits in one small corner of the line, and the nodes are too close together. node distance doesn't work, and minimum size just expands the nodes such that they overlap each other. How can I set the minimum distance between the nodes? Thanks.

The unfinished (no lines yet) graph looks like this:

enter image description here

Ideally, it should be the width of the page and the nodes should be distributed farther away from each other.

My code looks like this:

 \documentclass[12pt, oneside]{amsart}      
\usepackage{geometry}                       
\geometry{a4paper}

\usepackage{tikz}

\begin{document}

\tikzstyle{node}=[circle, draw=black!100, thick]
\tikzstyle{dangling node}=[circle, draw=black!100, fill=black!30, thick]
\begin{tikzpicture}[minimum size=5mm][node distance=5.3cm,>=stealth?,bend angle=45,auto]
  \node[node](page 1){1};
  \node[node](page 2)[right of=page 1]{2};
  \node[node](page 3)[below of=page 1]{3};
  \node[node](page 4)[below of=page 2]{4};
  \node[dangling node](page 5)[below of=page 3, xshift=5mm]{5};
\end{tikzpicture}

\end{document}
Torbjørn T.
  • 206,688
Victor Z
  • 383
  • 1
    Try the syntax right=10mm of page 1 (instead of right of). – TeXnician Apr 19 '17 at 09:12
  • Hmm, I tried it, but latex gave me the error that 'of' was not defined. – Victor Z Apr 19 '17 at 09:27
  • I forgot \usetikzlibrary{positioning} – TeXnician Apr 19 '17 at 09:30
  • 2
    You have \begin{tikzpicture}[<options1>][<options 2>], while you should have \begin{tikzpicture}[<options 1>,<options 2>]. – Torbjørn T. Apr 19 '17 at 09:39
  • Wow, I didn't think that would work. Thanks! I thought that the multiple option blocks made no difference. One interesting thing is that when I combine the options, the code that worked before returned an error saying =>stealth? was undefined due to the ? sign, even though it worked fine when in two option blocks. Do you know why this happens? @TorbjørnT. – Victor Z Apr 19 '17 at 13:22
  • 1
    The second block of options is just ignored altogether I think. – Torbjørn T. Apr 19 '17 at 13:23
  • 1
    Another way to increase node spacing is to use [outer sep=...] or [every node/.style={outer sep=...}]. – John Kormylo Apr 19 '17 at 15:12

1 Answers1

10

The tikzpicture environment only takes one optional argument. I.e., if LaTeX sees a [ after \begin{tikzpicture}, it grabs everything up to the first ] it sees, and that is the options for the environment. When you have \begin{tikzpicture}[..][..], the second bracket pair is actually just read as normal text I think, and TiKZ generally tries to suppress anything that isn't a pgf/TikZ command, so it is basically ignored.

So in summary, all the options for a tikzpicture has to be placed in the same bracket pair.

If you change your code accordingly (and remove the misplaced ? after stealth), you will find that your node distance setting works as expected.

That said, you might want to follow TeXnicians advice to use the positioning library, and say e.g. right=of instead of right of=. Some discussion on this is found in Difference between "right of=" and "right=of" in PGF/TikZ. Another reason for using the positioning library is that you can set the horizontal and vertical node distances separately, by writing node distance=Ycm and Xcm, an example is seen in the code below. As gernot mentions in a comment, in that case one would probably also want to add on grid to the node style. on grid means that the distances are calculated from the center point of the nodes.

Final note: you can put one style inside another, so if dangling node has the same style as node, with the addition of the fill, then you can say dangling node/.style={node,fill=black!30}. This reduces code duplication, and makes it easier to modify.

enter image description here

\documentclass[12pt, oneside]{amsart}
\usepackage{geometry}                       
\geometry{a4paper}

\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
  node/.style={circle, draw=black!100, thick, on grid}, % on grid added
  dangling node/.style={node, fill=black!30}
}
\begin{document}
\begin{center}
\begin{tikzpicture}[
  minimum size=5mm,
  node distance=4cm and 7cm,
  >=stealth,
  bend angle=45,
  auto
]
  % grid to easier see that the node centers line up  
  \draw [help lines] (-1,-9) grid (9,1);

  \node[node](page 1){1};
  \node[node](page 2)[right=of page 1]{22};
  \node[node](page 3)[below=of page 1]{3333};
  \node[node](page 4)[below=of page 2]{4555555};
  \node[dangling node](page 5)[below=of page 3, xshift=5mm]{5};
\end{tikzpicture}
\end{center}
\end{document}
Torbjørn T.
  • 206,688