Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Using the intersections library, we are able to name a path. For example

\path[name path=line1] (0,0) -- (1,1);

Now, how can I later in the picture call upon this path with the name line1 and do things to it -- such as drawing, marking etc.?

PS. I did look at answers to some other seemingly related questions (such as part drawing of a path etc.) but not able to find an answer.

share|improve this question
add comment

1 Answer

up vote 11 down vote accepted

If you want to do serious stuff with reusing paths, then you might want to take a look at the spath part of the TeX-SX package. If you just want a quick way to (essentially) copy a previously defined path then the following code will work (though I haven't tested it in great detail). The point is that when you put the name path key on a path then TikZ really does save the path. So all that is needed is to sneak it back in at the right time. Turns out that there's a difference between the main path and a pre- or postaction path, but that's not hard to deal with.

Here's the code. The intersections library provides the name path key, the decorations.markings library is used purely for demonstration purposes.

\documentclass[border=1cm]{standalone}
% \url{http://tex.stackexchange.com/q/26382/86}
\usepackage{tikz}
\usetikzlibrary{intersections,decorations.markings}

\makeatletter
\tikzset{
  use path for main/.code={%
    \tikz@addmode{%
      \expandafter\pgfsyssoftpath@setcurrentpath\csname tikz@intersect@path@name@#1\endcsname
    }%
  },
  use path for actions/.code={%
    \expandafter\def\expandafter\tikz@preactions\expandafter{\tikz@preactions\expandafter\let\expandafter\tikz@actions@path\csname tikz@intersect@path@name@#1\endcsname}%
  },
  use path/.style={%
    use path for main=#1,
    use path for actions=#1,
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\draw[ultra thick,name path=mypath] (0,0) circle[radius=1cm];
\fill[blue,use path=mypath] (0,0) rectangle (1,1);
\draw[use path=mypath,postaction={decorate},decoration={markings,mark=at position .5 with {\arrow[orange,line width=5pt]{>};}}] (3,3) -- (4,4);
\end{tikzpicture}
\end{document}

Here's the result:

using paths in TikZ

So neither the rectangle nor the line in the second two commands are used. What is worth pointing out is that while they aren't used, their size is used in computing the size of the TikZ picture. That's what accounts for the large space at the top and right of the circle. So choose the path to be thrown away carefully (or figure out a better place to do the path swap!).

share|improve this answer
    
Ah, very nice! Have you needed/used this in a practical application? –  Jake Aug 24 '11 at 9:13
    
@Jake: Not this particular code - that was for this question. But the technique of taking a path and reusing and modifying it is at the heart of the calligraphy package. –  Andrew Stacey Aug 24 '11 at 9:28
    
@Andrew, Thanks. Btw, will \pgfkeysvalueof{tikz/name path} not give me the name of the current path? Also, thanks for the tip on TeX-SX package -- I did not know that such a thing existed! –  Murali Agastya Aug 25 '11 at 4:16
    
The easiest way is to add no path at all when using use path. –  Paul Gaborit Mar 1 at 16:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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