7

Is it possible to use the node distance as a variable while expressing coordinates of new nodes with Calc and Positioning libraries? What I want to have is everything within a single piece of code for tikz picture.

This is what I have now.

\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows,calc}

\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=0.5pt,auto,node distance=2cm,semithick,on grid]

  \tikzstyle{every state}=[minimum size=20pt, fill=none,draw=black]
  \node[state,label=center:$a$,inner sep=0pt] (a) {};
  \node[state,label=center:$b$] (b) at ($(a) + sqrt(1/3)*4*(0:1cm) $) {};
  \node[state,label=center:$c$] (c) at ($(a) + sqrt(1/3)*4*(-60:1cm)$) {};

  \path[->] (a) edge node {$1$} (b)
            (c) edge node[right] {$1$} (b)
      (b) edge [loop right] node[above] {$0,1$} (b);
   \path[shorten <=0.5pt,<->]  (a)  edge node[left]{$0$} (c);

\end{tikzpicture}
\end{document}

I would like to be able to change the below expression with something like:

$(a) + sqrt(1/3)*2*(1:\nodedistance)$

Then if I would be copying the code elsewhere, such as a presentations, I would be able to change all these distances at once. Is that possible? If not, introducing a variable in the first line of the TikZ code, and using it afterwards instead the "\nodedistance", would also do.

Sorry if it's a dull question, I'm pretty new to TikZ.

  • 1
    Welcome to TeX.SE! This is not at all a dumb question. Are you aware of this post, I guess that /tikz/node distance/.append code={...} does what you want. This allows you to store the distance in a key that you can use. – marmot Feb 3 at 0:56
6

AFAIK you cannot simply read off the value of /tikz/node distance. However, in this great answer by @Jake there is a simple way to amend node distance by a pgf key that you can use.

\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows,calc}
\tikzset{node distance/.append code={
        \pgfkeyssetvalue{/tikz/node distance value}{#1}
    }}
\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=0.5pt,auto,node
distance=2cm,semithick,on grid,
every state/.style={minimum size=20pt, fill=none,draw=black}]
  \node[state,label=center:$a$,inner sep=0pt] (a) {};
  \node[state,label=center:$b$] (b) at ($(a) + sqrt(4/3)*(0:\pgfkeysvalueof{/tikz/node distance value}) $) {};
  \node[state,label=center:$c$] (c) at ($(a) + sqrt(4/3)*(-60:\pgfkeysvalueof{/tikz/node distance value})$) {};

  \path[->] (a) edge node {$1$} (b)
            (c) edge node[right] {$1$} (b)
      (b) edge [loop right] node[above] {$0,1$} (b);
   \path[shorten <=0.5pt,<->]  (a)  edge node[left]{$0$} (c);
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thank you very much for your explanation, I haven't seen Jake's post before, or at least I haven't noticed that very detail I needed. Yes, this gives me exactly what I wanted - "the way it was meant to be done". – gari Feb 3 at 9:37
  • 2
    @gari please don't forget to accept this answer. – Hafid Boukhoulda Feb 3 at 16:58
4

You can create a macro \def\nodedistance{1cm} at the begining of the tikzpicture . Later if needed this macro could be redefined .

\documentclass[12pt, a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{automata, positioning, arrows,calc}

\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=0.5pt,auto,semithick,on grid]

\def\nodedistance{1cm} % <------------ 

  \tikzstyle{every state}=[minimum size=20pt, fill=none,draw=black]
  \node[state,label=center:$a$,inner sep=0pt] (a) {};
  \node[state,label=center:$b$] (b) at ($(a) + sqrt(1/3)*4*(0:\nodedistance) $) {};
  \node[state,label=center:$c$] (c) at ($(a) + sqrt(1/3)*4*(-60:\nodedistance)$) {};

  \path[->] (a) edge node {$1$} (b)
            (c) edge node[right] {$1$} (b)
      (b) edge [loop right] node[above] {$0,1$} (b);
   \path[shorten <=0.5pt,<->]  (a)  edge node[left]{$0$} (c);

\end{tikzpicture}

\begin{tikzpicture}[>=stealth',shorten >=0.5pt,auto,semithick,on grid]

\def\nodedistance{2cm} % <----------------

  \tikzstyle{every state}=[minimum size=20pt, fill=none,draw=black]
  \node[state,label=center:$a$,inner sep=0pt] (a) {};
  \node[state,label=center:$b$] (b) at ($(a) + sqrt(1/3)*4*(0:\nodedistance) $) {};
  \node[state,label=center:$c$] (c) at ($(a) + sqrt(1/3)*4*(-60:\nodedistance)$) {};

  \path[->] (a) edge node {$1$} (b)
            (c) edge node[right] {$1$} (b)
      (b) edge [loop right] node[above] {$0,1$} (b);
   \path[shorten <=0.5pt,<->]  (a)  edge node[left]{$0$} (c);

\end{tikzpicture}
\end{document}

enter image description here

  • 1
    Thanks, this is quite elegant way to do it. While marmot's reply gives me the exact thing I needed, your reply offers more - in case I might want to have more than one default distances. Then one could do it like this - manually. – gari Feb 3 at 9:41
  • 2
    You can also use a length register: \newlength{\nodedistance} and \nodedistance=1cm. – John Kormylo Feb 3 at 19:07
  • Thanks, that's also a nice solution. I'll keep that in mind too. – gari Feb 3 at 20:24

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.