4

This two questions have been answered separatly it should be interesting to be able to shade a region using previously named pathes. Is there a possibility to combine this two macros ?

enter image description here

\documentclass[tikz]{standalone}
\usetikzlibrary{intersections}

%%%% Tikz: shading region bounded by several curves
%%%% thanks to Qrrbrbirlbel
%%%% http://tex.stackexchange.com/questions/140312/tikz-shading-region-bounded-by-several-curves
\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.style={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}},/pgf/math set seed/.code=\pgfmathsetseed{#1}
  }

%%%% Calling a previously named path in tikz
%%%% thanks to Andrew Stacey
%%%% http://tex.stackexchange.com/questions/26382/calling-a-previously-named-path-in-tikz
\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}

\path[name path={Rond}] (0,0) circle (.5) ;
\draw[use path=Rond] ;

\begin{scope}[overlay]
\clip[saveuse path={A}{(0,0) circle (1.5)}] [preaction={fill=red!30}] ;
\path[saveuse path={B}{(0,0) circle (1 and 2)},dashed] [draw,preaction={fill=blue!30}];
\end{scope}

\path[A,B] ;

\end{tikzpicture}
\end{document}
Tarass
  • 16,912

1 Answers1

4

Here is a high level path saving technique using show path construction from decorations.pathreplacing library.

For example after \path[save tikz path=\test] (0,0) -- (1,1); the macro \test will contain (0,0) -- (1,1).

\documentclass[varwidth,border=7pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\tikzset{
  store savedtikzpath in/.code = {\xdef#1{\savedtikzpath}},
  append tikz path/.style = {
    decoration={show path construction,
      moveto code={\xdef\savedtikzpath{\savedtikzpath (\tikzinputsegmentfirst)}},
      lineto code={\xdef\savedtikzpath{\savedtikzpath -- (\tikzinputsegmentlast)}},
      curveto code={\xdef\savedtikzpath{\savedtikzpath%
        .. controls (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)%
        .. (\tikzinputsegmentlast)}},
      closepath code={\xdef\savedtikzpath{\savedtikzpath -- cycle}}
    },
    decorate
  },
  append tikz path/.append style={postaction = {store savedtikzpath in=#1}},
  append tikz path/.default = \savedtikzpath,
  save tikz path/.style = {append tikz path=#1},
  save tikz path/.prefix code={\xdef\savedtikzpath{}},
}
\begin{document}
  \begin{tikzpicture}
    \path[postaction={save tikz path=\rod},draw] (0,0) circle (.5);
    \path[postaction={save tikz path=\A},fill=red!30] (0,0) circle (1.5);
    \clip \A;
    \path[postaction={save tikz path=\B},draw,dashed,fill=blue!30] (0,0) circle (1 and 2);
    \shade[even odd rule,top color=yellow] \B \rod;
    \draw[red] \rod;
  \end{tikzpicture}
\end{document}

enter image description here

Note: This should also be possible with spath3 library, like here.

Kpym
  • 23,002