我使用 tikz 绘制图形,每条边应有一个边标签。此边标签应位于中心的边缘。
我把一个放在edge node
每个边缘。应该有一个小的正交标签到边缘中心的距离。因此,从边缘中心开始,沿着边缘的正交方向走一段指定的距离,然后将标签放在那里。
到现在为止,我得到的是无法正常工作的东西。
\documentclass[12pt,a4paper,oneside,listof=totoc,bibliography=totoc,BCOR=4mm,DIV=12,]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture} [scale=0.75]
\begin{scope}[every node/.style={circle,draw,fill=white}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[>=latex,
every node/.style={midway},
every edge/.style={draw=black,thick}]
\path [-] (5) edge[right] node {\footnotesize $1$} (6);
\path [-] (6) edge[right] node {\footnotesize $1$} (7);
\path [-] (6) edge[right] node {\footnotesize $1$} (8);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
如何使用quotes
库代替edge nodes
?然后,您可以通过在边本身内指定边节点edge["1"]
。然后,边节点的位置由auto
可以采用left
或的键控制right
。
键的left
和的含义是,节点将放置在路径的左侧,尊重方向(例如,如果路径从左到右,则节点将放置在路径中间及上方)。right
auto
auto=left
(0,0)
(1,0)
平均能量损失
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture} [scale=0.75]
\begin{scope}[every node/.style={circle,draw,fill=white}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\begin{scope}[
>=latex,
every node/.style={font=\footnotesize},
every edge/.style={auto=right, draw=black,thick}]
\path [-] (6) edge["1"] (5);
\path [-] (6) edge["1"] (7);
\path [-] (6) edge["1"] (8);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
替代好答案吉列尔梅·扎诺泰利(较少考虑 OP MWE):
\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[
every edge quotes/.style = {auto, inner sep=1pt, font=\footnotesize},
]
\begin{scope}[every node/.style = {circle,draw}]
\node (5) at (0.5,4.5) {1};
\node (6) at (3.5,4.5) {3};
\node (7) at (1.5,6) {3};
\node (8) at (4.5,6) {1};
\end{scope}
\draw[thick] (5) to ["$1$"] (6)
(7) to ["$1$"] (6)
(6) to ["$1$"] (8);
\end{tikzpicture}
\end{document}