Generalcsvtools | Print Text based on Condition

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
i4shagi
Posts: 2
Joined: Fri Nov 09, 2012 6:29 am

csvtools | Print Text based on Condition

Post by i4shagi »

Hi,

I am generating letter using csvtools package. I have a CSV file which has three fields: Name, address, grade.

I wrote the code, but I want to print a part of text based on condition (data from the CSV file). For example if grade is "A" then print "distincation" for that student, if grade is "B" then print "firstclass" for that student.

Please let me know how can I do this .

Regards
Shagi

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

csvtools | Print Text based on Condition

Post by nlct »

csvtools is obsolete and has been replaced by datatool. Here's a minimal example:

Code: Select all

\documentclass{letter}

\usepackage{etoolbox}
\usepackage{datatool}

\begin{filecontents*}{test.csv}
Name,Address,Grade
A.N. Other,1 The Street,A
Some One,2 The Road,B
\end{filecontents*}

\DTLloaddb{mydata}{test.csv}

\newcommand{\Agrade}{distinction}
\newcommand{\Bgrade}{first class}

\newcommand{\gradelabel}[1]{%
 \ifcsdef{#1grade}{\csuse{#1grade}}{unknown classification}%
}

\begin{document}

\DTLforeach*{mydata}{\Name=Name,\Address=Address,\Grade=Grade}{%
 \begin{letter}{\Name\\\Address}

 \opening{Dear \Name}

 You grade is \Grade\ (\gradelabel{\Grade}).

 \end{letter}
}

\end{document}
Regards
Nicola Talbot
Post Reply