I suggest the clist
facilities by expl3
and a \GetString{number}
macro which fetches the relevant string with number number
instead a bunch of \StringOne
etc. routines
Edit: Please note that the list container \g_samhoff_string_clist
is cleared each time \tableData
is used.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\clist_new:N \g_samhoff_string_clist
\NewDocumentCommand{\tableData}{mm}{
\clist_clear:N \g_samhoff_string_clist % Clearing the list
\clist_set:Nn{\g_samhoff_string_clist}{#1}
\clist_put_right:Nn{\g_samhoff_string_clist}{#2}
}
\NewDocumentCommand{\GetString}{m}{
\clist_item:Nn{\g_samhoff_string_clist}{#1}
}
\ExplSyntaxOff
\begin{document}
\tableData{one, two, three}{four}
Now the data:
\GetString{1}
\GetString{2}
\GetString{3}
\GetString{4}
\end{document}
Explanation:
\tableData
is defined to be a macro with two mandatory arguments, therefore {mm}
The first argument is supposed to have the comma-separated-value list.
\clist_new:N \g_samhoff_string_clist
defines a new csv - list variable globally with name g_samhoff_string_clist. (\g
stands for global)
- At the begin of
\tableData
, clear the list: \clist_clear:N
- Now store the first parameter (the clist!) with
\clist_set:Nn
- Append the 2nd parameter to right side of
\g_samhoff_string_clist}
with \clist_put_right:Nn
With \clist_item{\g_samhoff_string_clist}{2}
for example it's possible to fetch the 2nd item in the list.
\ExplSyntaxOn
and \ExplSyntaxOff
are necessary to make the new macro names possible (and other stuff)
\String1
,\String2
,\String3
, and\String4
are not valid TeX macro names. You probably need to consider naming them\StringA
,\StringB
,\StringC
, and\StringD
. – Mico Sep 25 '15 at 10:08{string1,string2,string3}{string4}
and not{string1,string2,string3,string4}
? – touhami Sep 25 '15 at 11:30\documentclass[11pt]{article} \def\tableData #1,#2,#3,#4{% \def\stringA{#1}% \def\stringB{#2}% \def\stringC{#3}% \def\stringD{#4}} \begin{document} \tableData string1,string2,string3,{string4} \stringA \stringB \stringC \stringD \end{document}
– touhami Sep 25 '15 at 11:37