0

I am trying to call a specific command in combination with a argument given. The Idea is to spimplify a tedious and (semi)repetaive command.

Example what I mean:

\newcommand{\HelloA}{something specific}

\newcommand{\HelloB}{something else specific}

\newcommand{\HelloC}{something extra specific}

...

\newcommand{\HelloZ}{something super weird}

\newcommand{\SomeCommand}[1][2][3]{\Hello#1 \Hello#2 \Hello#3}

With the last command I want to call my "\Hello[...]" command by giving it the "Name" or characters needed to complete the command. -> I would like to write:

\SomeCommand{A}{C}{Z}

->and it sould execute my \HelloA, \HelloB and \HelloZ command

Thank you very much for your help!

2 Answers2

1

I'd do it with property lists.

You can define a property list based on an arbitrary name, in the example Hello and populate it.

Then you can extract the items by key.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\makelist}{mm} {% #1 = list name % #2 = key-value pairs \prop_new:c { g_simsalabim_list_#1_prop } \prop_gset_from_keyval:cn { g_simsalabim_list_#1_prop } { #2 } }

\NewDocumentCommand{\getfromlist}{+O{~}mm} {% #1 = optional separator, default a space % #2 = list name % #3 = list of keys \simsalabim_list_get:nnn { #1 } { #2 } { #3 } }

\seq_new:N \l__simsalabim_list_get_seq

\cs_new_protected:Nn \simsalabim_list_get:nnn { % clear the temporary sequence \seq_clear:N \l__simsalabim_list_get_seq % populate it by cycling over the last argument \clist_map_inline:nn { #3 } { \seq_put_right:Nx \l__simsalabim_list_get_seq { \prop_item:cn { g_simsalabim_list_#2_prop } { ##1 } } } % output the items with the desired separator \seq_use:Nn \l__simsalabim_list_get_seq { #1 } }

\ExplSyntaxOff

\makelist{Hello}{ A=something specific, B=something else specific, C=something extra specific, Z=something super weird }

\begin{document}

With spaces: \getfromlist{Hello}{A,C,Z}

Paragraphs

\getfromlist[\par]{Hello}{Z,B,A}

\end{document}

enter image description here

Note: if an item contains either a comma or an = character, you have to use braces like below

X={math, $a=b$},

If you need to add to lists (or replace values), you can do as follows.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\makelist}{mm} {% #1 = list name % #2 = key-value pairs \prop_new:c { g_simsalabim_list_#1_prop } \prop_gset_from_keyval:cn { g_simsalabim_list_#1_prop } { #2 } }

\NewDocumentCommand{\addtolist}{mm} {% #1 = list name % #2 = key-value pairs \prop_gput_from_keyval:cn { g_simsalabim_list_#1_prop } { #2 } }

\NewDocumentCommand{\getfromlist}{+O{~}mm} {% #1 = optional separator, default a space % #2 = list name % #3 = list of keys \simsalabim_list_get:nnn { #1 } { #2 } { #3 } }

\seq_new:N \l__simsalabim_list_get_seq

\cs_new_protected:Nn \simsalabim_list_get:nnn { \seq_clear:N \l__simsalabim_list_get_seq \clist_map_inline:nn { #3 } { \seq_put_right:Nx \l__simsalabim_list_get_seq { \prop_item:cn { g_simsalabim_list_#2_prop } { ##1 } } } \seq_use:Nn \l__simsalabim_list_get_seq { #1 } }

\ExplSyntaxOff

\makelist{Hello}{ A=something specific, B=something else specific, C=something extra specific, Z=something super weird } \addtolist{Hello}{ X={math, $a=b$}, Y=whatever, }

\begin{document}

With spaces: \getfromlist{Hello}{A,C,Z,X}

Paragraphs

\getfromlist[\par]{Hello}{Z,B,A,Y}

\end{document}

enter image description here

egreg
  • 1,121,712
0

If your LaTeX installation is up to date (2022-06-01 version) you can do

\newcommand{\SomeCommand}[3]{\UseName{Hello#1} \UseName{Hello#2} \UseName{Hello#3}}
nim
  • 335