1

I am a newbie with latex

I have a file, just for test, ListeEleves.csv

nom,prénom
Avogadro,Amédéo
Bohr,Niels
Copernic,Nicolas
Einstein,Albert

I would like to get

nom,prénom,ID,IDInitials
Avogadro,Amédéo,Avogadro-Amedeo,Avogadro-A
Bohr,Niels,Bohr-Niels,Bohr-N
Copernic,Nicolas,Copernic-Nicolas,Copernic-N
Einstein,Albert,Einstein-Albert,Einstein-A

my code

\documentclass[10pt,a4paper,french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{array,multirow,tabularx}
\usepackage{datatool}
%\DTLsetseparator{;}
\usepackage{fp,pgf,tikz}
\usepackage{ifthen,multido}
\usepackage{babel}
\begin{document}

\DTLloaddb{ListeEleves}{ListeEleves.csv}

\DTLforeach{ListeEleves}{\nom=nom,\prenom=prénom}{%début for each
\DTLforeachkeyinrow{\ItemLu}{%début de for each key in row
%\ifthenelse{\dtlcol >2}{\dtlbreak}{%on limite la lecture aux deux premières colonnes
%suppression des lettres accentuées
\DTLsubstituteall{\ItemLu}{À}{A}
\DTLsubstituteall{\ItemLu}{à}{a}
\DTLsubstituteall{\ItemLu}{Â}{a}
\DTLsubstituteall{\ItemLu}{â}{a}
\DTLsubstituteall{\ItemLu}{È}{E}
\DTLsubstituteall{\ItemLu}{è}{e}
\DTLsubstituteall{\ItemLu}{É}{E}
\DTLsubstituteall{\ItemLu}{é}{e}
\DTLsubstituteall{\ItemLu}{Ê}{E}
\DTLsubstituteall{\ItemLu}{ê}{e}
\DTLsubstituteall{\ItemLu}{Î}{I}
\DTLsubstituteall{\ItemLu}{î}{i}
\DTLsubstituteall{\ItemLu}{Ï}{I}
\DTLsubstituteall{\ItemLu}{ï}{i}
\DTLsubstituteall{\ItemLu}{Ü}{U}
\DTLsubstituteall{\ItemLu}{ü}{u}
\DTLsubstituteall{\ItemLu}{Û}{U}
\DTLsubstituteall{\ItemLu}{û}{u}
\DTLsubstituteall{\ItemLu}{Ù}{u}
\DTLsubstituteall{\ItemLu}{ù}{u}
\DTLsubstituteall{\ItemLu}{Ô}{o}
\DTLsubstituteall{\ItemLu}{ô}{o}
\DTLsubstituteall{\ItemLu}{'}{}
\DTLsubstituteall{\ItemLu}{ }{}
%fin suppression des lettres accentuées
%}%
\DTLifeq{\dtlcol}{1}{\FPset\NOM{\ItemLu}}{\relax}
\DTLifeq{\dtlcol}{2}{\FPset\PRENOM{\ItemLu}}{\relax}
}%fin de for each key in row
\NOM \PRENOM
}%fin for each

\DTLdisplaydb{ListeEleves}

\end{document}

1) Is it a more easier way to encode lines \DTLifeq{\dtlcol}{1}{\FPset\NOM{\ItemLu}}{\relax} and \DTLifeq{\dtlcol}{2}{\FPset\PRENOM{\ItemLu}}{\relax}

2) I can not manage to automatically add a new entry/column with \DTLnewdbentry{ListeEleves}{ID}{\NOM-Prenom} even \dtlgetentryfromcurrentrow or \dtlreplaceentryincurrentrow

3) \DTLstoreinitials{\Ini}{\PRENOM} does not work with a command name

Do you have any idée ?

Thanks

2

2 Answers 2

2

1) Is it a more easier way to encode lines \DTLifeq{\dtlcol}{1}{\FPset\NOM{\ItemLu}}{\relax} and \DTLifeq{\dtlcol}{2}{\FPset\PRENOM{\ItemLu}}{\relax}

Yes, there is an easier way. I don't understand why you're iterating through all the keys when you have already accessed them in the assignment {\nom=nom,\prenom=prénom}. As far as I can tell, you're trying to strip the accents from these two values. This can be done as follows (since you're using \usepackage[utf8]{inputenc}):

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{datatool}

\DTLloaddb{ListeEleves}{ListeEleves.csv}

\makeatletter
\newcommand*{\stripaccents}[2]{%
  \begingroup
  \def\IeC##1{\@thirdofthree##1}%
  \protected@edef\x{\endgroup\noexpand\def\noexpand#1{#2}}%
  \x
}
\makeatother

\begin{document}

\DTLforeach{ListeEleves}{\nom=nom,\prenom=prénom}{%
  \stripaccents{\NOM}{\nom}%
  \stripaccents{\PRENOM}{\prenom}%
  \NOM \PRENOM
}

\DTLdisplaydb{ListeEleves}

\end{document}

2) I can not manage to automatically add a new entry/column with \DTLnewdbentry{ListeEleves}{ID}{\NOM-Prenom} even \dtlgetentryfromcurrentrow or \dtlreplaceentryincurrentrow

You already worked this one out in the cross-post on LC. You need to switch on the expansion when adding values containing commands that need expanding:

\dtlexpandnewvalue

\DTLforeach{ListeEleves}{\nom=nom,\prenom=prénom}{%
  \stripaccents{\NOM}{\nom}%
  \stripaccents{\PRENOM}{\prenom}%
  \DTLappendtorow{ID}{\NOM-\PRENOM}%
}

3) \DTLstoreinitials{\Ini}{\PRENOM} does not work with a command name

The first argument of \DTLstoreinitials must be a string not a command as no expansion is performed. Since there's no \Ini in your code, I'm guessing you have the arguments the wrong way round here and that you want the initials stored in \Ini. So you need to do:

\expandafter\DTLstoreinitials\expandafter{\PRENOM}{\Ini}

The complete document is:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{datatool}

\DTLloaddb{ListeEleves}{ListeEleves.csv}

\makeatletter
\newcommand*{\stripaccents}[2]{%
  \begingroup
  \def\IeC##1{\@thirdofthree##1}%
  \protected@edef\x{\endgroup\noexpand\def\noexpand#1{#2}}%
  \x
}
\makeatother

\begin{document}

\dtlexpandnewvalue
\renewcommand*{\DTLafterinitials}{}

\DTLforeach{ListeEleves}{\nom=nom,\prenom=prénom}{%
  \stripaccents{\NOM}{\nom}%
  \stripaccents{\PRENOM}{\prenom}%
  \DTLappendtorow{ID}{\NOM-\PRENOM}%
  \expandafter\DTLstoreinitials\expandafter{\PRENOM}{\Ini}%
  \DTLappendtorow{IDInitials}{\NOM-\Ini}%
}

\DTLdisplaydb{ListeEleves}

\end{document}

This produces:

image of result

which replicates your requested data:

nom,prénom,ID,IDInitials
Avogadro,Amédéo,Avogadro-Amedeo,Avogadro-A
Bohr,Niels,Bohr-Niels,Bohr-N
Copernic,Nicolas,Copernic-Nicolas,Copernic-N
Einstein,Albert,Einstein-Albert,Einstein-A
0
\DTLforeach{ListeCompetencesEleves}{\nom=nom,\prenom=prénom}{%
\def\IdQuestion{}
\DTLforeachkeyinrow{\ItemLu}{%
\ifthenelse{\dtlcol >2}{\dtlbreak}{%
\StrSubstitute{\ItemLu}{ }{}[\ItemLu]
\StrSubstitute{\ItemLu}{'}{}[\ItemLu]
\StrSubstitute{\ItemLu}{.}{}[\ItemLu]
\StrSubstitute{\ItemLu}{-}{}[\ItemLu]
\StrSubstitute{\ItemLu}{À}{A}[\ItemLu]
\StrSubstitute{\ItemLu}{à}{a}[\ItemLu]
\StrSubstitute{\ItemLu}{Â}{A}[\ItemLu]
\StrSubstitute{\ItemLu}{â}{a}[\ItemLu]
\StrSubstitute{\ItemLu}{È}{E}[\ItemLu]
\StrSubstitute{\ItemLu}{è}{e}[\ItemLu]
\StrSubstitute{\ItemLu}{É}{E}[\ItemLu]
\StrSubstitute{\ItemLu}{é}{e}[\ItemLu]
\StrSubstitute{\ItemLu}{Ê}{E}[\ItemLu]
\StrSubstitute{\ItemLu}{ê}{e}[\ItemLu]
\StrSubstitute{\ItemLu}{Î}{I}[\ItemLu]
\StrSubstitute{\ItemLu}{î}{i}[\ItemLu]
\StrSubstitute{\ItemLu}{Ï}{I}[\ItemLu]
\StrSubstitute{\ItemLu}{ï}{i}[\ItemLu]
\StrSubstitute{\ItemLu}{Ü}{U}[\ItemLu]
\StrSubstitute{\ItemLu}{ü}{u}[\ItemLu]
\StrSubstitute{\ItemLu}{Û}{U}[\ItemLu]
\StrSubstitute{\ItemLu}{û}{u}[\ItemLu]
\StrSubstitute{\ItemLu}{Ù}{u}[\ItemLu]
\StrSubstitute{\ItemLu}{ù}{u}[\ItemLu]
\StrSubstitute{\ItemLu}{Ô}{O}[\ItemLu]
\StrSubstitute{\ItemLu}{ô}{o}[\ItemLu]
\StrSubstitute{\ItemLu}{Ö}{O}[\ItemLu]
\StrSubstitute{\ItemLu}{ö}{o}[\ItemLu]
   }%
\xdef\IdQuestion{\IdQuestion\ItemLu-}
}%
\DTLgetvalue{\NOM}{ListeCompetencesEleves}{\DTLcurrentindex}{\dtlcolumnindex{ListeCompetencesEleves}{nom}}%
\DTLgetvalue{\PRENOM}{ListeCompetencesEleves}{\DTLcurrentindex}{\dtlcolumnindex{ListeCompetencesEleves}{prénom}}%
\StrChar{\PRENOM}{1}[\Initiale]%
\def\Patronyme{\NOM~\Initiale}% 
\StrGobbleRight{\IdQuestion}{2}[\IdQuestion]%
\DTLappendtorow{IdQuestion}{\IdQuestion}%
\DTLappendtorow{Patronyme}{\Patronyme}%
}%
1
  • 2
    Is this supposed to be an answer? This is just some uncompilable snippet of code. Commented Jun 5, 2016 at 9:38

You must log in to answer this question.

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