Skip to content

Commit

Permalink
Patches TikZ's foreach to remember more information between iterations
Browse files Browse the repository at this point in the history
This commit adds more information to what TikZ remembers between foreach
iterations, and does so in a way that makes it easier to add other
macros and dimensions to the list of things it remembers, both at the
code level and via keys.

Fixes pgf-tikz#356, pgf-tikz#1047, pgf-tikz#1303, pgf-tikz#1313

Signed-off-by: Andrew Stacey <[email protected]>
  • Loading branch information
loopspace committed Apr 12, 2024
1 parent 20d9027 commit 33aff2d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
1 change: 1 addition & 0 deletions doc/generic/pgf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

- Foreach in path remembers nodes between iterations #356, #1047, #1303, #1313
- Typo in animations `end on` key #1273
- Output bounding box adjustment in pgfsys-dvisvgm.def #1275
- Fix shadings under LuaMetaTeX
Expand Down
78 changes: 62 additions & 16 deletions tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2580,28 +2580,77 @@

\def\tikz@fchar oreach{\tikz@foreach}%

%
% These are to allow \foreach to remember macros and dimensions between iterations
%

\def\tikzforeach@smugglers@cove{}
\let\tikztostart=\relax

\def\tikzforeach@smuggle@macro#1{%
\pgfutil@ifx#1\relax{}{%
\expandafter
\pgfutil@g@addto@macro
\expandafter
\tikzforeach@smugglers@cove
\expandafter
{%
\expandafter\def\expandafter#1\expandafter{#1}%
}%
}%
}

\def\tikzforeach@smuggle@dimen#1{%
\pgfutil@ifx#1\relax{}{%
\expandafter
\pgfutil@g@addto@macro
\expandafter
\tikzforeach@smugglers@cove
\expandafter
{%
\expandafter#1\expandafter=\the#1\relax%
}%
}%
}

\def\tikzforeach@smugglers@loot{%
%
\tikzforeach@smuggle@macro\tikz@moveto@waiting%
\tikzforeach@smuggle@macro\tikztostart%
\tikzforeach@smuggle@macro\tikz@tangent%
%
\tikzforeach@smuggle@dimen\tikz@lastx%
\tikzforeach@smuggle@dimen\tikz@lasty%
\tikzforeach@smuggle@dimen\tikz@lastxsaved%
\tikzforeach@smuggle@dimen\tikz@lastysaved%
%
}

\tikzset{
remember macro/.code={
\tikzforeach@smuggle@macro#1
},
remember dimension/.code={
\tikzforeach@smuggle@dimen#1
}
}


\def\tikz@foreach{%
\def\pgffor@beginhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\tikzforeach@smugglers@cove%
\gdef\tikzforeach@smugglers@cove{}%
\setbox\tikz@figbox=\box\tikz@tempbox%
\setbox\tikz@figbox@bg=\box\tikz@tempbox@bg%
\expandafter\tikz@scan@next@command\pgfutil@firstofone}%
\def\pgffor@endhook{\pgfextra{%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\tikzforeach@smugglers@loot%
\global\setbox\tikz@tempbox=\box\tikz@figbox%
\global\setbox\tikz@tempbox@bg=\box\tikz@figbox@bg%
\pgfutil@gobble}}%
\def\pgffor@afterhook{%
\tikz@lastx=\tikz@foreach@save@lastx%
\tikz@lasty=\tikz@foreach@save@lasty%
\tikz@lastxsaved=\tikz@foreach@save@lastxsaved%
\tikz@lastysaved=\tikz@foreach@save@lastysaved%
\tikzforeach@smugglers@cove%
\gdef\tikzforeach@smugglers@cove{}%
\let\pgffor@beginhook\relax%
\let\pgffor@endhook\relax%
\let\pgffor@afterhook\relax%
Expand All @@ -2610,10 +2659,7 @@
\tikz@scan@next@command}%
\global\setbox\tikz@tempbox=\box\tikz@figbox%
\global\setbox\tikz@tempbox@bg=\box\tikz@figbox@bg%
\xdef\tikz@foreach@save@lastx{\the\tikz@lastx}%
\xdef\tikz@foreach@save@lasty{\the\tikz@lasty}%
\xdef\tikz@foreach@save@lastxsaved{\the\tikz@lastxsaved}%
\xdef\tikz@foreach@save@lastysaved{\the\tikz@lastysaved}%
\tikzforeach@smugglers@loot%
\foreach}%


Expand Down

0 comments on commit 33aff2d

Please sign in to comment.