Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
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:

enter image description here

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:

enter image description here

\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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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