Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add shortcuts to uninstallcommands #1160

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/generic/pgf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ lot of contributed changes. Thanks to everyone who volunteered their time!
- Make `graphdrawing` work with `name prefix` and `name suffix` options #1087
- pgfkeys was a bit too relaxed around `\relax` #1132
- Remove spurious spaces for `3d view` #1151
- Add shortcuts defined by `\tikzaddtikzonlycommandshortcut(def|let)` to
uninstallcommands #1160

### Changed

Expand Down
53 changes: 53 additions & 0 deletions testfiles/gh-pull-1160.lvt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
\documentclass{minimal}
\input{pgf-regression-test}

\RequirePackage{tikz}

\begin{document}

\START

\def\myspecialsauce{\node{specialsauce}}

\protected\long\def\ASSERTEQUAL#1#2{%
\ifx#1#2\TRUE\else\FALSE\fi
}
\let\UNDEFINED\undefined
\def\specialsauceOUTER{outer definition}


\BEGINTEST{\tikzaddtikzonlycommandshortcutlet, undefined}
\tikzaddtikzonlycommandshortcutlet{\specialsauce}{\myspecialsauce}
\begin{tikzpicture}
\specialsauce;
\node at (0,1) {\ASSERTEQUAL\specialsauce\UNDEFINED};
\end{tikzpicture}
\ENDTEST

\BEGINTEST{\tikzaddtikzonlycommandshortcutlet, defined}
\let\specialsauce\specialsauceOUTER
\tikzaddtikzonlycommandshortcutlet{\specialsauce}{\myspecialsauce}
\begin{tikzpicture}
\specialsauce;
\node at (0,1) {\ASSERTEQUAL\specialsauce\specialsauceOUTER};
\end{tikzpicture}
\ENDTEST

\BEGINTEST{\tikzaddtikzonlycommandshortcutdef, undefined}
\tikzaddtikzonlycommandshortcutdef{\specialsauce}{\myspecialsauce}
\begin{tikzpicture}
\specialsauce;
\node at (0,1) {\ASSERTEQUAL\specialsauce\UNDEFINED};
\end{tikzpicture}
\ENDTEST

\BEGINTEST{\tikzaddtikzonlycommandshortcutdef, defined}
\let\specialsauce\specialsauceOUTER
\tikzaddtikzonlycommandshortcutdef{\specialsauce}{\myspecialsauce}
\begin{tikzpicture}
\specialsauce;
\node at (0,1) {\ASSERTEQUAL\specialsauce\specialsauceOUTER};
\end{tikzpicture}
\ENDTEST

\END
22 changes: 22 additions & 0 deletions testfiles/gh-pull-1160.tlg
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
This is a generated file for the l3build validation system.
Don't change this file in any respect.
============================================================
TEST 1: \tikzaddtikzonlycommandshortcutlet , undefined
============================================================
TRUE
============================================================
============================================================
TEST 2: \tikzaddtikzonlycommandshortcutlet , defined
============================================================
TRUE
============================================================
============================================================
TEST 3: \tikzaddtikzonlycommandshortcutdef , undefined
============================================================
TRUE
============================================================
============================================================
TEST 4: \tikzaddtikzonlycommandshortcutdef , defined
============================================================
TRUE
============================================================
28 changes: 24 additions & 4 deletions tex/generic/pgf/frontendlayer/tikz/tikz.code.tex
Original file line number Diff line number Diff line change
Expand Up @@ -1899,16 +1899,36 @@
% #1: shortcut command inside of tikzpicture
% #2: real command name
\def\tikzaddtikzonlycommandshortcutlet#1#2{%
\expandafter\def\expandafter\tikz@installcommands\expandafter{\tikz@installcommands
\let#1=#2%
\edef\tikz@installcommands{\pgfutil@unexpanded\expandafter{\tikz@installcommands}%
\ifdefined#1%
\let\expandafter\noexpand\csname tikz@orig\detokenize{#1}\endcsname=\noexpand#1%
\fi
\pgfutil@unexpanded{\let#1=#2}%
}%
\edef\tikz@uninstallcommands{\pgfutil@unexpanded\expandafter{\tikz@uninstallcommands}%
\ifdefined#1%
\let\noexpand#1\expandafter\noexpand\csname tikz@orig\detokenize{#1}\endcsname
\else
\pgfutil@unexpanded{\let#1=\pgfutil@undefined}%
\fi
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work because now \ifdefined is executed at the point where \tikzaddtikzonlycommandshortcutlet is used. However at this point #2 might not be defined yet, so code like this will restore the wrong thing

\tikzaddtikzonlycommandshortcutlet{\oops}{\specialsauce} % <-- \specialsauce not yet defined
\def\specialsauce{tada}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\ifdefined always acts on #1 so when #2 is defined is irrelevant here and I suppose you want to say

However at this point #1 (not #2) might not be defined yet, so code like this will restore the wrong thing

\tikzaddtikzonlycommandshortcutlet{\oops}{\myspecialsauce} % <-- \oops not yet defined
\def\oops{tada}

Or maybe you noticed in the patch to uninstallcommands, we should check if \tikz@orig#1 is defined.

Anyway I find it suffices to just construct cs from \csname tikz@orig ...\endcsname before adding it to installcommands/uninstallcommands.

}%
}%

% Has the same effect as \tikzaddtikzonlycommandshortcutlet but uses
% \def#1{#2} instead of \let.
\def\tikzaddtikzonlycommandshortcutdef#1#2{%
\expandafter\def\expandafter\tikz@installcommands\expandafter{\tikz@installcommands
\def#1{#2}%
\edef\tikz@installcommands{\pgfutil@unexpanded\expandafter{\tikz@installcommands}%
\ifdefined#1%
\let\expandafter\noexpand\csname tikz@orig\detokenize{#1}\endcsname=\noexpand#1%
\fi
\pgfutil@unexpanded{\def#1{#2}}%
}%
\edef\tikz@uninstallcommands{\pgfutil@unexpanded\expandafter{\tikz@uninstallcommands}%
\ifdefined#1%
\let\noexpand#1\expandafter\noexpand\csname tikz@orig\detokenize{#1}\endcsname
\else
\pgfutil@unexpanded{\let#1=\pgfutil@undefined}%
\fi
}%
}%

Expand Down