我有以下用于生成图表的代码。如何像 IPE 一样使生成的 PDF 仅与图形一样大?
\documentclass[landscape]{article}
\usepackage{standalone}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{graphicx}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\begin{document}
\begin{figure}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=3 cm,scale=1,transform shape,line width=0.25mm]
\node[initial,state,fill=green!20,initial text={},line width=0.25mm] (A) {$q_{000}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (B) [right of=A] {$q_{100}$};
\node[state,fill=blue!20,line width=0.25mm] (C) [right of=B] {$q_{010}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (D) [right of=C] {$q_{110}$};
\node[state,fill=blue!20,line width=0.25mm] (E) [below of=A] {$q_{001}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (F) [right of=E] {$q_{101}$};
\node[state,fill=blue!20,line width=0.25mm] (G) [right of=F] {$q_{011}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (H) [right of=G] {$q_{111}$};
\path[->,black] (A) edge [loop above] node [align=center] {$ 0$} (A)
(B) edge [above] node [align=center] {$0$} (A)
(C) edge [above] node [align=center] {$0$} (B)
(D) edge [ above, bend right ] node [align=center] {$0$} (B)
(A) edge [ left ] node [align=center] {$1$} (E)
(B) edge [ above ] node [align=center,xshift=-5] {$1$} (E)
(E) edge [ bend left = 6, above ] node [align=center] {$0$} (C)
(C) edge [ bend right = 10, above] node [align=center,xshift=-8,yshift=-5] {$1$} (F)
(F) edge [ bend right = 10, below ] node [align=center,xshift=8,yshift=5] {$0$} (C)
(F) edge [ ] node [align=center] {$1$} (G)
(G) edge [ ] node [align=center] {$1$} (H)
(H) edge [loop right] node [align=center] {$ 1$} (H)
(H) edge [ right ] node [align=center] {$0$} (D)
(G) edge [ below ] node [align=center,xshift=5] {$0$} (D)
(D) edge [ bend left = 5,above] node [align=center,yshift=3] {$1$} (F)
(E) edge [ below, bend right ] node [align=center] {$1$} (G);
\end{tikzpicture}
\end{figure}
\end{document}
已更新工作代码。谢谢您的帮助!
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsthm}
\begin{document}
\begin{tikzpicture}[>=stealth',shorten >=1pt,auto,node distance=3 cm,scale=1,transform shape,line width=0.25mm]
\node[initial,state,fill=green!20,initial text={},line width=0.25mm] (A) {$q_{000}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (B) [right of=A] {$q_{100}$};
\node[state,fill=blue!20,line width=0.25mm] (C) [right of=B] {$q_{010}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (D) [right of=C] {$q_{110}$};
\node[state,fill=blue!20,line width=0.25mm] (E) [below of=A] {$q_{001}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (F) [right of=E] {$q_{101}$};
\node[state,fill=blue!20,line width=0.25mm] (G) [right of=F] {$q_{011}$};
\node[state,accepting,fill=red!20,line width=0.25mm] (H) [right of=G] {$q_{111}$};
\path[->,black] (A) edge [loop above] node [align=center] {$ 0$} (A)
(B) edge [above] node [align=center] {$0$} (A)
(C) edge [above] node [align=center] {$0$} (B)
(D) edge [ above, bend right ] node [align=center] {$0$} (B)
(A) edge [ left ] node [align=center] {$1$} (E)
(B) edge [ above ] node [align=center,xshift=-5] {$1$} (E)
(E) edge [ bend left = 6, above ] node [align=center] {$0$} (C)
(C) edge [ bend right = 10, above] node [align=center,xshift=-8,yshift=-5] {$1$} (F)
(F) edge [ bend right = 10, below ] node [align=center,xshift=8,yshift=5] {$0$} (C)
(F) edge [ ] node [align=center] {$1$} (G)
(G) edge [ ] node [align=center] {$1$} (H)
(H) edge [loop right] node [align=center] {$ 1$} (H)
(H) edge [ right ] node [align=center] {$0$} (D)
(G) edge [ below ] node [align=center,xshift=5] {$0$} (D)
(D) edge [ bend left = 5,above] node [align=center,yshift=3] {$1$} (F)
(E) edge [ below, bend right ] node [align=center] {$1$} (G);
\end{tikzpicture}
\end{document}
答案1
删除figure
环境,删除\usepackage{standalone}
并更改为\documentclass{standalone}
。
不相关的评论。
请注意,虽然它仍然有效,但该
arrows
库已被弃用,取而代之的是arrows.meta
。但这确实有不同的语法,请参阅手册。不推荐使用该
of=
语法,而建议加载positioning
库并说=of
,请参阅PGF/TikZ 中“right of=”和“right=of”之间的区别\usepackage{tikz}
也会加载pgf
(这并不奇怪)和graphicx
,因此加载后两个包是多余的。您没有使用此图中的任何包的功能ams
,因此也可以将它们删除。在这种情况下,所有这些都
align=center
实际上没有任何作用。就我个人而言,我会使用 eg
above left
而不是xshift
and/oryshift
。对于有许多具有相同选项的元素的情况,添加样式可以使事情变得更整洁且更容易更改。
完整代码
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,automata,positioning}
\begin{document}
\begin{tikzpicture}[
>={Stealth[round]},shorten >=1pt,
auto,node distance=2.5cm,
transform shape,line width=0.25mm,
StartNode/.style={initial,state,fill=green!20,initial text={},line width=0.25mm},
TheRedOnes/.style={state,accepting,fill=red!20,line width=0.25mm},
TheBlueOnes/.style={state,fill=blue!20,line width=0.25mm}
]
\node[StartNode] (A) {$q_{000}$};
\node[TheRedOnes] (B) [right=of A] {$q_{100}$};
\node[TheBlueOnes] (C) [right=of B] {$q_{010}$};
\node[TheRedOnes] (D) [right=of C] {$q_{110}$};
\node[TheBlueOnes] (E) [below=of A] {$q_{001}$};
\node[TheRedOnes] (F) [right=of E] {$q_{101}$};
\node[TheBlueOnes] (G) [right=of F] {$q_{011}$};
\node[TheRedOnes] (H) [right=of G] {$q_{111}$};
\path[->,black] (A) edge [loop above] node {$ 0$} (A)
(B) edge [above] node {$0$} (A)
(C) edge [above] node {$0$} (B)
(D) edge [ above, bend right ] node {$0$} (B)
(A) edge node [left] {$1$} (E)
(B) edge node [above left] {$1$} (E)
(E) edge [ bend left = 6 ] node [above] {$0$} (C)
(C) edge [ bend right = 10] node [left=3pt] {$1$} (F)
(F) edge [ bend right = 10] node [right=3pt] {$0$} (C)
(F) edge node {$1$} (G)
(G) edge node {$1$} (H)
(H) edge [loop right] node {$ 1$} (H)
(H) edge node [ right ] {$0$} (D)
(G) edge node [below right] {$0$} (D)
(D) edge [ bend left = 5 ] node [above] {$1$} (F)
(E) edge [ bend right ] node [below] {$1$} (G);
\end{tikzpicture}
\end{document}