12

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}

2 Answers 2

16

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}
2
  • 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, 2013 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}}). Aug 7, 2013 at 0:24
4

A simple solution is to store the path D1 in a variable \D1, and so for S1. Then use this variable as many times as needed. Here is an example:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{pgfplots}

\usetikzlibrary{calc,3d,shapes, pgfplots.external, intersections}
\pgfplotsset{compat=1.11} 

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

%Pre-Define Paths

  \def\D1{(0,1.8) -- (1.8,0)}
  \def\S1{(0,0) -- (1.8,1.8)}

\path[name path=D1] \D1;
\path[name path=S1] \S1;
\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] \D1;
\draw[red] \S1;

\end{tikzpicture}
\end{document}

Here is the figure:

enter image description here

3
  • 1
    Technically speaking you are not saving the path. You are saving the input of the path. If there is a decoration or something similar on the path this won't work.
    – percusse
    Nov 16, 2015 at 23:24
  • note that the 1 in \def\D1{...} is rather acting like a parameter delimeter than part of the control sequence name. If you said \def\D2{...} afterwards and then tried to use \D1 you would get the error ! Use of \D doesn't match its definition.. Also note that spaces after \D1 are not ignored. see TeXBook pages 202--204.
    – jakun
    Feb 15, 2017 at 9:58
  • You can probably also use \newcommand instead of \def.
    – Turion
    Feb 14, 2018 at 16:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .