LaTeX forum ⇒ GeneralDatatool : search key, add up values, copy & past in a list Topic is solved

LaTeX specific issues not fitting into one of the other forums of this category.
pasbal
Posts: 12
Joined: Thu Jun 26, 2014 10:00 pm

Datatool : search key, add up values, copy & past in a list

Postby pasbal » Fri Apr 08, 2016 6:53 pm

Hello,

I have many difficulties to use datatool package because i am new with latex.
I have a CSV file made up something like this:

  1. \begin{filecontents}{dbTest.csv}
  2. ColA,ColB,ColC
  3. A,1,A
  4. B,1.5,Ba
  5. A,1.25,DSa
  6. D,2.5,Aza
  7. B,0.5,RE
  8. \end{filecontents}


  • How can i reuse \DTLrowcount{dbTest} with an mathematical operation ?
    Expected result : Rows = 5
  • I would like to count each key equal to A then B ... in the column A and use it for a calcul. I found this code

    1. \def\Somme{0}
    2. \DTLforeach[\DTLiseq{\ColA}{A}]{dbTest}{\ColA=ColA}{\DTLadd{\Somme}{1}{\Somme}}
    3. \Somme


    but is there a similar command like \DTLsumcolumn{dbTest}{ColB}{\Total}\Total ?

    Expected result
    A=2 => 2/5
    B=2 => 2/5
    D=1 => 1/5
  • I would like to add each value in the column B when the key is equal A then B ... and use it for a calcul.

    Expected result
    A 2.25/6.75 =
    B 2/6.75
    D 2.5/6.75

  • I would like to list each string in the column C when the key is equal A then B ... and use it with \cref

    Expected result
    List A => \cref{A,DSa}
    List B => \cref{Ba,RE}

I enclose my file.
Thanks and regards
Attachments
Test-DataTool.tex
(643 Bytes) Downloaded 166 times

Tags:

rais
Posts: 268
Joined: Sun Nov 16, 2014 8:51 pm

Postby rais » Sun Apr 10, 2016 4:19 pm

Hi,
pasbal wrote:
  • How can i reuse \DTLrowcount{dbTest} with an mathematical operation ?
    Expected result : Rows = 5

Isn't that what \DTLrowcount{dbTest} yields in this case?
Or are you after something like \edef\numberofrows{\DTLrowcount{dbTest}}, i.e., to be able to use \numberofrows as a variable?
pasbal wrote:
  • I would like to count each key equal to A then B ... in the column A and use it for a calcul. I found this code

    1. \def\Somme{0}
    2. \DTLforeach[\DTLiseq{\ColA}{A}]{dbTest}{\ColA=ColA}{\DTLadd{\Somme}{1}{\Somme}}
    3. \Somme


    but is there a similar command like \DTLsumcolumn{dbTest}{ColB}{\Total}\Total ?

    Expected result
    A=2 => 2/5
    B=2 => 2/5
    D=1 => 1/5
  • I would like to add each value in the column B when the key is equal A then B ... and use it for a calcul.

    Expected result
    A 2.25/6.75 =
    B 2/6.75
    D 2.5/6.75

Well, instead of using \DTLforeach to create just the number of rows, you could use it to create the sum of relevant points.
The number of effective rows (since you're using a condition) can be obtained by calling \DTLsavelastrowcount.
pasbal wrote:
  • I would like to list each string in the column C when the key is equal A then B ... and use it with \cref

    Expected result
    List A => \cref{A,DSa}
    List B => \cref{Ba,RE}

uhhmm, do you really have a label called `A' in your TeX file (and another one called `DSa')? Or do you have a label called `A,DSa'? The latter does not work with cleveref's \cref command that way: the comma is used here as separator between labels.

  1. \documentclass[12pt,a4paper]{article}
  2. \usepackage[utf8]{inputenc}
  3. \usepackage[francais]{babel}
  4. \usepackage[T1]{fontenc}
  5. \usepackage{datatool}
  6. \usepackage[french]{cleveref}
  7. \usepackage{filecontents}
  8. % datatool loads fp package already
  9. \usepackage{siunitx}
  10.  
  11. \begin{document}
  12.  
  13. %création base
  14. \begin{filecontents}{dbTest.csv}
  15. ColA,ColB,ColC
  16. A,1,A
  17. B,1.5,Ba
  18. A,1.25,DSa
  19. D,2.5,Aza
  20. B,0.5,RE
  21. \end{filecontents}
  22.  
  23. \DTLloaddb{dbTest}{dbTest.csv}
  24.  
  25. \DTLdisplaydb{dbTest}
  26. \edef\numberofrows{\DTLrowcount{dbTest}}%
  27. \DTLsumcolumn{dbTest}{ColB}{\Total}
  28. Total points : \Total
  29.  
  30. \def\SumB{0}
  31. \DTLforeach[\DTLiseq{\ColA}{A}]{dbTest}{\ColA=ColA, \ColB=ColB}{\DTLadd{\SumB}{\SumB}{\ColB}}
  32. \DTLsavelastrowcount{\Somme}%
  33. NB colonne A :\Somme
  34.  
  35. Number of rows: \numberofrows
  36.  
  37. \DTLdiv{\relrows}{\Somme}{\numberofrows}%
  38. \DTLmul{\relrows}{\relrows}{100}%
  39. \DTLround{\relrows}{\relrows}{1}%
  40. `A' rows: \SI{\relrows}{\percent} (\Somme/\numberofrows)
  41.  
  42. \DTLdiv{\SBa}{\SumB}{\Total}%
  43. \DTLmul{\SBap}{\SBa}{100}%
  44. \DTLround{\SBa}{\SBa}{2}%
  45. \DTLround{\SBap}{\SBap}{1}%
  46. \DTLround{\SumB}{\SumB}{2}%
  47. \DTLround{\TotalR}{\Total}{2}%
  48. `A' rows contain \SumB\ points of total \TotalR\ points or \SBa, read \SI{\SBap}{\percent}
  49. \end{document}

KR
Rainer

pasbal
Posts: 12
Joined: Thu Jun 26, 2014 10:00 pm

Postby pasbal » Mon Apr 11, 2016 12:28 pm

Hello,

Thanks for help. I will take some time to read, to assimilate your file, so that i'll write a new file much closer my idea.

pasbal.

pasbal
Posts: 12
Joined: Thu Jun 26, 2014 10:00 pm

Postby pasbal » Fri Apr 22, 2016 7:20 pm

Hello,

Your post was very helpful. Here a test that seems to work and totally compatible with my competences.

Thanks again and regards

  1. \documentclass[12pt,a4paper]{article}
  2. \usepackage[utf8]{inputenc}
  3. \usepackage[francais]{babel}
  4. \usepackage[T1]{fontenc}
  5. \usepackage{datatool}
  6. \usepackage[french]{cleveref}
  7. \usepackage{filecontents}
  8. % datatool loads fp package already
  9. \usepackage{siunitx}
  10. \usepackage{xstring}
  11.  
  12. \begin{document}
  13.  
  14.  
  15. \DTLnewdb{dbScore}
  16.  
  17.  
  18. \begin{equation}
  19. y=a\times x +3
  20. \label{EqA}
  21. \end{equation}
  22. \DTLnewrow{dbScore}
  23. \DTLnewdbentry{dbScore}{Competence}{Analyse}
  24. \DTLnewdbentry{dbScore}{Name}{EqA}
  25. \DTLnewdbentry{dbScore}{Score}{1}
  26. \DTLnewrow{dbScore}
  27. \DTLnewdbentry{dbScore}{Competence}{Redaction}
  28. \DTLnewdbentry{dbScore}{Name}{EqA}
  29. \DTLnewdbentry{dbScore}{Score}{1}
  30.  
  31.  
  32.  
  33. \begin{equation}
  34. y=x \times x +3
  35. \label{EqB}
  36. \end{equation}
  37. \DTLnewrow{dbScore}
  38. \DTLnewdbentry{dbScore}{Competence}{Analyse}
  39. \DTLnewdbentry{dbScore}{Name}{EqB}
  40. \DTLnewdbentry{dbScore}{Score}{2}
  41. \DTLnewrow{dbScore}
  42. \DTLnewdbentry{dbScore}{Competence}{Reason}
  43. \DTLnewdbentry{dbScore}{Name}{EqB}
  44. \DTLnewdbentry{dbScore}{Score}{2}
  45.  
  46. \begin{equation}
  47. y=x +3
  48. \label{EqC}
  49. \end{equation}
  50. \DTLnewrow{dbScore}
  51. \DTLnewdbentry{dbScore}{Competence}{Analyse}
  52. \DTLnewdbentry{dbScore}{Name}{EqC}
  53. \DTLnewdbentry{dbScore}{Score}{1.5}
  54. \DTLnewrow{dbScore}
  55. \DTLnewdbentry{dbScore}{Competence}{Redaction}
  56. \DTLnewdbentry{dbScore}{Name}{EqC}
  57. \DTLnewdbentry{dbScore}{Score}{1.5}
  58.  
  59.  
  60.  
  61. %%
  62. \def\ListAnalyse{}
  63. \DTLforeach[\DTLiseq{\Competence}{Analyse}]{dbScore}{\Competence=Competence,\Name=Name}{\xdef\ListAnalyse{\ListAnalyse,\Name}}
  64. \StrGobbleLeft{\ListAnalyse}{1}[\ListAnalyse]
  65.  
  66. \def\SumAnalyser{0}
  67. \DTLforeach[\DTLiseq{\Competence}{Analyse}]{dbScore}{\Competence=Competence,\Score=Score}{\DTLadd{\SumAnalyser}{\SumAnalyser}{\Score}}
  68. %\SumAnalyser
  69.  
  70. %%
  71. \def\ListRedaction{}
  72. \DTLforeach[\DTLiseq{\Competence}{Redaction}]{dbScore}{\Competence=Competence,\Name=Name}{\xdef\ListRedaction{\ListRedaction,\Name}}
  73. %\DTLsavelastrowcount{\Somme}%
  74. \StrGobbleLeft{\ListRedaction}{1}[\ListRedaction]
  75.  
  76. \def\SumRedaction{0}
  77. \DTLforeach[\DTLiseq{\Competence}{Redaction}]{dbScore}{\Competence=Competence,\Score=Score}{\DTLadd{\SumRedaction}{\SumRedaction}{\Score}}
  78. %\SumRedaction
  79.  
  80. %%
  81. \def\ListReason{}
  82. \DTLforeach[\DTLiseq{\Competence}{Reason}]{dbScore}{\Competence=Competence,\Name=Name}{\xdef\ListReason{\ListReason,\Name}}
  83. %\DTLsavelastrowcount{\Somme}%
  84. \StrGobbleLeft{\ListReason}{1}[\ListReason]
  85.  
  86. \def\SumReason{0}
  87. \DTLforeach[\DTLiseq{\Competence}{Reason}]{dbScore}{\Competence=Competence,\Score=Score}{\DTLadd{\SumReason}{\SumReason}{\Score}}
  88. %\SumReason
  89.  
  90. \DTLaddall{\Somme}{\SumAnalyser,\SumRedaction,\SumReason}
  91.  
  92. \DTLdisplaydb{dbScore}
  93.  
  94.  
  95.  
  96. \begin{tabular}{cccc}
  97. \hline
  98. Competence&Équations&Page&\\
  99. \hline
  100. Analyse&\Cref{\ListAnalyse}&\Cpageref{\ListAnalyse}&\FPeval\Total{round(100*\SumAnalyser/\Somme,0)}\SI{\Total}{\percent}\\
  101. \hline
  102. Reason&\Cref{\ListReason}&\Cpageref{\ListReason}&\FPeval\Total{round(100*\SumReason/\Somme,0)}\SI{\Total}{\percent}\\
  103. \hline
  104. Redaction&\Cref{\ListRedaction}&\Cpageref{\ListRedaction}&\FPeval\Total{round(100*\SumReason/\Somme,0)}\SI{\Total}{\percent}\\
  105. \hline
  106. \end{tabular}
  107.  
  108. \end{document}


Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests