标题+图片也许能让你猜到我的问题:
我的目标是修改 tikz 样式companion
,以便在此示例中,text along path
-decoration 仅在适合时才显示,即在 a、b 之间,但不在 b、c 之间。
更确切地说,我希望样式
Le
计算应用的边的长度,Ld
计算其装饰文本的长度,- 检查是否
Le ≥ Ld + c
成立,其中c
是某个常数,用于最小边距)并且 - 只有在这种情况下,才应用装饰。
最小独立源:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.text,arrows}
\begin{document}
\begin{tikzpicture}[companion/.style={->,
postaction={
decorate,decoration={raise=2pt,text along path,text color=purple!60!black,text={|\tiny|companion},text align=center}
}
},node distance=1.2cm,>=stealth',auto]
\node[draw] (t3) at (1,0) {$c$};
\node[draw] (t1) at (0,0) {$b$} edge[companion] (t3);
\node[draw] (t2) at (-2,0) {$a$} edge[companion,bend left] (t1);
\end{tikzpicture}
\end{document}
我希望有一个优雅而又动态的解决方案。
答案1
如果您要定义新的装饰,可以使用 key 来执行此操作switch if less than=<dim> to <state>
。幸运的是,将其添加到当前装饰中并不太难。我们必须在计算文本长度后执行此操作,因此我们将其挂接到状态中left indent
。不过,我没有考虑右边距。
\documentclass{article}
%\url{http://tex.stackexchange.com/q/67470/86}
\usepackage{tikz}
\usetikzlibrary{decorations,decorations.text,arrows}
\makeatletter
\pgfkeys{
/pgf/decoration/omit long text/.code={%
\expandafter\def\csname pgf@decorate@@text along path@left indent@options\expandafter\expandafter\expandafter\endcsname\expandafter\expandafter\expandafter{\csname pgf@decorate@@text along path@left indent@options\endcsname,switch if less than=\pgf@lib@dec@text@width to final}%
},
}
\makeatother
\begin{document}
\begin{tikzpicture}[companion/.style={->,
postaction={
decorate,decoration={raise=2pt,omit long text,text along path,text color=purple!60!black,text={|\tiny|companion},text align=center}
}
},node distance=1.2cm,>=stealth',auto]
\node[draw] (t3) at (1,0) {$c$};
\node[draw] (t1) at (0,0) {$b$} edge[companion] (t3);
\node[draw] (t2) at (-2,0) {$a$} edge[companion,bend left] (t1);
\end{tikzpicture}
\begin{tikzpicture}[companion/.style={->,
postaction={
decorate,decoration={raise=2pt,text along path,text color=purple!60!black,text={|\tiny|companion},text align=center}
}
},node distance=1.2cm,>=stealth',auto]
\node[draw] (t3) at (1,0) {$c$};
\node[draw] (t1) at (0,0) {$b$} edge[companion] (t3);
\node[draw] (t2) at (-2,0) {$a$} edge[companion,bend left] (t1);
\end{tikzpicture}
\end{document}
结果: