# Eso-pic and data-tool

Csv files can be easier to prepare for the person who is not used to LaTeX syntax. I would like to use a csv file containing

1. Sebastian, 12.12.2012, XYZ road, XYZ city
2. Rose, 12.12.2013, XYZ road, abc city

I have to define a command in such a way that if I provide input of Id, the respective fields in following example should be automatically populated.

\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example
\AtTextUpperLeft{%
\makebox(420,75)[lt]{%
\footnotesize%
\begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
\textbf{Author}\newline\myauthor&%
\textbf{Date of birth}\newline\mydate&%
\end{tabular}%
}}}
\pagestyle{fancy}

\newcommand{\myauthor}{user34083}
\newcommand{\mydate}{December 26, 1997}
\newcommand{\myaddress}{26 Washington Ave., Manhattan, New York.\newline United States of America}

\begin{document}

\blinddocument % just for the example

\end{document}

• The CSV file doesn't match the headers; can you adjust it? – egreg Dec 16 '13 at 13:33

Here's a possible solution:

\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example

\usepackage{datatool}

% create sample csv file
\begin{filecontents*}{test.csv}
\end{filecontents*}

\AtTextUpperLeft{%
\makebox(420,75)[lt]{%
\footnotesize%
\begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
\textbf{Author}\newline\myauthor&%
\textbf{Date of birth}\newline\mydate&%
\end{tabular}%
}}}
\pagestyle{fancy}

\newcommand{\myauthor}{user34083}
\newcommand{\mydate}{December 26, 1997}
\newcommand{\myaddress}{26 Washington Ave., Manhattan, New York.\newline United States of America}

% Define a command that fetches data for the row with the ID
% given in the argument
\newcommand*{\fetchdata}[1]{%
% fetch the first matching row
\dtlgetrowforvalue{mydata}{\dtlcolumnindex{mydata}{Id}}{#1}%
% Lookup the required values from this row
\dtlgetentryfromcurrentrow{\myauthor}{\dtlcolumnindex{mydata}{myauthor}}%
\dtlgetentryfromcurrentrow{\mydate}{\dtlcolumnindex{mydata}{mydate}}%
}

% fetch data for Id=2
\fetchdata{2}

\begin{document}

\blinddocument % just for the example

\end{document}


This produces (top part of page only):

Edit:

In the above example, the city is in a separate column from the road, so it doesn't appear in the document. If you want both the street and city to be in the myaddress column, then remember to use double-quote delimiters in the CSV file for entries containing a comma:

Id,myauthor,mydate,myaddress


Alternatively, add an extra header and modify \fetchdata accordingly. For example, edit the CSV file so that it looks like:

Id,myauthor,mydate,myroad,mycity

and change \fetchdata to:
\newcommand*{\fetchdata}[1]{%