collection.sty 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. %% start of file `collection.sty'.
  2. %% Copyright 2013-2013 Xavier Danaux (xdanaux@gmail.com).
  3. %
  4. % This work may be distributed and/or modified under the
  5. % conditions of the LaTeX Project Public License version 1.3c,
  6. % available at http://www.latex-project.org/lppl/.
  7. %-------------------------------------------------------------------------------
  8. % identification
  9. %-------------------------------------------------------------------------------
  10. \NeedsTeXFormat{LaTeX2e}
  11. \ProvidesPackage{collection}[2013/03/28 v1.0.0 collections]
  12. %-------------------------------------------------------------------------------
  13. % requirements
  14. %-------------------------------------------------------------------------------
  15. \RequirePackage{ifthen}
  16. %-------------------------------------------------------------------------------
  17. % code
  18. %-------------------------------------------------------------------------------
  19. % creates a new collection
  20. % usage: \collectionnew{<collection name>}
  21. \newcommand*{\collectionnew}[1]{%
  22. \newcounter{collection@#1@count}}
  23. % adds an item to a collection
  24. % usage: \collectionadd[<optional key>]{<collection name>}{<item to add>}
  25. \newcommand*{\collectionadd}[3][]{%
  26. \expandafter\def\csname collection@#2@item\roman{collection@#2@count}\endcsname{#3}%
  27. \if\relax\noexpand#1\relax% if #1 is empty
  28. \else\expandafter\def\csname collection@#2@key\roman{collection@#2@count}\endcsname{#1}\fi%
  29. \stepcounter{collection@#2@count}}
  30. % returns the number of items in a collection
  31. % usage: \collectioncount{<collection name>}
  32. \newcommand*{\collectioncount}[1]{%
  33. \value{collection@#1@count}}
  34. % gets an item from a collection
  35. % usage: \collectiongetitem{<collection name>}{<element id>}
  36. % where <element id> is an integer between 0 and (collectioncount-1)
  37. \newcommand*{\collectiongetitem}[2]{%
  38. \csname collection@#1@item\romannumeral #2\endcsname}
  39. % gets a key from a collection
  40. % usage: \collectiongetkey{<collection name>}{<element id>}
  41. % where <element id> is an integer between 0 and (collectioncount-1)
  42. \newcommand*{\collectiongetkey}[2]{%
  43. \csname collection@#1@key\romannumeral #2\endcsname}
  44. % loops through a collection and perform the given operation on every element
  45. % usage: \collectionloop{<collection name>}{<operation sequence>}
  46. % where <operation sequence> is the code sequence to be evaluated for each collection item,
  47. % code which can refer to \collectionloopid, \collectionloopkey, \collectionloopitem and
  48. % \collectionloopbreak
  49. \newcounter{collection@iterator}
  50. \newcommand*{\collectionloopbreak}{\let\iterate\relax}
  51. \newcommand*{\collectionloop}[2]{%
  52. \setcounter{collection@iterator}{0}%
  53. \loop\ifnum\value{collection@iterator}<\value{collection@#1@count}%
  54. \def\collectionloopid{\arabic{collection@iterator}}%
  55. \def\collectionloopitem{\collectiongetitem{#1}{\collectionloopid}}%
  56. \def\collectionloopkey{\collectiongetkey{#1}{\collectionloopid}}%
  57. #2%
  58. \stepcounter{collection@iterator}%
  59. \repeat}
  60. % loops through a collection and finds the (first) element matching the given key
  61. % usage: \collectionfindbykey{<collection name>}{key>}
  62. \newcommand*{\collectionfindbykey}[2]{%
  63. \collectionloop{#1}{%
  64. \ifthenelse{\equal{\collectionloopkey}{#2}}{\collectionloopitem\collectionloopbreak}{}}}
  65. \endinput
  66. %% end of file `collection.cls'.