I want to extract a set of n characters stored in a macro like this:
\def\foo{bar}
\extract{\foo}{1}
prints: b
using expl3.
I have the following code:
\DeclareExpandableDocumentCommand{\extract}{mm}{%
\tl_item:nn { #1 } { #2 }%
}
which works on a literal string: \extract{foo}{2}
prints fo
, but it does not work when using a macro. More specifically, a string stored in pgfkeys.
The final result should be fully expanded and not something that will most likely give the desired character (see: xstring
package).
Full MWE:
\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\extract}{mm}{%
Extracted: \tl_item:nn { #1 } { #2 }%
}
\ExplSyntaxOff
\begin{document}
\extract{foo}{2} % output: fo as expected
\def\foo{bar}
\extract{\foo}{2} % output: nothing
\pgfkeys{/foo/.is family,
/foo/.unknown/.style = %
{\pgfkeyscurrentpath/\pgfkeyscurrentname/%
.initial = #1}}
\pgfkeys{/foo/bar = some text here}
\pgfkeysvalueof{/foo/bar}
\extract{\pgfkeysvalueof{/foo/bar}}{2} % output: /foo/bar
\end{document}