几天前我已经发布了这个问题,但我仍然无法完成算法。
这是迄今为止未完成的工作
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains}
\begin{document}
% Define block styles
\tikzset{
desicion/.style={
diamond,
draw,
text width=4em,
text badly centered,
inner sep=0pt
},
block/.style={
rectangle,
draw,
text width=10em,
text centered,
rounded corners
},
cloud/.style={
draw,
ellipse,
minimum height=2em
},
descr/.style={
fill=white,
inner sep=2.5pt
},
connector/.style={
-latex,
font=\scriptsize
},
rectangle connector/.style={
connector,
to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },
pos=0.5
},
rectangle connector/.default=-2cm,
straight connector/.style={
connector,
to path=--(\tikztotarget) \tikztonodes
}
}
\begin{tikzpicture}
\matrix (m)[matrix of nodes, column sep=2cm,row sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]{
|[block]| {Start} & \\
|[block]| {Assume that $a=c$ the optimilalty cretierin given by } & \\
|[desicion]| {Locally optimal} & \\
|[block]| {Assume that $a=d$ the optimilalty cretierin given by} & \\
|[desicion]| {Locally optimal} & |[block]| {Stop} \\
|[block]| {Assume that $a=e$ the optimilalty cretierin given by} & |[desicion]| {Locally optimal} \\
|[desicion]| {Locally optimal} & |[block]| {Assume that $a=k$ the optimilalty cretierin given by} \\
|[block]| {Assume that $a=f$ the optimilalty cretierin given by} & |[desicion]| {Locally optimal} \\
};
\path [>=latex,->] (m-1-1) edge (m-2-1);
\path [>=latex,->] (m-2-1) edge (m-3-1);
\path [>=latex,->] (m-3-1) edge (m-4-1);
\path [>=latex,->] (m-4-1) edge (m-5-1);
\path [>=latex,->] (m-5-1) edge (m-6-1);
\path [>=latex,->] (m-6-1) edge (m-7-1);
\path [>=latex,->] (m-7-1) edge (m-8-1);
\path [>=latex,->] (m-8-1) edge (m-8-2);
\path [>=latex,->] (m-8-2) edge (m-7-2);
\path [>=latex,->] (m-7-2) edge (m-6-2);
\path [>=latex,->] (m-6-2) edge (m-5-2);
\end{tikzpicture}
\end{document}
我该如何根据第一张图所示的流程图修复路径?
答案1
尽量foreach
使用循环画线,line
新增一种样式。
代码
\documentclass[a4paper,10pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains,calc}
\begin{document}
% Define block styles
\tikzset{
desicion/.style={
diamond,
draw, thick,
text width=4em,
text badly centered,
inner sep=0pt
},
block/.style={
rectangle,
draw, thick,
text width=10em,
text centered,
rounded corners
},
cloud/.style={
draw,
ellipse,
minimum height=2em
},
descr/.style={
fill=white,
inner sep=2.5pt
},
connector/.style={
-latex,
font=\scriptsize
},
rectangle connector/.style={
connector,
to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },
pos=0.5
},
rectangle connector/.default=-2cm,
straight connector/.style={
connector,
to path=--(\tikztotarget) \tikztonodes
},
line/.style={>=latex,->,thick}
}
\begin{tikzpicture}
\matrix (m)[matrix of nodes, column sep=3cm,row sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]
{
|[block]| {Start} & & \\
|[block]| {Assume that $a=c$ the optimilalty cretierin given by } & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=d$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=e$ the optimilalty cretierin given by} & |[block]| {$A=c^2$ \\ $A=b^2$} & |[block]| {Globsl \\ Optimal \\ Configuration} \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=f$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & |[block]| {Stop} \\
|[block]| {Assume that $a=k$ the optimilalty cretierin given by} & |[desicion]| {Locally optimal} & \\
};
\foreach \f/\t[evaluate=\f as \t using int(\f+1)] in {1,2,3,4,5,6,7,8,9}{
\path [line] (m-\f-1) edge (m-\t-1);
}
\path [line,red] (m-10-1) edge (m-10-2);
\draw[line,red] (m-10-2) -- (m-6-2) node[pos=0.3,right,text=black]{ Yes, Pass (a,c)};
\draw [line] (m-6-2) --node[midway,below,text=black]{Yes, Pass (a,c)} node[midway,above,text=black]{Step 6} (m-6-3);
\path [line,red] (m-6-3) edge (m-9-3);
\draw [line,red] (m-10-2) -| (m-9-3);
\foreach \f/\l[evaluate=\f as \t using int(\f+1)] in {3/a,5/b,7/c,9/d}{
\draw [line,red] (m-\f-1.east) --node[midway,above,text=black]{Yes, Pass (a,c)} ++ (2.5cm,0)coordinate[](\l);
\draw [line,red] (m-\f-1.east) -| ([xshift=1.5cm]m-\t-1.north);
}
\node[xshift=-2cm] at (m-3-1){Step 1(4)};
\node[xshift=-2cm] at (m-5-1){Step 2 (7)};
\node[xshift=-2cm] at (m-7-1){Step 3 (9)};
\node[xshift=-2cm] at (m-9-1){Step 4 (3)};
\node[xshift=-2cm,above] at (m-10-2){Step 5};
\draw [>=latex,-,red,thick] (a) --(d);
\draw [line,red] ($(a)!0.5!(d)$) -- (m-6-2);
\end{tikzpicture}
\end{document}
编辑:应 OP 的要求,我在report
课堂上进行了案例分析,当然,还提供了 OP 所需的信息。结果如下
相关代码
\documentclass[a4paper,11pt]{report}
\usepackage[margin=1cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains,calc}
\begin{document}
% Define block styles
\tikzset{
desicion/.style={
diamond,
draw, thick,
text width=4em,
text badly centered,
inner sep=0pt
},
block/.style={
rectangle,
draw, thick,
text width=10em,
text centered,
rounded corners
},
cloud/.style={
draw,
ellipse,
minimum height=2em
},
descr/.style={
fill=white,
inner sep=2.5pt
},
connector/.style={
-latex,
font=\scriptsize
},
rectangle connector/.style={
connector,
to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },
pos=0.5
},
rectangle connector/.default=-2cm,
straight connector/.style={
connector,
to path=--(\tikztotarget) \tikztonodes
},
line/.style={>=latex,->,thick}
}
\begin{figure}[!htpb]
\begin{tikzpicture}
\matrix (m)[matrix of nodes, column sep=3cm,row sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]
{
|[block]| {Start} & & \\
|[block]| {Assume that $a=c$ the optimilalty cretierin given by } & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=d$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=e$ the optimilalty cretierin given by} & |[block]| {$A=c^2$ \\ $A=b^2$} & |[block]| {Globsl \\ Optimal \\ Configuration} \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=f$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & |[block]| {Stop} \\
|[block]| {Assume that $a=k$ the optimilalty cretierin given by} & |[desicion]| {Locally optimal} & \\
};
\foreach \f/\t[evaluate=\f as \t using int(\f+1)] in {1,2,3,4,5,6,7,8,9}{
\draw [line] (m-\f-1) -- node[midway,right]{No} (m-\t-1);
}
\path [line,red] (m-10-1) edge (m-10-2);
\draw[line,red] (m-10-2) -- (m-6-2) node[pos=0.3,right,text=black]{ Yes, Pass (a,c)};
\draw [line] (m-6-2) --node[midway,below,text=black]{Yes, Pass (a,c)} node[midway,above,text=black]{Step 6} (m-6-3);
\path [line,red] (m-6-3) edge (m-9-3);
\draw [line,red] (m-10-2) -| (m-9-3);
\foreach \f/\l[evaluate=\f as \t using int(\f+1)] in {3/a,5/b,7/c,9/d}{
\draw [line,red] (m-\f-1.east) --node[midway,above,text=black]{Yes, Pass (a,c)} ++ (2.5cm,0)coordinate[](\l);
\draw [line,red] (m-\f-1.east) -| ([xshift=1.5cm]m-\t-1.north);
}
\node[xshift=4cm] at (m-1-1) {A,b,c,d,k given};
\node[xshift=-2cm] at (m-3-1){Step 1(4)};
\node[xshift=-2cm] at (m-5-1){Step 2 (7)};
\node[xshift=-2cm] at (m-7-1){Step 3 (9)};
\node[xshift=-2cm] at (m-9-1){Step 4 (3)};
\node[xshift=-2cm,above] at (m-10-2){Step 5};
\node[above,xshift=1.5cm] at (m-10-2){No};
\draw [>=latex,-,red,thick] (a) --(d);
\draw [line,red] ($(a)!0.5!(d)$) -- (m-6-2);
\end{tikzpicture}
\caption{Your caption here}
\end{figure}
\end{document}
编辑 2:最后,经过几次讨论,就是这样。
代码
\documentclass[a4paper,11pt]{report}
\usepackage[margin=1cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning,chains,calc}
\begin{document}
% Define block styles
\tikzset{
desicion/.style={
diamond,
draw, thick,
text width=4em,
text badly centered,
inner sep=0pt
},
block/.style={
rectangle,
draw, thick,
text width=9em,
text centered,
rounded corners
},
cloud/.style={
draw,
ellipse,
minimum height=2em
},
descr/.style={
fill=white,
inner sep=2.5pt
},
connector/.style={
-latex,
font=\scriptsize
},
rectangle connector/.style={
connector,
to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },
pos=0.5
},
rectangle connector/.default=-2cm,
straight connector/.style={
connector,
to path=--(\tikztotarget) \tikztonodes
},
line/.style={>=latex,->,thick}
}
\begin{figure}[!htpb]
\begin{tikzpicture}
\matrix (m)[matrix of nodes, column sep=3cm, row sep=8mm, align=center, nodes={rectangle,draw, anchor=center} ]
{
|[block]| {Start} & & \\
|[block]| {Assume that $a=c$ the optimilalty cretierin given by } & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=d$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=e$ the optimilalty cretierin given by} & |[block]| {$A=c^2$ \\ $A=b^2$} & |[block]| {Globsl \\ Optimal \\ Configuration} \\
|[desicion]| {Locally optimal} & & \\
|[block]| {Assume that $a=f$ the optimilalty cretierin given by} & & \\
|[desicion]| {Locally optimal} & & |[block]| {Stop} \\
|[block]| {Assume that $a=k$ the optimilalty cretierin given by} & |[desicion]| {Locally optimal} & \\
};
\foreach \f/\t[evaluate=\f as \t using int(\f+1)] in {3,4,5,6,7,8,9}{
\draw [line] (m-\f-1) -- node[midway,right]{No} (m-\t-1);
}
\node[above,xshift=1.5cm] at (m-10-2){No};
\foreach \f/\t[evaluate=\f as \t using int(\f+1)] in {1,2}{
\draw [line] (m-\f-1) -- (m-\t-1);
}
\path [line,red] (m-10-1) edge (m-10-2);
\path [line,red] (m-6-3) edge (m-9-3);
\draw [line,red] (m-10-2) -| (m-9-3);
\draw [line,red] (m-3-1.east) --node[midway,above,text=black]{Yes, Pass (a1,c1)} ++ (2.8cm,0)coordinate[](a);
\draw [line,red] (m-3-1.east) -| ([xshift=1.5cm]m-4-1.north);
\draw [line,red] (m-5-1.east) --node[midway,above,text=black]{Yes, Pass (a2,c2)} ++ (2.8cm,0)coordinate[](b);
\draw [line,red] (m-5-1.east) -| ([xshift=1.5cm]m-6-1.north);
\draw [line,red] (m-7-1.east) --node[midway,above,text=black]{Yes, Pass (a3,c3)} ++ (2.8cm,0)coordinate[](c);
\draw [line,red] (m-7-1.east) -| ([xshift=1.5cm]m-8-1.north);
\draw [line,red] (m-9-1.east) --node[midway,above,text=black]{Yes, Pass (a4,c4)} ++ (2.8cm,0)coordinate[](d);
\draw [line,red] (m-9-1.east) -| ([xshift=1.5cm]m-10-1.north);
\draw[line,red] (m-10-2) -- (m-6-2) node[pos=0.3,right,text=black]{Yes, Pass (a5,c5)};
\draw [line] (m-6-2) --node[midway,below,text=black]{Yes, Pass (a6,c6)} node[midway,above,text=black]{Step 6} (m-6-3);
\node[xshift=4cm] at (m-1-1) {A,b,c,d,k given};
\node[xshift=-2cm] at (m-3-1){Step 1(4)};
\node[xshift=-2cm] at (m-5-1){Step 2 (7)};
\node[xshift=-2cm] at (m-7-1){Step 3 (9)};
\node[xshift=-2cm] at (m-9-1){Step 4 (3)};
\node[xshift=-2cm,above] at (m-10-2){Step 5};
\draw [>=latex,-,red,thick] (a) --(d);
\draw [line,red] ($(a)!0.5!(d)$) -- (m-6-2);
\end{tikzpicture}
\caption{Your caption here}
\end{figure}
\end{document}