3

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}
  • 1
    Try the syntax right=10mm of page 1 (instead of right of). – TeXnician Apr 19 '17 at 9:12
  • Hmm, I tried it, but latex gave me the error that 'of' was not defined. – Victor Z Apr 19 '17 at 9:27
  • 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 9:39
  • 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
3

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}
  • You should probably add the option on grid such that the nodes really line up. – gernot Apr 19 '17 at 23:24
  • @gernot Fair point, edited. – Torbjørn T. Apr 20 '17 at 6:13

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.