4

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?

3

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}
  \csvreader[no head]{names.csv}{1=\name, 2=\surname, 3=\affiliation}{%
  \item \name\ \surname\ at \affiliation
  }
\end{itemize}

Badges:\newline
\csvreader[no head]{names.csv}{1=\name, 2=\surname, 3=\affiliation}{%
  \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}

enter image description here

  • 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. – Alberto Apr 26 '17 at 17:24
1

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}

\usepackage{readarray}
\readarraysepchar{,}
\begin{document}

\readdef{names.csv}\namedata

\readarray\namedata\names[-,\ncols]

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

enter image description here

0

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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