1

I am using csvreader for dynamic reporting.

I would like to call values from specific cells using the csvreader function for example:

Thank you \csvreader[head to column names, filter={\thecsvinputline=2}]{sample.csv}{}{\csvcolii} \csvreader[head to column names, filter={\thecsvinputline=2}]{sample.csv}{}{\csvcoliii.}

To write "Thank you Mark Smith"

Where sample.csv containts for example:

Name,LastName
Mark,Smith
x,y
a,b

If I do this I actually get: "Thank you Name,LastName Smith"

The information will not necessarily be in the same line of the csv file...

Any ideas of how to fix this or do it differently?

Thank you! :)

3 Answers 3

1

You can look example on the csvsimple package https://www.ctan.org/pkg/csvsimple

\csvnames{my names}{Name=\surname,LastName=\givenname}

\csvreader[my names, late after line=\\, late after last line=]%
  {example.csv}{}{%
    Thank you \givenname\ \surname\ %was born in \birthyear\ and lives in \address.
}

So you need to use directly the header string and put into variable after that you can use it easily.

1

Here is a way with readarray:

\documentclass{article}
\usepackage{readarray}
\begin{filecontents*}{sample.csv}
Name,LastName
Mark,Smith
x,y
a,b
\end{filecontents*}
\newcommand\fullname[1]{\edef\tmp{\the\numexpr#1+1\relax}%
  \sample[\tmp,1] \sample[\tmp,2]}
\begin{document}
\readarraysepchar{,}
\readdef{sample.csv}\sampledata
\readarray\sampledata\sample[,2]

Thank you, \fullname{1}.

Third name is \fullname{3}.
\end{document}

enter image description here

1

Another solution using datatool.

\documentclass{article}
\usepackage{datatool}
\begin{filecontents*}{sample.csv}
    Name,LastName
    Mark,Smith
    x,y
    a,b
\end{filecontents*}
\DTLloaddb{data}{sample.csv}

\newcommand\fullname[1]{%
    \DTLassign{data}{#1}{\dbName=Name,\dbLastName=LastName}%
    \dbName~{\scshape\dbLastName}%
}

\begin{document}
    Thank you, \fullname{1}.
    
    Third name is \fullname{3}.
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .