![使用 tikz 图呈现最佳路线解决方案](https://linux22.com/image/319122/%E4%BD%BF%E7%94%A8%20tikz%20%E5%9B%BE%E5%91%88%E7%8E%B0%E6%9C%80%E4%BD%B3%E8%B7%AF%E7%BA%BF%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88.png)
我打算进行一个数学优化问题,其中我的解决方案将以图形方式呈现。为此,我尝试tikz
使用节点和它们之间的边来形成一个图。到目前为止,我的代码是:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{tikz,fullpage}
\usetikzlibrary{arrows,%
petri,%
topaths}%
\usepackage{tkz-berge}
\usepackage[position=top]{subfig}
\begin{document}
\centering
\twocolumn
\begin{tikzpicture}[scale=0.75,transform shape]
\SetGraphUnit{4}
\tikzset{
VertexStyle/.append style={
bend angle = 45,%
fill = gray!30
}
}
\Vertex{21}
\NOEA(21){31}
\SOEA(21){52}
\NOEA(31){41}
\SOEA(31){62}
\SOEA(62){72}
\tikzstyle{EdgeStyle}=[pre and post]
\Edge[label=$4$](21)(52)
\Edge[label=$9$](52)(62)
\Edge[label=$8$](21)(31)
\Edge[label=$7$](31)(41)
\Edge[label=$3$](62)(72)
\Edge[label=$1$](52)(31)
\Edge[label=$10$](41)(62)
\end{tikzpicture}
\end{document}
但我有两个问题:
- 有没有办法制作如图所示的图表来说明解决方案的流程和
- 如何使这张图片稍微小一点,以便可以用于解决方案
\twocolumn
?
提前致谢!
问候,
答案1
对于弯曲的箭头,您只需在图片末尾添加一些常规的绘图命令即可。该quotes
库使添加标签变得简单:
\draw [blue,->]
(41) edge ["+=1",bend left] (62)
(62) edge ["+=2", bend left] (72)
;
\draw [red,->]
(21) edge ["+=1",bend left] (31)
(31) edge ["+=2", bend left] (21)
(21) edge ["+=3", bend right, swap] (52)
(52) edge ["+=4", bend right, swap] (62)
;
至于如何让它适合。好吧,你可以通过scale
向下调整因子来让它变小。例如使用0.625
而不是0.75
并显示页面的框架:
完整代码:
\documentclass{article}
\usepackage{tikz,fullpage,showframe,kantlipsum}
\usetikzlibrary{arrows,petri,quotes}
\usepackage{tkz-berge}
\begin{document}
\twocolumn
{\centering
\begin{tikzpicture}[scale=0.625,transform shape]
\SetGraphUnit{4}
\tikzset{
VertexStyle/.append style={
bend angle = 45,
fill = gray!30
}
}
\Vertex{21}
\NOEA(21){31}
\SOEA(21){52}
\NOEA(31){41}
\SOEA(31){62}
\SOEA(62){72}
\tikzset{EdgeStyle/.style=pre and post}
\Edge[label=$4$](21)(52)
\Edge[label=$9$](52)(62)
\Edge[label=$8$](21)(31)
\Edge[label=$7$](31)(41)
\Edge[label=$3$](62)(72)
\Edge[label=$1$](52)(31)
\Edge[label=$10$](41)(62)
\draw [blue,->]
(41) edge ["+=1",bend left] (62)
(62) edge ["+=2", bend left] (72)
;
\draw [red,->]
(21) edge ["+=1",bend left] (31)
(31) edge ["+=2", bend left] (21)
(21) edge ["+=3", bend right, swap] (52)
(52) edge ["+=4", bend right, swap] (62)
;
\end{tikzpicture}\par}
\kant[1-4]
\end{document}