我正在尝试使用 Tikz 在 LaTeX 中绘制图表。不幸的是,这是我第一次使用这个包,我不太熟悉它。我使用了此网站上的示例:http://www.texample.net/tikz/examples/state-machine/
但不知为何,有些事情我还是想不通。
我现在的问题是:
解决了我想删除指向第一个节点 (A) 的箭头。我删除了单词“start”,但我不知道如何处理箭头本身。
解决了(问题通过将标签放在箭头的起点或终点来解决,否则会出现错误。)我需要 (D) 和 (G) 之间的两条边。不知何故,总是会出现错误。
我试图将这幅“图片”放入我的主要文本(连续文本)中。但是,我无法将其放入文本中,因为文本位于这幅图片的上方和下方。我尝试使用“图形”和“中心”添加 Tikz 图片,但 Tikz 图片总是漂浮在某处。
我的代码:
\documentclass[12pt,a4paper,twoside]{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\setlength{\voffset}{-28.4mm}
\setlength{\hoffset}{-1in}
\setlength{\topmargin}{20mm}
\setlength{\oddsidemargin}{25mm}
\setlength{\evensidemargin}{25mm}
\setlength{\textwidth}{160mm}
\setlength{\parindent}{0pt}
\setlength{\textheight}{235mm}
\setlength{\footskip}{20mm}
\setlength{\headsep}{50pt}
\setlength{\headheight}{0pt}
\begin{document}
\pagestyle{empty}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\begin{figure}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=4cm, semithick,initial text=,overlay,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]
\node[state](A) {$P_{000}$};
\node[state](B) [right of=A] {$P_{010}$};
\node[state](C) [above of=B] {$P_{001}$};
\node[state](D) [below of=B] {$P_{100}$};
\node[state](E) [right of=B] {$P_{101}$};
\node[state](F) [below of=E] {$P_{110}$};
\node[state](G) [above of=E] {$P_{011}$};
\node[state](H) [right of=E] {$P_{111}$};
\path (A) edge [bend angle = 10, bend left] node {$\beta_{A_2}a$} (C)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (B)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$} (D)
(B) edge [bend angle = -10, bend right] node {$\alpha_H$} (A)
edge [bend angle = 10, bend left] node[very near start] {$\beta_{A_2}a$} (G)
edge [bend angle = 10, bend left] node[very near start] {$\beta_{A_1}a$} (F)
(C) edge [bend angle = -10, bend right] node {$\alpha_{A_2}$} (A)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$} (G)
edge [bend angle = 10, bend left] node[very near end] {$\beta_{H}h$} (E)
(D) edge [bend angle = -10, bend right] node[near start] {$\alpha_{A_1}$} (A)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (F)
edge [bend angle = 10, bend left] node[very near end] {$\beta_{A_2}a$} (E)
(E) edge [bend angle = -10, bend right] node[very near start] {$\alpha_H$} (C)
edge [bend angle = -10, bend right] node[very near start] {$\alpha_{A_2}$} (D)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$} (H)
(F) edge [bend angle = -10, bend right] node {$\alpha_H$} (D)
edge [bend angle = -10, bend right] node[very near end] {$\alpha_{A_1}$} (B)
edge [bend angle = 10, bend left] node {$\beta_{A_2}a$} (H)
(G) edge [bend angle = -10, bend right] node {$\alpha_{A_1}$} (C)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (H)
edge [bend angle = -10, bend right] node[very near end] {$\alpha_{A_2}$} (B)
(H) edge [bend angle = -10, bend right] node {$\alpha_{H}$} (G)
edge [bend angle = -10, bend right] node{$\alpha_{A_1}$} (E)
edge [bend angle = -10, bend right] node{$\alpha_{A_2}$} (F)
;
\end{tikzpicture}
\end{figure}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
\end{document}
第一个屏幕截图是\begin{figure} \end{figure}
,第二个屏幕截图是\begin{center} \end{center}
。
答案1
我没有收到错误,但是输出却很糟糕。
\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}[->,shorten >=1pt,auto,node distance=4cm,
semithick,initial text=,overlay,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]
\node[state] (A) {$P_{000}$};
\node[state] (B) [right of=A] {$P_{100}$};
\node[state] (C) [above of=B] {$P_{001}$};
\node[state] (D) [below of=B] {$P_{010}$};
\node[state] (E) [right of=B] {$P_{101}$};
\node[state] (F) [below of=E] {$P_{110}$};
\node[state] (G) [above of=E] {$P_{011}$};
\node[state] (H) [right of=E] {$P_{111}$};
\path (A) edge [bend angle = 10, bend left] node {$\beta_{A_2}a$}(C)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (B)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$}(D)
(B) edge [bend angle = -10, bend right] node {$\alpha_H$} (A)
edge [bend angle = 10, bend left] node {$\beta_{A_2}a$}(E)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$}(F)
(C) edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(A)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$}(G)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (E)
(D) edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(A)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (F)
%edge [bend left, out=180,in=270] node {$\beta_{A_2}a$} (G) %ERROR
(E) edge [bend angle = -10, bend right] node {$\alpha_H$} (C)
edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(B)
edge [bend angle = 10, bend left] node {$\beta_{A_1}a$}(H)
(F) edge [bend angle = -10, bend right] node {$\alpha_H$} (D)
edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(B)
edge [bend angle = 10, bend left] node {$\beta_{A_2}a$}(H)
(G) edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(C)
edge [bend angle = 10, bend left] node {$\beta_{H}h$} (H)
(H) edge [bend angle = -10, bend right] node {$\alpha_{H}$} (G)
edge [bend angle = -10, bend right] node {$\alpha_{A_1}$}(E)
edge [bend angle = -10, bend right] node {$\alpha_{A_2}$}(F)
;
\path (D) edge [bend angle = 10, bend left] (G);
\path (G) edge [bend angle = 10, bend left] (D);
\end{tikzpicture}
\end{document}
答案2
和 @marmot 一样,代码可以编译,但看起来不太好,因为缺失的边缘占据了图片的主导地位。一种解决方法是使缺失的边缘dashed
和gray
,并调整弯曲角度,这样你就会得到:
我还会做出其他一些改变:
>=stealth'
您通过提供和指定了两个不同的箭头>=latex
。由于第二个规范“获胜”,我放弃了隐形箭头- 由于所有边都非常相似,我会将它们放在一个
\foreach
循环中,将端点和标签作为循环的“参数”。这样可以简化代码,使其更易于阅读和理解。此外,如果您使用单独的\path
语句绘制边,那么它们都会使用 ,因此不需要太多思考。从到的bend angle=10, bend left
边需要单独绘制,因为它们是虚线。(D)
(G)
修改后的代码如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,automata}
\usepackage[latin1]{inputenc}
\begin{document}
\begin{tikzpicture}[->,shorten >=1pt,auto,node distance=4cm,
semithick,initial text=,>=latex]
\tikzstyle{every state}=[fill=lightgray,draw=none,text=black]
\node[initial,state] (A) {$P_{000}$};
\node[state] (B) [right of=A] {$P_{100}$};
\node[state] (C) [above of=B] {$P_{001}$};
\node[state] (D) [below of=B] {$P_{010}$};
\node[state] (E) [right of=B] {$P_{101}$};
\node[state] (F) [below of=E] {$P_{110}$};
\node[state] (G) [above of=E] {$P_{011}$};
\node[state] (H) [right of=E] {$P_{111}$};
% loop through and draw the edges
\foreach \X/\Y/\A/\a in {
A/C/A_2/a,% 000 <-> 001
C/G/A_1/a,% 001 <-> 011
G/H/H /h,% 011 <-> 111
C/E/H /h,% 001 <-> 101
A/B/H /h,% 000 <-> 100
B/E/A_2/a,% 100 <-> 101
E/H/A_1/a,% 101 <-> 111
A/D/A_2/a,% 000 <-> 010
B/F/A_1/a,% 100 <-> 110
D/F/H /h,% 100 <-> 110
F/H/A_2/a% 100 <-> 110
}{
\path (\X) edge[->, bend angle=10, bend left]
node{$\beta_{\A}\a$}(\Y);
\path (\Y) edge[->, bend angle=10, bend left]
node{$\alpha_{\A}$}(\X);
}
\path[dashed,gray] (D) edge[->, bend angle=22, bend left]
node{$\beta_{A_2}a$}(G);
\path[dashed,gray] (G) edge[->, bend angle=22, bend left]
node{$\alpha_{A}$}(D);
\end{tikzpicture}
\end{document}
不幸的是,似乎无法选择弯曲角度来避开所有其他标签,但我认为这样bend angle=22
看起来还不错。另一种选择是将这些边缘放在图片的“外面”,但我也不喜欢这种外观。