1

I need to split a string by commas, and I haven't found an easy way to do it in xstring or xparse (although I may be missing something obvious). So if I say:

\def\pleaseHelp{"I, am, confused"}

What is the simplest way to extract:

{"I", "am", "confused"}

from \pleaseHelp and store it as a variable? I feel like this should be simple.

2
  • 2
    Welcome to TeX.SX! Does it have to produce these additional quotes? TeX does not need them to mark it as string. – TeXnician May 3 '18 at 15:36
  • No, it doesn't need the quotes. I should have clarified that. – Kevin Gregory May 3 '18 at 15:38
4

listofitems is a powerful list parsing package. Your example is the simplest of its capability:

\documentclass{article}
\usepackage{listofitems}
\begin{document}
\def\pleaseHelp{I, am, confused}
\readlist*\mylist{\pleaseHelp}% star option removes surrounding whitespace
Individual items:\\
``\mylist[1]'' and\\
``\mylist[2]'' and\\
``\mylist[3]''

Loop over list:
\foreachitem\x\in\mylist[]{\ifnum\xcnt=1\else\ and \fi``\x''}
\end{document}

enter image description here

Additionally, you can change the the parsing separator with the following prior to the \readlist:

\setsepchar{;}

You can parse based on several separators simultaneously with an or (||) separated specification of separators:

\setsepchar{,||.||;||:}
\readlist*\Z{To be, or not to be; that is the question.}

will yield a list containing \Z[1] as To be, \Z[2] as or not to be, \Z[3] as that is the question.

You can do nested parsing with a slash (/) separated list of separators, for example:

\setsepchar{*/,}
\readlist*\Z{this is, a test* of multi-level, parsing}

Then, \Z[1,1] contains this is, \Z[1,2] contains a test, \Z[2,1] contains of multi-level, and \Z[2,2] contains parsing.

When I use the word "contains", I mean "expands to the actual tokens, via two expansions" so that, in the above example, \detokenize\expandafter\expandafter\expandafter{\Z[2,2]} will yield parsing.

2

As you did not post a MWE here's a simple way to access such a comma separated list's items (have a look at http://mirrors.ctan.org/macros/latex/contrib/l3kernel/interface3.pdf for further information):

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand { \giveme } { m m }
{
    \clist_item:nn { #1 } { #2 }
}
\ExplSyntaxOff

Which can be used as:

\giveme{I, am, confused}{1} % would return I which is item 1

If you really would like to store the variable have a look at the l3clist module described in the document I linked above.

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.