# Creating string array from csv file

I would like to create some name badges using a loop. I have a spreadsheet with names and affiliations that I save in names.csv file as follows

"Name1","Surname1","Affiliation1",
"Name2","Surname2","Affiliation2",
"Name3","Surname3","Affiliation3"


I can copy the content of names.csv in a string array and use a loop to recover the entries. The following is an example of working code producing the output Surname2:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{array}

\def\names{{"Name1","Surname1","Affiliation1",
"Name2","Surname2","Affiliation2",
"Name3","Surname3","Affiliation3"}}

\begin{document}

\pgfmathparse{\names[4]}\pgfmathresult

\end{document}


I can use this idea to create hundreds of name badges in a loop. But instead of writing the whole list in the .tex file I would like to call directly my names.csv file. I used the modified code

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{array}

\def\names{{\input{names.csv}}}

\begin{document}

\pgfmathparse{\names[4]}\pgfmathresult

\end{document}


which apparently enters in an endless loop. I understood that \input{} works just as copy-pastying, but it doesn't seem the case. Of course I can work in the spreadsheet and then copy paste the names.csv file content, but I would like to understand what happens. Any clues?

PS edit: The suggested solution is enough for my case. But still I would like to be able to define macros with \input'. It seems that\input{}' inside a macro is not expanded immediately. I mean if I have some code in file.tex', that I want to plugin in a new macro,\def\mymacro{\input{file}}' will save \mymacro' as '\input{file}' and will only look what's inside the file when I call the macro in the document. That's why in the previous example\names' is not understood as an array. In the same way

\begin{filecontent*}{newfile.tex}
\input{file}
\end{filecontent*}


will save the code \input{file}' innewfile.tex', but not the content in file.tex'. Anyone has any idea how to force input to expand?

Instead of importing the whole csv file you can read it row wise. There are some packages for this, here I use csvsimple. The file names.csv is created by the filecontents environment just to have it all in one file for this example.

\documentclass{article}
\usepackage{filecontents}
%% Create file
\begin{filecontents*}{names.csv}
Name1, Surname1, Affiliation1
Name2, S. Urname2, A f filiation2
Name3, Surname3, Affiliation3
Name4, Surname4, Affiliation4
Name5, Surname5, Affiliation5
\end{filecontents*}
\usepackage{tikz}
\usepackage{csvsimple}
\begin{document}
List of participants:
\begin{itemize}
\item \name\ \surname\ at \affiliation
}
\end{itemize}

\begin{tikzpicture}
\draw[clip] (0,0) rectangle (6,4);
\node at (3,2){\Large\name\ \surname};
\node at (3,1.3){\affiliation};
%% Logo
\fill[color=red!40] (0.7,3.3) circle(1);
\node[fill=green!40,opacity=0.5,rounded corners=5pt,anchor=west] at (0.8,3.5){\textsf{The Epic Event}};
\node[anchor=west] at (0.8,3.5){\textsf{The Epic Event}};
\end{tikzpicture}
}

\end{document}


• This solution is nice enough for my case, and runs smoothly. But still I wonder if it is possible to work with arrays, for a more flexible case. Apr 26, 2017 at 17:24

The readarray package is set up to digest file data into arrays.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{names.csv}
Name1,Surname1,Affiliation1,
Name2,Surname2,Affiliation2,
Name3,Surname3,Affiliation3
\end{filecontents*}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{array}

\begin{document}

\names[2,2] and \names[3,3]
\end{document}


You need to define \names so that its definition is the contents of the file. This means some expansion is required:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf}
\usepackage{array}

\begingroup
\makeatletter
\endlinechar=-1\relax
\everyeof{\noexpand}\relax
\edef\x{\endgroup\def\noexpand\names{{\@@input names.csv }}}\x

\begin{document}

\pgfmathparse{\names[4]}\pgfmathresult

\end{document}

• Can you explain why and how this works to load the contents of a file into a macro? I tried forever with \edef and \expandafter to get just the actual text in a file expanded into a macro, and I kept getting weird complaints like Argument of \@iinput has an extra } that I can't parse. What is \@@input, as opposed to normal \input? Why are you creating a group and then ending it inside \x? Why do you need to define \x at all if you then immediately use it? Where is the documentation for \everyeof` and how do you know to use it here? May 10, 2021 at 20:57