0

The package xcolor can "prepare" color definitions in such a way that they're only loaded if they're actually needed, which may be indicated by a later call to e.g. \providecolors{<id-list>}.

The package also has convenient commands to define a large set of colors sharing a common prefix/suffix: \preparecolorset{HTML}{<prefix>}{<suffix>}{red,ff0000;blue,0000ff} prepares colors <prefix>red<suffix> and <prefix>blue<suffix>.

These colors could then be loaded by calling \providecolors{<prefix>red<suffix>,<prefix>blue<suffix>}.

I'm trying to implement a convenience command to avoid having to repeatedly write out the shared prefix and suffix of the colors. The syntax would be \ProvideColorsFromSet{<prefix>}{<suffix>}{<list>}.

However, the code below, which I thought was rather simple, throws errors Undefined color 'my-red' and Undefined color 'my-blue'.

I'm guessing the problem's to do with expansion, so I've tried different combinations of \exp_args:... and \exp_not:... (without really knowing what I'm doing), but to no avail. I also tried to convert the clist to a seq, and to use \..._map_function instead of \..._map_inline, but with no success.

Any help would be much appreciated. Thank you!

\documentclass{article} \usepackage{xparse} \usepackage{xcolor} \definecolorsfalse \preparecolorset{HTML}{my-}{}{red,ff0000;blue,0000ff} \ExplSyntaxOn \NewDocumentCommand { \ProvideColorsFromSet } { m m m } { \clist_set:Nn \l_tmpa_clist { #3 } % \exp_args:NNx \clist_set:Nn \l_tmpa_clist { #3 } % \clist_set:Nn \l_tmpa_clist { \exp_not:n #3 } \clist_map_inline:Nn \l_tmpa_clist { #1##1#2 } \tl_set:Nn \l_tmpa_tl { \clist_use:Nn \l_tmpa_clist {,} } \providecolors{ \tl_use:N \l_tmpa_tl } % \exp_args:NV \providecolors{ \l_tmpa_tl } % \providecolors{ \l_tmpa_tl } % \providecolors{ \exp_not:N \l_tmpa_tl } % \providecolors{ \exp_args:NV \exp_not:N \l_tmpa_tl } % hmm... % \exp_args:Nx \providecolors{ \l_tmpa_tl } % \exp_args:Nx \providecolors{ \tl_use:N \l_tmpa_tl } } \ExplSyntaxOff \ProvideColorsFromSet{my-}{}{red,blue} \begin{document} \textcolor{my-red}{red} \textcolor{my-blue}{blue} \end{document} 

Addendum:

Definition of \providecolors in xcolor:

\def\XC@preparec@lor[#1]#2[#3]#4#5% {\XC@sdef\@@cls{#1}\XC@edef\@@nam{#2}% \XC@getmodclr01{#4}{#5}{}% {\xdef\XC@@stack {\ifx\XC@@stack\@empty\else\XC@@stack,\fi{#1}{\@@nam}{#3}% {\ifx\@@Mod\XC@mod@ignore\@@mod\else\@@Mod\fi}{\@@clr}}}} \let\XC@@stack\@empty \def\XC@definecolors#1,% {\@ifxempty{#1}{}{\expandafter\XC@definec@lors#1=#1=:\XC@definecolors}} \def\XC@definec@lors#1=#2=#3:% {\XC@edef\@@nam{#1}\XC@edef\@@arg{#2}% \let\next\XC@definec@l@rs\expandafter\next\XC@@stack,,\@nnil} \def\XC@definec@l@rs#1,% {\ifx,#1,\let\next\remove@to@nnil\else\XC@defin@c@l@rs#1\fi\next} \def\XC@defin@c@l@rs#1#2#3#4#5% {\def\@@tmp{#2}% \ifx\@@tmp\@@arg \let\xglobal@\XC@@glb\XC@@cmd[#1]{\@@nam}[#3]{#4}{#5}% \let\next\remove@to@nnil \fi} \def\providecolors#1% {\let\XC@@cmd\providecolor\let\XC@@glb\xglobal@ \expandafter\XC@definecolors#1,,} 

1 Answer 1

1

You want to build a clist from the given third argument in which every item is prefixed and suffixed as indicated by the first and second argument, then pass the list to \providecolors.

\documentclass{article} %\usepackage{xparse}% no longer needed \usepackage{xcolor} \definecolorsfalse \preparecolorset{HTML}{my-}{}{red,ff0000;blue,0000ff} \ExplSyntaxOn \NewDocumentCommand { \ProvideColorsFromSet } { m m m } { \clist_clear:N \l_tmpa_clist \clist_map_inline:nn { #3 } { \clist_put_right:Nn \l_tmpa_clist { #1##1#2 } } \exp_args:NV \providecolors \l_tmpa_clist } \ExplSyntaxOff \ProvideColorsFromSet{my-}{}{red,blue} \begin{document} \textcolor{my-red}{red} \textcolor{my-blue}{blue} \end{document} 

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.