3

I have something like this:

\documentclass{article}

\newcommand*\tableData[2]{string1,string2,string3}{string4}

\newcommand*{\String1}[1]{#1}
\newcommand*{\String2}[1]{#1}
\newcommand*{\String3}[1]{#1}
\newcommand*{\String4}[1]{#1}

\begin{document}

 \String1 
 ..
 \String4

\end{document}

So, i have two parameters, but i want to split the commaseparated strings in the first parameter and put it in the variables. How to do this with build-in-command or is it a better way to work with a package?

I found this already, but i want to put the different strings in different variables, not in \fbox or something else...

7
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – Martin Schröder Sep 25 '15 at 9:33
  • 1
    Note that \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
  • @Mico I'll consider this, thank you, it should be an example for my problem. – SamHoff Sep 25 '15 at 10:14
  • 1
    why {string1,string2,string3}{string4} and not {string1,string2,string3,string4}? – touhami Sep 25 '15 at 11:30
  • 1
    you can try this \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
3

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)

9
  • @egreg: I know: I've omitted the <module> ;-) I'll change – user31729 Sep 25 '15 at 16:42
  • @egreg: Better? ;-) – user31729 Sep 25 '15 at 16:45
  • @ChristianHupfer, thank you, that works for now, but i didn't understand the entire code and the documentary of the xparse-package is unfortunately not clear for me.. I think my english is not good enough for that. So, ist it possible to explain the lines from \ExplSyntaxOn to \ExplSyntaxOff ? – SamHoff Sep 28 '15 at 10:17
  • @SamHoff: See the explanations – user31729 Sep 28 '15 at 10:29
  • thank you, it helps a lot and i'm going to understand it a little bit. Only one question: when i get more arguments in table data, like \tableData{a,s,d}{q,w,e,r}{j,k,l}{t,z}, then i only have to change the mandatory argument to {mmmm} or did i misunderstood this..?? – SamHoff Sep 29 '15 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.