以固定距离在线旁添加标注

以固定距离在线旁添加标注

我需要为使用 TikZ 创建的加权图制作动画,强调每条边一次(暂时将线宽设置为粗)。问题是,更改线宽时边权重会跳跃。为了防止边权重跳跃,我复制了每条边。第一个副本使用细线宽,带有标签,并且始终绘制在背景上。第二个副本使用粗线宽,没有标签,并且仅绘制在某些幻灯片上。

\begin{tikzpicture}
  \node (1) at (0,0) {1};
  \node (2) at (1,0) {2}
      edge node [auto] {1} (1)
      edge [thick] (1);
  \node (3) at (1,1) {3}
      edge node [auto] {2} (2)
      edge [thick] (2);
  \node (4) at (0,1) {4}
      edge node [auto] {3} (3)
      edge node [auto, swap] {4} (1)
      edge [thick] (3)
      edge [thick] (1);
\end{tikzpicture}

有没有更好的解决方案,不重复每条边?

答案1

我认为你的策略已经相当不错了。我个人会使用这个overlay-beamer-styles库来实现这一点。这也允许你只绘制一次边缘,这是thick on我为此目的创建的。我没有看到任何跳跃。正如@Max Snippe 指出的那样,我之前的解决方案中存在微小的跳跃。鉴于此,我仍然相信你最初的想法是最经济的提议之一。我添加了另一个以供比较。

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}
\tikzset{
    thick on/.style={alt={#1{thick}{thin}}},
    ultra thick on/.style={alt={#1{line width=3mm}{thin}}}
}

\begin{document}
\begin{frame}
\frametitle{An animation with the overlay-beamer-styles library}
\begin{tikzpicture}
  \node (1) at (0,0) {1};
  \node (2) at (1,0) {2}
      edge node [auto] {1} (1)
      edge [thick,visible on=<1>] (1);
  \node (3) at (1,1) {3}
      edge node [auto] {2} (2)
      edge [thick,visible on=<2>] (2);
  \node (4) at (0,1) {4}
      edge node [auto] {3} (3)
      edge node [auto, swap] {4} (1)
      edge [thick,visible on=<3>] (3)
      edge [thick,visible on=<4>] (1);
\end{tikzpicture}
\begin{tikzpicture}
  \node (1) at (0,0) {1};
  \node (2) at (1,0) {2}
      edge [thick on=<1>] (1) (2) --(1) node [midway,auto]{1} ;
  \node (3) at (1,1) {3}
      edge [thick,thick on=<2>] (2) (3) -- node [midway,auto] {2} (2);
  \node (4) at (0,1) {4}
      edge [thick,thick on=<3>] (3) 
      edge [thick,thick on=<4>] (1) 
      (4) -- node [midway,auto] {3} (3)
      (4) -- node [midway,auto, swap] {4} (1);
\end{tikzpicture}
\begin{tikzpicture}
  \node (1) at (0,0) {1};
  \node (2) at (1,0) {2}
      edge [ultra thick on=<1>] (1) (2) --(1) node [midway,auto]{1} ;
  \node (3) at (1,1) {3}
      edge [ultra thick,ultra thick on=<2>] (2) (3) -- node [midway,auto] {2} (2);
  \node (4) at (0,1) {4}
      edge [ultra thick,ultra thick on=<3>] (3) 
      edge [ultra thick,ultra thick on=<4>] (1) 
      (4) -- node [midway,auto] {3} (3)
      (4) -- node [midway,auto, swap] {4} (1);
\end{tikzpicture}
\end{frame}
\end{document}

在此处输入图片描述

第三张图片只是为了说明没有跳跃。

答案2

尽管我同意@marmot的观点,即大多数情况下更简单的解决方案更好,但我会添加此答案作为替代方案,因为只需绘制一次边缘即可完成此操作(带有相当繁琐的装饰)。 此解决方案确实需要库decorations.pathreplacing

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles,decorations.pathreplacing}
\tikzset{
    thick on/.style={line width=0.4pt,alt={#1{decoration={show path construction, lineto code={\draw[thick] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);}},decorate}{}}},
}

\begin{document}
\begin{frame}
\frametitle{An animation with the overlay-beamer-styles library}
\begin{tikzpicture}
    \node (1) at (0,0) {1};
    \node (2) at (1,0) {2}
        edge node [auto] {1} (1)
        edge [thick] (1);
    \node (3) at (1,1) {3}
        edge node [auto] {2} (2)
        edge [thick] (2);
    \node (4) at (0,1) {4}
        edge node [auto] {3} (3)
        edge node [auto, swap] {4} (1)
        edge [thick] (3)
        edge [thick] (1);
\end{tikzpicture}
\begin{tikzpicture}
    \node (1) at (0,0) {1};
    \node (2) at (1,0) {2};
    \node (3) at (1,1) {3};
    \node (4) at (0,1) {4};
    \begin{scope}[every node/.style={auto,swap,midway}]
        \draw[thick on=<1>] (1) -- node{1} (2);
        \draw[thick on=<2>] (2) -- node{2} (3);
        \draw[thick on=<3>] (3) -- node{3} (4);
        \draw[thick on=<4>] (4) -- node{4} (1);
    \end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

相关内容