1

I want to save a path, draw it cropped and put an arrow on it

\documentclass[10pt,convert={convertexe=magick,density=1000,outext=.png}]{standalone}

\usepackage{tikz} \usetikzlibrary{decorations.markings}

\begin{document}

\begin{tikzpicture}

\path[save path=\test] (0,0) -- (5,5); \node at (0,0) {origin};

\begin{scope} \clip (1,1) rectangle (4,4); \draw[decorate,decoration={markings,mark=at position 0.5 with {\arrow{stealth}}}][use path=\test]; \end{scope}

\end{tikzpicture}

\end{document}

Well, putting arrow does not work, because I get the message

! Package pgf Error: I cannot decorate an empty path.

What is a workaround for this problem?

Pygmalion
  • 6,387
  • 4
  • 34
  • 68
  • A quick fix would probably be to use \newcommand{\test}{ (0,0) -- (5,5) } instead of saving the path. I also found these answers that might be helpful: https://tex.stackexchange.com/a/127045/47927 and https://tex.stackexchange.com/a/26386/47927 – Jasper Habicht Jul 12 '22 at 21:11

1 Answers1

3

The use path facility provided by TikZ is not quite as sophisticated as it needs to be. In particular, it doesn't make the path work with actions and decorations.

My spath3 library (development version on github) handles this correctly, as in the following version of your code.

Note that if you want your line drawn and decorated then you should put the decoration in a pre or post action.

\documentclass[10pt,convert={convertexe=magick,density=1000,outext=.png}]{standalone}
%\url{https://tex.stackexchange.com/q/650655/86}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,spath3}

\begin{document}

\begin{tikzpicture}

\path[spath/save=test] (0,0) -- (5,5); \node at (0,0) {origin};

\begin{scope} \clip (1,1) rectangle (4,4); \draw[postaction={decorate},decoration={markings,mark=at position 0.5 with {\arrow{stealth}}}][spath/use=test]; \end{scope}

\end{tikzpicture}

\end{document}

decorated re-used path

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • OK, I just realised that this does not work in the my actual situation, which is saving paths with \addplot in axis environment. The original question was meant to be a simplification of the problem, perhaps I oversimplified. Is there a workaround for that too? – Pygmalion Jul 13 '22 at 06:38
  • I opened a new question: https://tex.stackexchange.com/questions/650703/cannot-decorate-an-empty-path-in-pgfplot – Pygmalion Jul 13 '22 at 07:22