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.

I would like to use the name of a previous defined path to draw it later on in my code. I have included an example below, where I define two paths, "D1" and "S1", and then later on, draw these very sames paths. In the example, I draw the paths, using essentially the same code, which is redundant. Is there a way to recall the name of the path and draw a line using the previously defined path?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths

\path[name path=D1] (0,1.8) -- (1.8,0);
\path[name path=S1] (0,0) -- (1.8,1.8);
\path[name intersections={of=D1 and S1,by=EQ}];

%axis

\draw[->,thick] (0,0) -- (3,0) node[right,fontscale=0] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left,fontscale=0] {Price};

%Initial Equilibrium

\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue] (0,1.8) -- (1.8,0);
\draw[red] (0,0) -- (1.8,1.8);


\end{tikzpicture}

\end{document}
share|improve this question

1 Answer 1

up vote 6 down vote accepted

You can use the undocumented save path key that saves the whole path on a very low level in a macro. You can use this path again with the use path key defined as

\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother

Any path operators shouldn’t be used here.

If you want to save part of a path and want to use them later (maybe also only as a part), you can use the saveuse path key from another answer of mine defined as

\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.estyle={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname % not optimal as it is now global through out the document
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}%
  }
}

The saveuse path key saves a path as an insert path key (and uses it also) which can be used later for the same path (as long as you don’t change the transformation matrix).

Code A

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{intersections}
\makeatletter
\tikzset{use path/.code=\tikz@addmode{\pgfsyssoftpath@setcurrentpath#1}}
\makeatother
\begin{document}
\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths
\path[save path=\pathA,name path=D1] (0,1.8) -- (1.8,0);
\path[save path=\pathB,name path=S1] (0,0) -- (1.8,1.8);
\path[name intersections={of=D1 and S1,by=EQ}];

%axis
\draw[->,thick] (0,0) -- (3,0) node[right] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left] {Price};

%Initial Equilibrium
\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue][use path=\pathA];
\draw[red] [use path=\pathB];
\end{tikzpicture}
\end{document}

Code B

\documentclass[tikz,convert=false]{standalone}
\usetikzlibrary{intersections}
\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.estyle={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname % not optimal as it is now global through out the document
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}%
  }
}
\begin{document}
\begin{tikzpicture}[yscale=3,xscale=3]

%Pre-Define Paths
\path[name path=D1] [saveuse path={pathA}{(0,1.8) -- (1.8,0)}];
\path[name path=S1] [saveuse path={pathB}{(0,0) -- (1.8,1.8)}];
\path[name intersections={of=D1 and S1,by=EQ}];

%axis
\draw[->,thick] (0,0) -- (3,0) node[right] {Quantity};
\draw[->,thick] (0,0) -- (0,2) node[left] {Price};

%Initial Equilibrium
\fill[black] (EQ) circle (.5pt) node[right] {$EQ$};
\draw[blue][pathA];
\draw[red] [pathB];
\end{tikzpicture}
\end{document}
share|improve this answer
    
Great. One question: In Code A, I tried to make the "save path" name more intuitive, i.e. \pathD1. This would not compile because of the number "1" in the name. Are there restrictions on the names that can be used with "save path?" –  profj Aug 7 '13 at 0:17
1  
@stanford202 The same as with every macro name in TeX with the default catcode settings. You could use save path/.expand once=\csname pathD1\endcsname and use path=\csname pathD1\endcsname (or create wrapper macros for save path and use path that do exactly this: save path*/.style={save path/.expand once={\csname#1\endcsname}}, use path*/.style={use path={\csname#1\endcsname}}). –  Qrrbrbirlbel Aug 7 '13 at 0:24

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.