1
\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usepackage{caption}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%

\begin{document}
  \begin{figure}
    \begin{tikzpicture}[scale=1,cap=round,>=latex]
      \usetikzlibrary{fit}
      \node[draw,fill=blue,rectangle] (v1) at (0,0) {$v_1$}   ;  
      \node[draw,fill=red,circle] (v2) at (2,0) {$v_2$} ;  
      \draw[<-] (v1) to (v2) ;  
      \node[draw,rectangle,very thick,fit=(v1) (v2)] (rec) {  }   ;
    \end{tikzpicture}
    \caption{Figure 1}
  \end{figure}

  \begin{figure}
    \begin{tikzpicture}[scale=1,cap=round,>=latex]
      \usetikzlibrary{fit}
      \node[draw,fill=blue,rectangle] (v1) at (0,0) {$v_1$}   ;  
      \node[draw,fill=red,circle] (v2) at (2,0) {$v_2$} ;  
      \draw[<-] (v1) to (v2) ;  
      \node[draw,rectangle,very thick,fit=(v1) (v2)] (rec) {  }   ;
    \end{tikzpicture}
    \caption{Figure 2}
  \end{figure}
\end{document}

In the above tex code, Figure 2 is an exact copy of Figure 1. When I comment out Figure 2, the code compiles normally. However, when I include Figure 2 in the code, the compiler complains with the following error message:

Package pgfkeys Error: I do not know the key '/tikz/fit', to which you passed '(v1) (v2)', and I am going to ignore it. Perhaps you misspelled it. [...e[draw,rectangle,very thick,fit=(v1) (v2)]]

Why does this happens how can I correct this?

  • 1
    You wrote \end{tikzpictur} a final "e" is missing – CarLaTeX Aug 23 at 3:53
  • 1
    ... and move (both) \usetikzlibrary{fit} to the preamble of the document. – Schrödinger's cat Aug 23 at 3:54
  • Moving \usetikzlibrary{fit} to the preamble solved the problem. Thanks. – fourscore Aug 23 at 4:00
1

As Schrödinger's cat commented, moving \usetikzlibrary{fit} to the preamble solved the problem.

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\usepackage{caption}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\usetikzlibrary{fit}

\begin{document}
  \begin{figure}
    \begin{tikzpicture}[scale=1,cap=round,>=latex]
      \node[draw,fill=blue,rectangle] (v1) at (0,0) {$v_1$}   ;  
      \node[draw,fill=red,circle] (v2) at (2,0) {$v_2$} ;  
      \draw[<-] (v1) to (v2) ;  
      \node[draw,rectangle,very thick,fit=(v1) (v2)] (rec) {  }   ;
    \end{tikzpicture}
    \caption{Figure 1}
  \end{figure}

  \begin{figure}
    \begin{tikzpicture}[scale=1,cap=round,>=latex]
      \node[draw,fill=blue,rectangle] (v1) at (0,0) {$v_1$}   ;  
      \node[draw,fill=red,circle] (v2) at (2,0) {$v_2$} ;  
      \draw[<-] (v1) to (v2) ;  
      \node[draw,rectangle,very thick,fit=(v1) (v2)] (rec) {  }   ;
    \end{tikzpicture}
    \caption{Figure 2}
  \end{figure}
\end{document}

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.