12

How do I name the indicated TikZ path with a name such as X and subsequently use it, for example, to draw it?

The code shown does not compile!

\documentclass[tikz,border=0pt]{standalone}
\begin{document}

\begin{tikzpicture}
    % coordinates for a region X:
    \coordinate (X1) at (0,0); \coordinate (X2) at (2,2); \coordinate (X3) at (4,1);
    \coordinate (X4) at (3,-2); \coordinate (X5) at (1,-2); \coordinate (X6) at (0,0);
    % try to name the path ''X'':
    \path[name=X] (X1) to[out=90,in=180] (X2) to[out=0,in=90] (X3) to[out=270,in=90] (X4) to[out=270,in=0] (X5) to[out=180,in=270] (X6);
    % draw region X and its boundary:
    \filldraw[thick,fill=gray!30] X;
\end{tikzpicture}

\end{document}

3 Answers 3

22

TikZ allows this, just save the path in a macro here save path=\pathX and reuse it with use path=\pathX

\documentclass[tikz,border=0pt]{standalone}
\begin{document}

\begin{tikzpicture}
    % coordinates for a region X:
    \coordinate (X1) at (0,0); 
    \coordinate (X2) at (2,2); 
    \coordinate (X3) at (4,1);
    \coordinate (X4) at (3,-2); 
    \coordinate (X5) at (1,-2); 
    \coordinate (X6) at (0,0);
    % try to name the path ''X'':
    \path[save path=\pathX] (X1) to[out=90,in=180] (X2) to[out=0,in=90] (X3) to[out=270,in=90] (X4) to[out=270,in=0] (X5) to[out=180,in=270] (X6);
    % draw region X and its boundary:
    \filldraw[thick,fill=gray!30,use path=\pathX];
\end{tikzpicture}

\end{document}

enter image description here

10
  • Why then does \filldraw[thick,fill=gray!30] \pathX not subsequently work instead?
    – murray
    Commented May 20 at 20:05
  • Is there any advantage (or disadvantage) of this method compared to the one in my answer?
    – Alan Munn
    Commented May 20 at 20:12
  • 1
    @murray Tikz syntax requires it to be used in options with use path=\Xpath, see the current manual 3.1.10 section 14.22 Interacting with the Soft Path subsystem
    – AndréC
    Commented May 20 at 20:26
  • 1
    @AlanMunn: That seeming arbirariness is the main thing driving me to distraction trying to learn and effectively use TikZ.
    – murray
    Commented May 21 at 18:21
  • 2
    @murray All things considered, the TikZ syntax is pretty consistent and straightforward, and also extremely well documented, and compared to its main alternative, PSTricks, a breath of fresh air, syntactically. I take it you don't use R... :)
    – Alan Munn
    Commented May 21 at 18:32
12

The best way to save a path depends entirely on what you are going to do with it. The spath3 TikZ library defines a whole slew of additional things that can be done with a saved path, such as transformations, cutting at intersections, using a part of a larger path, and many more.

To save a path for use with this library requires the spath/save=<name>, and then use with spath/use=<name>.

\documentclass[tikz,border=0pt]{standalone}
\usetikzlibrary{spath3}
\begin{document}

\begin{tikzpicture}
    % coordinates for a region X:
    \coordinate (X1) at (0,0); \coordinate (X2) at (2,2); \coordinate (X3) at (4,1);
    \coordinate (X4) at (3,-2); \coordinate (X5) at (1,-2); \coordinate (X6) at (0,0);
    % try to name the path ''X'':
    \path[spath/save=X] (X1) to[out=90,in=180] (X2) to[out=0,in=90] (X3) to[out=270,in=90] (X4) to[out=270,in=0] (X5) to[out=180,in=270] (X6);
    % draw region X and its boundary:
    \filldraw[thick,fill=gray!30,spath/use=X];
\end{tikzpicture}

\end{document}
2
  • What are the advantages of using spath3 saves and uses instead of just plain saves and uses?
    – murray
    Commented May 21 at 18:23
  • @murray as I said in the first paragraph, it's useful if you want to do stuff with it in between defining and using it. Commented May 21 at 20:09
8

You can give a path a macro name, and then use the macro all within the tikzpicture:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
    % coordinates for a region X:
    \coordinate (X1) at (0,0); \coordinate (X2) at (2,2); \coordinate (X3) at (4,1);
    \coordinate (X4) at (3,-2); \coordinate (X5) at (1,-2); \coordinate (X6) at (0,0);
    % try to name the path ''X'':
    \def\mypath{ (X1) to[out=90,in=180] (X2) to[out=0,in=90] (X3) to[out=270,in=90] (X4) to[out=270,in=0] (X5) to[out=180,in=270] (X6);}
    % draw region X and its boundary:
    \filldraw[thick,fill=gray!30] \mypath;
\end{tikzpicture}

\end{document}

output of code

4
  • What's confusing me is that I've seen old TikZ code that seems to use things like \path[name path=X].....
    – murray
    Commented May 20 at 19:57
  • @murray That's possible with the intersections library, but it's specifically for that purpose, not for general reuse of a path.
    – Alan Munn
    Commented May 20 at 20:03
  • This is not really saving a path but just a macro(a string) -that TikZ then later interprets as a path. Commented May 20 at 20:31
  • 1
    @hpekristiansen yes I’m not claiming anything different. But for many purposes it will do what the OP requires I think.
    – Alan Munn
    Commented May 20 at 20:47

You must log in to answer this question.

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