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...

SamHoff
  • 89
  • 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 09: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
  • @touhami: The 2nd argument is strange indeed ;-) –  Sep 25 '15 at 16:26
  • @touhami, i will get more parameters as seen in the example and they will be grouped, so i get 12 or 13 values in the end... And so, i need a solution, that is not restricted to nine parameters... – SamHoff Sep 28 '15 at 06:48

1 Answers1

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)

  • @egreg: I know: I've omitted the ;-) I'll change –  Sep 25 '15 at 16:42
  • @egreg: Better? ;-) –  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 –  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 06:43
  • @SamHoff: Yes, but I don't recommend this. It's no good way, in my point of view for this {a,b,c,d} stuff –  Sep 29 '15 at 11:45
  • oh, okay... :-/ So, did you have a better approach for that? – SamHoff Sep 30 '15 at 10:11
  • @SamHoff: In my opinion the {a,b,c,d}{e,f,g}{...} etc. approach is not really useful unless you explicitly explain why this is should be done this way and no other –  Sep 30 '15 at 13:39
  • i will get the table data like this

    \row{day, date}{come, go,hours,days}{project}

    from an external program. This program commits 30 or 40 records and then i have to create a time sheet in LaTex. I also think, that this is not useful, but that's the way I get the data. So, if you have a soltution in a completely different way for me, I would be deeply grateful.

    – SamHoff Oct 05 '15 at 06:50