Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following TikZ code:

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows,automata} 
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,node distance=2cm,>=stealth',thick]
        \node[state] (1) {$1$};
        \node[state,draw=none] (d1) [left of=1] {};
        \draw [->] (1) to[loop above] node[auto] {} (1);
        \draw [->] (1) to[bend left] node[auto] {} (d1);
    \end{tikzpicture}
\end{document}

which produces this image:

TikZ output

Notice, that I've used a hidden node (d1) above in order to produce the nice arrow going to the left.

The sadness of this is that now I have a big circle-sized empty space.

I'd like to keep the arrow, I'd like the arrow to look the same, but I'd like to get rid of that empty space.

Is there a way to draw the arrow without introducing the invisible node? Or is there a way to crop the figure? What's an elegant way to solve this problem?

Thanks!

share|improve this question

5 Answers 5

up vote 17 down vote accepted

You can use a \coordinate instead of a \node:

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows,automata} 
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,node distance=2cm,>=stealth',thick]
        \node[state] (1) {$1$};
        \coordinate[left of=1] (d1);
        \draw [->] (1) to[loop above] node[auto] {} (1);
        \draw [->] (1) to[bend left] node[auto] {} (d1);
    \end{tikzpicture}
\end{document}

enter image description here

To compensate for the arrow tip shortening, you could use

   \draw [->,shorten >=0pt] (1) to[bend left] node[auto] {} (d1);

in the above code.

share|improve this answer
    
That's a very long arrow going to the left. Is there a way to shorten it (perhaps this was the latter part of your comment, but it doesn't seem to work). –  Richard Apr 16 '12 at 19:39
    
@Richard: Sure, you can use something like \coordinate (d1) at (-1,0); but since you said: "...I'd like the arrow to look the same...", I thought you wanted to keep the original lengths. –  Gonzalo Medina Apr 16 '12 at 19:44
1  
I think your solution leads to an extended arrow, @GonzaloMedina, because the arrow will go to the coordinate which is defined as "left of" d1. This coordinate will be the center of the invisible circular node I'd defined, rather than the edge. Therefore, in your code, the arrow becomes longer. –  Richard Apr 16 '12 at 20:10

You could also use the ++ option to specify go left 2 centimeters from the last position :

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows,automata} 
\begin{document}
    \begin{tikzpicture}[thick]
        \node[state] (1) {$1$};
        \draw [->] (1) to[loop above] node[auto] {} (1);
        \draw [->] (1) to[bend left] node[auto] {} ++ (-2,0);
    \end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

A compact code is

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows,automata} 
\begin{document}
    \begin{tikzpicture}[thick]
        \node[state] (1) {$1$};
        \draw [->] (1) edge   [loop above]  ()
                       edge   [bend left]   ++ (-2,0);
    \end{tikzpicture}
\end{document}
share|improve this answer

The actual problem here is, that the invisible node (draw=none) is only used to place the arrow as if there is an actual node. Which works fine but also expands the bounding box.

But there is the special pgfinterruptboundingbox environment. From the PGF manual, 69.3.2 “Graphic Scope Environments”, p. 568f.:

This environment temporarily interrupts the computation of the bounding box and sets up a new bounding box. At the beginning of the environment the old bounding box is saved and an empty bounding box is installed. After the environment the original bounding box is reinstalled as if nothing has happened.


To quote from the question:

I'd like to keep the arrow,

Check.

I'd like the arrow to look the same, but I'd like to get rid of that empty space.

Double-check.

Is there a way to draw the arrow without introducing the invisible node?

Yes, see the other answers.

Or is there a way to crop the figure?

Yes, there is an additional way: the \clip (\path[clip]) command/path operation, but that has two disadvantages:

  1. It clips only following material.
  2. It needs specifying a path that is to be clipped.

What's an elegant way to solve this problem?

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,automata}
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,node distance=2cm,>=stealth',thick]
        \node[state] (1) {$1$};
        \begin{pgfinterruptboundingbox}
            \node[state,draw=none] (d1) [left of=1] {};
        \end{pgfinterruptboundingbox}
        \draw [->] (1) to[loop above] node[auto] {} (1);
        \draw [->] (1) to[bend left]  node[auto] {} (d1);
    \end{tikzpicture}
\end{document}

Output

(Note, that the arrow is shorten by 1pt and the resulting .png has an additional border of 2pt, hence the arrow does not touch the picture’s margin.)

with draw=none

enter image description here

without draw=none

enter image description here

share|improve this answer

In the case of your MWE, a simple solution uses the overlay option:

\documentclass{standalone} 
\usepackage{tikz}
\usetikzlibrary{arrows,automata} 
\begin{document}
    \begin{tikzpicture}[shorten >=1pt,node distance=2cm,>=stealth',thick]
        \node[state] (1) {$1$};
        \node[state,draw=none,overlay] (d1) [left of=1] {};
        \draw [->] (1) to[loop above] node[auto] {} (1);
        \draw [->] (1) to[bend left] node[auto] {} (d1);
    \end{tikzpicture}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.