3

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


Id, myauthor, mydate, myaddress

  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
\usepackage[headsep=3cm,top=5cm]{geometry}
\AddToShipoutPicture{%
  \AtTextUpperLeft{%
    \makebox(420,75)[lt]{%
      \footnotesize%
      \begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
      \textbf{Author}\newline\myauthor&%
      \textbf{Date of birth}\newline\mydate&%
      \textbf{Address}\newline\myaddress%
      \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
7

Here's a possible solution:

\documentclass{article}
\usepackage{eso-pic}
\usepackage{fancyhdr}
\usepackage{blindtext} % just for the example
\usepackage[headsep=3cm,top=5cm]{geometry}

\usepackage{datatool}

% create sample csv file
\begin{filecontents*}{test.csv}
Id,myauthor,mydate,myaddress
1,Sebastian,12.12.2012,XYZ road,XYZ city
2,Rose,12.12.2013,XYZ road,abc city
\end{filecontents*}

% load data
\DTLloaddb{mydata}{test.csv}

\AddToShipoutPicture{%
  \AtTextUpperLeft{%
    \makebox(420,75)[lt]{%
      \footnotesize%
      \begin{tabular}{@{}*{3}{p{4.5cm}}@{}}%
      \textbf{Author}\newline\myauthor&%
      \textbf{Date of birth}\newline\mydate&%
      \textbf{Address}\newline\myaddress%
      \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}}%
   \dtlgetentryfromcurrentrow{\myaddress}{\dtlcolumnindex{mydata}{myaddress}}%
}

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

\begin{document}

\blinddocument % just for the example

\end{document}

This produces (top part of page only):

Image of resulting text

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
1,Sebastian,12.12.2012,"XYZ road, XYZ city"
2,Rose,12.12.2013,"XYZ road, abc city"

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
1,Sebastian,12.12.2012,XYZ road,XYZ city
2,Rose,12.12.2013,XYZ road,abc city

and change \fetchdata to:

\newcommand*{\fetchdata}[1]{%
   \dtlgetrowforvalue{mydata}{\dtlcolumnindex{mydata}{Id}}{#1}%
   \dtlgetentryfromcurrentrow{\myauthor}{\dtlcolumnindex{mydata}{myauthor}}%
   \dtlgetentryfromcurrentrow{\mydate}{\dtlcolumnindex{mydata}{mydate}}%
   \dtlgetentryfromcurrentrow{\myroad}{\dtlcolumnindex{mydata}{myroad}}%
   \dtlgetentryfromcurrentrow{\mycity}{\dtlcolumnindex{mydata}{mycity}}%
   \renewcommand*{\myaddress}{\myroad, \mycity}%
}

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.