4

I need a pretty simple thing - when I get a macro parameter which is a string delimited with commas, can I split it and assign resulting strings into an array?

In short, I'd like to write something like this:

\newcommand{\test}[1]{
    \def\array = \parse{#1}{,}
}

So I could later use it like this:

\someOtherMacro{\array[1]}

I was looking at the other answers, but I'm not able to find something really simple for this task (just parsing string and assigning results).

2
  • You mean something like this: \def\com#1,#2{\def\array{#1}}. i.e. \com{Hallo,Tübb} will result in \array -> Hallo. Commented Nov 11, 2016 at 15:02
  • Related: Data structures for TeX
    – Werner
    Commented Nov 11, 2016 at 17:01

3 Answers 3

2

You can consider using arrayjobx:

enter image description here

\documentclass{article}

\usepackage{arrayjobx}

\makeatletter
\renewcommand{\readarray}[3][&]{\read@array[#1]{#2}{#3}}%
\makeatother

\newcommand{\someOtherMacro}{\textbf}

\begin{document}

\newarray\mynames

\readarray{mynames}{Moe&Larry&Curly}

1: \mynames(1) and \someOtherMacro{\mynames(2)} and \mynames(3)

\readarray[,]{mynames}{Moe,Larry,Curly}

2: \mynames(1) and \mynames(2) and \someOtherMacro{\mynames(3)}

\end{document}
7

The listofitems package is very powerful in this regard. The first example parses based on a , separator.

The 2nd example is a two-tiered parsing, first by way of \\, and then again, using a space separator.

Parsing to n-tiers poses no problem for the package. It also has options to discard blank array elements, if desired.

In both cases, leading/trailing spaces have been excised, by using the star form of the \readlist invocation.

\documentclass{article}
\usepackage{listofitems}
\begin{document}
\setsepchar{,}
\readlist*\parsed{This, is, my , list, of words}
There are \parsedlen{} items in list

Here are the items:
\showitems\parsed

item 4 is ``\parsed[4]''.

\setsepchar{\\/ }
\readlist*\multiparsed{%
This is a test\\
of the\\
emergency broadcast system.%
}

Here is the other list:
\showitems\multiparsed

Row 2 is ``\multiparsed[2].''

2nd word of row 3 is ``\multiparsed[3,2]''.
\end{document}

enter image description here

1
  • Thank you very much! But I'm working on a shared document on OverLeaf and it doesn't support listofitems. Do you have any other solution?
    – Eenoku
    Commented Nov 11, 2016 at 16:12
5

It's very easy with expl3.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\definearray}{mm}
 {
  \seq_new:c { g_eenoku_array_#1_seq }
  \seq_gset_split:cnn { g_eenoku_array_#1_seq } { , } { #2 }
 }
\cs_generate_variant:Nn \seq_gset_split:Nnn { c }
\NewDocumentCommand{\usearray}{mm}
 {
  \seq_item:cn { g_eenoku_array_#1_seq } { #2 }
 }
\ExplSyntaxOff

\definearray{names}{Moe,Larry,Curly}

\begin{document}

\usearray{names}{1}

\usearray{names}{2}

\usearray{names}{3}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .