6

Title+Picture might make you guess my question:

enter image description here

My goal is to modify the tikz-style companion such that in this example the text along path-decoration will only be shown if it fits, i.e. between a,b but not between b,c.
More precisely, I want the style to

  1. compute the length Le of the edge it is applied to,
  2. compute the length Ld of its decoration-text,
  3. check, whether Le ≥ Ld + c holds, where c is some constant for a minimal margin) and
  4. only if that is the case, apply the decoration.

Minimal standalone source:

\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}

I hope there is an elegant but dynamic solution.

percusse
  • 157,807
IARI
  • 1,455
  • You can either hack into text decoration or define a new metadecoration. Is it going to be always the same text comapnion? – percusse Aug 16 '12 at 20:52
  • 1
    Well, for now yes, it's always the same text. However, with respect to that, the solution should be easily customizable. – IARI Aug 16 '12 at 20:55

1 Answers1

3

If you were defining a new decoration, you could do this with the key switch if less than=<dim> to <state>. Fortunately, it isn't too hard to add this to the current decoration. We have to do it after the text length has been computed, so we hook into the left indent state. I haven't taken into account the right margin, though.

\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}

Result:

Text decoration with long text suppressed

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751