Skip to content

Instantly share code, notes, and snippets.

@hmenke
Created May 9, 2019 03:42
Show Gist options
  • Save hmenke/3a35cb7f1d0c9fab36325775fea59761 to your computer and use it in GitHub Desktop.
Save hmenke/3a35cb7f1d0c9fab36325775fea59761 to your computer and use it in GitHub Desktop.
Strip the last token from a list
\documentclass{article}
\pagestyle{empty}
\begin{document}
\makeatletter
% pgf@keys@utilifxthenelse
%
% #1 = two tokens to be compared using \ifx
%
% This is just wrapper around \ifx which avoids the incomplete \ifx problem.
% The tokens to be compared are passed as the first argument and therefore
% tokenized.
\long\def\pgf@keys@utilfirstoftwo#1#2{#1}
\long\def\pgf@keys@utilsecondoftwo#1#2{#2}
\long\def\pgf@keys@utilifxthenelse#1{%
\ifx#1%
\expandafter\pgf@keys@utilfirstoftwo
\else
\expandafter\pgf@keys@utilsecondoftwo
\fi}
% \pgf@keys@utilstriplast
%
% #1 = token to stripped
% #2 = list of tokens (possibly empty)
%
% Sometimes it is required to strip the last element of a token list if it is a
% certain token, e.g. strip the trailing / in pgfkeys. This can usually be
% dealt with using a delimited macro but not if trailing token is optional.
%
% It does not like embedded groups, i.e. \pgf@keys@utilstriplast{/}{/a{b/c/}d/}
% will probably fail.
%
% \pgf@keys@utilstriplast is fully expandable with \edef and \romannumeral-`0.
\def\pgf@keys@utilstriplast@end{\pgf@keys@utilstriplast@end}
\long\def\pgf@keys@utilstriplast@chkendsplit#1#2{%
\pgf@keys@utilifxthenelse{#2\pgf@keys@utilstriplast@end}%
{}%
{\expandafter#1\expandafter#2\romannumeral-`0\pgf@keys@utilstriplast@{#1}}%
}
\long\def\pgf@keys@utilstriplast@#1#2{%
\pgf@keys@utilifxthenelse{#2\pgf@keys@utilstriplast@end}%
{}%
{\pgf@keys@utilifxthenelse{#1#2}%
{\pgf@keys@utilstriplast@chkendsplit{#1}}%
{\expandafter#2\romannumeral-`0\pgf@keys@utilstriplast@{#1}}}%
}
\long\def\pgf@keys@utilstriplast#1#2{%
\pgf@keys@utilstriplast@{#1}#2\pgf@keys@utilstriplast@end
}
\edef\x{\pgf@keys@utilstriplast{/}{///a}}
\texttt{\meaning\x} X\x Y\par
\edef\x{\pgf@keys@utilstriplast{/}{/a{b/c/}d/}}
\texttt{\meaning\x} X\x Y\par
\edef\x{\pgf@keys@utilstriplast{/}{a///}}
\texttt{\meaning\x} X\x Y\par
\edef\x{\pgf@keys@utilstriplast{/}{/a/b/c//d/e/}}
\texttt{\meaning\x} X\x Y\par
\edef\x{\pgf@keys@utilstriplast{/}{/a/b/c//d/e}}
\texttt{\meaning\x} X\x Y\par
\edef\x{\pgf@keys@utilstriplast{/}{}}
\texttt{\meaning\x} X\x Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{///a}}
\showthe\toks0 X\the\toks0 Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{/a{b/c/}d/}}
\showthe\toks0 X\the\toks0 Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{a///}}
\showthe\toks0 X\the\toks0 Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{/a/b/c//d/e/}}
\showthe\toks0 X\the\toks0 Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{/a/b/c//d/e}}
\showthe\toks0 X\the\toks0 Y\par
\toks0=\expandafter{\romannumeral-`0\pgf@keys@utilstriplast{/}{}}
\showthe\toks0 X\the\toks0 Y\par
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment