Take the 2-minute tour ×
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...

EDIT: Sadly, even if your answer solve my question, it doesn't work with my exact case so I'll tell it exactly. I currently have

\newcommand{\authors}[2][e ]{
  \usepackage[pdfauthor={#1}]{hyperref}
  \author{#2}
}

that I use that way

\authors{John Doe, Dark Vador and Yoda}{John Doe \and Dark Vador \and Yoda}

and I would like to have the same result without specifying it twice. However, the \and doesn't work with the proposed solution.

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
    
I have told exactly what I want to do now :) –  Benoît Legat Jul 16 '13 at 19:32
add comment

4 Answers

up vote 11 down vote accepted

Load the hyperref package first, and then set the PDF document properties using \hypersetup:

enter image description here

\documentclass{article}
\usepackage{hyperref}% http://ctan.org/pkg/hyperref
\newcommand{\authors}[1]{
  {\renewcommand{\and}{\unskip, }\hypersetup{pdfauthor={#1}}}
  \author{#1}
}
\title{My title}
\authors{John Doe \and Dark Vador \and Yoda}
\begin{document}
\maketitle
\end{document}

Inside \authors, the definition of \and is momentarily updated to insert commas.

share|improve this answer
add comment

For the special case, there is also a cheap solution. \and can be redfined for hyperref's pdfauthor:

\documentclass{article}
\usepackage{hyperref}

\pdfstringdefDisableCommands{\def\and{, }} 

\newcommand*{\authors}[1]{%   
  \hypersetup{pdfauthor={#1}}%
  \author{#1}%
}
\title{Test document}
\authors{John Doe\and Dark Vador\and Yoda}% without space before \and
\begin{document}
\maketitle
\end{document}

Result

And pdfinfo reports:

Author:         John Doe, Dark Vador, Yoda
share|improve this answer
add comment

The real application is almost trivial with xparse and expl3.

\documentclass{article}
\usepackage{xparse}
\usepackage{hyperref}

\ExplSyntaxOn
\NewDocumentCommand{\authors}{O{,}m}
 {
  \legat_authors:nn { #1 } { #2 }
 }
\seq_new:N \g_legat_authors_seq

\cs_new_protected:Npn \legat_authors:nn #1 #2
 {
  \seq_gset_split:Nnn \g_legat_authors_seq { #1 } { #2 }
  \use:x
   {
    \exp_not:N \hypersetup {pdfauthor={ \seq_use:Nn \g_legat_authors_seq { , } } }
   }
  \author{ \seq_use:Nn \g_legat_authors_seq { \and } }
 }
\ExplSyntaxOff

\begin{document}
\authors{John Doe, Dark Vador, Yoda}
\title{Star Wears}

\maketitle

\end{document}

The only subtle point is passing the list of authors expanded to \hypersetup, which is obtained by \use:x.

The command \authors has an optional argument; if a different delimiter than a comma is needed one can set it in the optional argument:

\authors[;]{Dean Martin; Sammy Davis, Jr; Jerry Lewis}

Note that this requires an up-to-date TeX distribution.

enter image description here

How does it work? The \seq_gset_split:Nnn function (globally) stores the third argument as elements of the sequence, using the second argument as delimiter between items. This sequence can then be used as many times as one wants. Here we use it first for passing the comma separated list of authors to \hypersetup, then as the argument to \author.

share|improve this answer
    
Your solution is great, sadly, I'm looking for a simple solution that works through many LaTeX version :) –  Benoît Legat Jul 17 '13 at 11:07
add comment

Since I couldn't get yours to give any output, I changed some definitions around to produce parsed output of the form you requested.

\documentclass{article}
\usepackage{stringstrings}

\renewcommand\and{\textbf{AND}}
\newcommand\MYusepackage[2][1]{[#1]\{#2\}\par}
\newcommand\MYauthor[1]{#1}

\newcommand\authors[1]{%
  \MYusepackage[pdfauthor={#1}]{hyperref}%
  \encodetoken{\and}
  \convertword[e]{#1}{~and~}{, }%
  \convertchar[e]{\thestring}{,}{ \and}%
  \retokenize{\thestring}%
  \MYauthor{\thestring}%
  \decodetoken{\and}%
}
\begin{document}
\authors{John Doe, Dark Vador and Yoda}
\end{document}

enter image description here

share|improve this answer
add comment

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.