# Make array in LaTeX

Is there a way to make an array in LaTeX. I want to write a function that does this (here the array has only 2 elements but I want to be able to have many as I want).

\foo{\MakeArray{foo,bar}}


foo|bar-foo&bar

I could have done this like that

\newcommand\foo[2]{\1-\2}
\foo{foo|bar}{foo&bar}


But I need to enter the elements twice which is not very neat...

-
 Welcome to TeX.SE. It would be helpful to know exactly what you want to accomplish. There is the arrayjobx pacakge, but I tend to just use comma separated lists, and use pgf's \foreach to process them. newcommand{\fooArray}{foo,bar} should suffice. – Peter Grill Sep 15 '12 at 0:12

## 1 Answer

An easy way to do this would be to define \foo like this:

\newcommand{\foo}[2]{#1$|$#2-#1\&#2}


This allows you to use \foo{foo}{bar} to obtain, as output, foo|bar-foo&bar:

However, for lengthier inputs, you can either process the contents as a list (using etoolbox or some LaTeX3 constructs), or you can do a search-and-replace on the list separator. xstring provides such functionality:

\documentclass{article}
\usepackage{xstring}% http://ctan.org/pkg/xstring
\newcommand{\foo}[1]{%
\StrSubstitute{#1}{,}{$|$}-\StrSubstitute{#1}{,}{\&}}
\begin{document}
\foo{foo,bar} \par
\foo{foo,bar,baz,boo,far,zab}
\end{document}

-