13

This should be an easy one. Instead of specifying where the start and finish of the arrow should be, I want to specify how long the arrow should be. It might sound like the same thing, but when you have nodes of unknown lengths between several arrows, it is not the same thing, because I'm no longer sure what the exact starting point of the arrow is when there is a node before it.

This example should make it clearer. What I want is a set length for each arrow, and the nodes should simply be placed where the arrows start and end.

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
    \node[left] at (0,0) (Step 1) {Step 1};
    \draw [very thick, ->] (Step 1) -- (2,0);
    \node[right] at (2,0) (Step 2) {Step 2};
    \draw [very thick, ->] (Step 2) -- (4,0);
    \node[right] at (4,0) (Step 3) {Step 3};
\end{tikzpicture}
\end{document}

enter image description here

PS. I know there's a question with the same question as this one (How to specify the arrow length in TikZ?), but it's way more complicated than what I'm trying to accomplish here.

1 Answer 1

10

If you want to specify the length you can use ++. So, if you to draw an arrow that is 3cm long use ++(3.0cm,0) as shown in the red arrow.

If your desire is to only draw fixed length arrows and place nodes following the arrows, let tikz figure out the location of the nodes by using node (not \node) as part of the \draw (as opposed to a separate \node). Be careful to start the drawing from one end of the node (below I used east). Otherwise the center of the node is used.

enter image description here

Notes:

  • This is probably the simplest way to specify a length, as long as you want horizontal, or vertical lines. If you want to specify the length along a certain vector than you should refer to How to specify the arrow length in TikZ?

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{siunitx}

\newcommand{\FixedLengthArrow}{2,0}
\begin{document}
The black arrows are \SI{2}{\centi\meter} long:\bigskip

\begin{tikzpicture}
    \node[left] at (0,0) (Step 1) {Step 1};
    \draw [very thick, ->] (Step 1.east) -- ++(\FixedLengthArrow)
        node[right] (Step 2) {NeXT Step 2};
    \draw [very thick, ->] (Step 2.east) -- ++(\FixedLengthArrow)
        node[right]  (Step 3) {Step 3};
        
    %% To draw a line which is 3cm long
    \draw [ultra thick, red, ->] (0,-1) -- ++(3.0cm,0) node [above, pos=0.5] {This is \SI{3}{\centi\meter}};
\end{tikzpicture}
\end{document}
8
  • Playing around with this, it seems to set the length of the arrow plus any following node, since the arrow in \node[left] at (0,0) (Step 1) {Steeeeeeeep 1}; \draw [very thick, ->] (Step 1) -- ++(2cm,0) node[right] (Step 2) {Step 2}; is much shorter than the arrow in \node[left] at (0,0) (Step 1) {Step 1}; \draw [very thick, ->] (Step 1) -- ++(2cm,0) node[right] (Step 2) {Step 2};. So how do I put nodes between set arrow lengths as in my MWE?
    – Sverre
    Oct 7, 2014 at 20:19
  • @Sverre: Have updated answer to show how to do that. Oct 7, 2014 at 20:33
  • But as mentioned in my initial comment, the lengths of the arrows are the same here only because the nodes are of equal length. If you change one of the nodes, e.g. {Step 1} to {Steeeeeeep 1}, you'll see that the arrow shortens. So the length specification {2cm,0} seems to include the node.
    – Sverre
    Oct 7, 2014 at 20:38
  • 1
    @Sverre: My apologizes for missing that detail... Let me know if it is better now... Oct 7, 2014 at 20:41
  • I tried the ++(3.0cm,0) in Tikz graph but odd semicolon missing err. The edges show up right in MWE here but get super small when added to a larger document. Anyway to force the edges to stay in certain length or increase the lengths somehow?
    – hhh
    Jul 20, 2016 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .