\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}
在上面的 tex 代码中,图 2 是图 1 的精确副本。当我注释掉图 2 时,代码可以正常编译。但是,当我在代码中包含图 2 时,编译器会发出以下错误消息:
软件包 pgfkeys 错误:我不知道您传递了“(v1) (v2)”的键“/tikz/fit”,我将忽略它。也许您拼错了。[...e[draw,rectangle,very thick,fit=(v1) (v2)]]
为什么会发生这种情况?我该如何解决?
答案1
正如薛定谔的猫所评论的那样,转向\usetikzlibrary{fit}
序言解决了这个问题。
\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}