83

I've got this Tikz picture and I'd like to increase the horizontal distance between the nodes while keeping the vertical distance unchanged. I know how to do it by placing the nodes manually using coordinates, but I'd rather not do it if it can be avoided.

picture showing the nodes

Here's the code:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  [align=center,node distance=2cm]
  \node[label=above:A] (A)                      
       {(1)};
  \node[label=above:B1] (B1) [above right of=A]
       {($m+1$)};
  \node[label=above:B2] (B2) [below right of=A]
       {($m+1$)};
  \node[label=above:C] (C)  [below right of=B1]
       {($2m-1$)};
\end{tikzpicture}
\end{document}

Any help appreciated.

4
  • Instead of specifying coordinates you could use xshift=<length> to indicate the amount of the shift. Apr 8, 2012 at 22:39
  • @PeterGrill: nice suggestion.
    – Ernest A
    Apr 8, 2012 at 22:50
  • 8
    you can also specify below(or above) and right (or left) dimensions separately like above right=0.7cm and 1cm of A. You have to use \usetikzlibrary{positioning} for this.
    – user11232
    Apr 8, 2012 at 23:13
  • @HarishKumar: this could be a solution.
    – Ernest A
    Apr 8, 2012 at 23:47

4 Answers 4

82

You can specify the relative x and y positions separately using the tikz library positioning

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  [align=center,node distance=2cm]  %< no need of this global node separation
  \node[label=above:A] (A)                      
       {(1)};
  \node[label=above:B1] (B1) [above right=0.7cm and 4cm of A]
       {($m+1$)};
  \node[label=above:B2] (B2) [below right=0.7cm and 4cm of A]
       {($m+1$)};
  \node[label=above:C] (C)  [below right=0.7cm and 4cm of B1]
       {($2m-1$)};
\end{tikzpicture}
\end{document}

enter image description here

1
30

The first method uses positioning and node distance=2cm and 4cm] but I don't appreciate it.

/tikz/node distance=⟨shifting part⟩ (no default, initially 1cm and 1cm) The value of this key is used as ⟨shifting part⟩ is used if and only if a ⟨of-part⟩ is present, but no ⟨shifting part⟩.

and

When the ⟨shifting part⟩ is of the form ⟨number or dimension⟩ and ⟨number or dimension⟩, it has (essentially) the effect of shifting the node vertically upwards by the first ⟨number or dimension⟩ and to the left by the second.

I think it's strange to shift first vertically and I prefer to make a shift manually by myself.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  [align=center,node distance=2cm and 4cm] 
  \node[label=above:A]  (A)                        {(1)};
  \node[label=above:B1] (B1) [above right= of A]   {($m+1$)};
  \node[label=above:B2] (B2) [below right= of A]   {($m+1$)};
  \node[label=above:C]  (C)  [below right= of B1]  {($2m-1$)};
\end{tikzpicture}
\end{document} 

I prefer +(2, 2) and ++(2, 2) it's like above right and it's more flexible. But It's fine to have a lot of possibilities !!!

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture} [align=center]
  \path  (0, 0)   node[label=above:A]   (A)  {(1)}
        +(2, 2)   node[label=above:B1]  (B1) {($m+1$)}
       ++(2,-2)   node[label=above:B2]  (B2) {($m+1$)}
       ++(2, 2)   node[label=above:C]   (C)  {($2m-1$)};
\end{tikzpicture} 

\begin{tikzpicture} [align=center,xscale=2]  % or x=2cm
  \path  (0, 0)   node[label=above:A]   (A)  {(1)}
        +(2, 2)   node[label=above:B1]  (B1) {($m+1$)}
       ++(2,-2)   node[label=above:B2]  (B2) {($m+1$)}
       ++(2, 2)   node[label=above:C]   (C)  {($2m-1$)};
\end{tikzpicture} 

\end{document} 

enter image description here

14

As Harish mentioned, you can use the positioning library and adjust horizontal and vertical position.

Another way, allowing you to specify distances for horizontal and vertical distances, is to use matrices. With this you can set the row and column distances. The code, for you diagram, is

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}[align=center]

\matrix[matrix of nodes,row sep=0.5cm,column sep=2cm]{
&|[label=above:B1]| ($m+1$)&\\
|[label=above:A]| (1)&&|[label=above:C]| ($2m-1$)\\
&|[label=above:B2]| ($m+1$)&\\
};

\end{tikzpicture}

\end{document}

The result is

enter image description here

0

I reached this question while trying to fix the inter-nodal distances between nodes in my state transition graph.

The tutorial here gives minimal examples of STGs and suggests the below \tikzset configuration in the preamble of the source,

\tikzset{
->,  % makes the edges directed
>=stealth', % makes the arrow heads bold
node distance=3cm, % specifies the minimum distance between two nodes. Change if necessary.
every state/.style={thick, fill=gray!10}, % sets the properties for each ’state’ node
initial text=$ $, % sets the text that appears on the start arrow
}

This worked well for me.

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.