2

My question is kind of extending of this answer. I want to decorate a path with text only when text fit the path, but also I want to know if decoration was success. I need to know this to be able to call decoration again with shorter text. I need this to implement solution for my question here.

Currently I end up with modifying decoration along path myself using \makeatletter .. \makeatother in document preambule. I just copy text decoration implementation in my preambule and add the setting flag to the end of the persistent precomputation = {..} of the inital state in the following way:

\newif\iffit

\pgfdeclaredecoration{my text along path}{initial}{
  \state{initial}[
    width=+0pt,
    next state=left indent,
    persistent precomputation = {
      ...
      \ifdim\pgf@lib@dec@text@width<\pgfdecoratedpathlength
        \global \fittrue
      \else
        \global \fitfalse
      \fi
    }]{}

And then I can use \iffit in my code to check if decoration if successful. Is there more elegant solution for such case?

1

Yes. Add the following

\makeatletter
    \pgfutil@namedef{pgf@decorate@@text along path@left indent@code}{
        \ifdim\pgf@lib@dec@text@width>\pgfdecoratedremainingdistance
            \tikzerror{text too long}
        \fi
    }
\makeatother

See https://tex.stackexchange.com/a/358705/51022 for more information.

MWE

\documentclass[border=9,tikz]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}

\makeatletter
    \pgfutil@namedef{pgf@decorate@@text along path@left indent@code}{
        \ifdim\pgf@lib@dec@text@width>\pgfdecoratedremainingdistance
            \tikzerror{text too long}
        \fi
    }
\makeatother

\begin{tikzpicture}
    \draw[postaction={decorate,decoration={text along path,
        text={Is this text long enough to trigger the error?}}}]
        (0,0)--(2,0);
\end{tikzpicture}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.