Advertisement
Guest User

lurc.tex

a guest
Feb 5th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 1.92 KB | None | 0 0
  1.  
  2. % lurc.tex
  3. % LaTeX Utility/Template for Generic Reusable Code (LURC)
  4. % Usage:
  5. %
  6. %    In main .tex file:
  7. %
  8. %    \InsertModule{mypath/foo}{instance-A}[key1=somevalue1,key2=somevalue2]
  9. %
  10. %    \InsertModule{mypath/foo}{instance-B}
  11. %
  12. %    \InsertModule{mypath/foo}{instance-C}[some other key=some value]
  13. %
  14. %
  15. %    In mypath/foo.tex (included .tex file):
  16. %
  17. %    \Instance
  18. %
  19. %    \OneTimeOnly{
  20. %       only once code
  21. %       \GetKeyValue{key1}
  22. %       \GetKeyValue{key2}
  23. %    }
  24. %
  25. %    every time code
  26. %
  27. %    \OnlyFor{instance-A}{
  28. %       'instance-A' code.
  29. %    }
  30. %
  31. %    \OnlyFor{instance-B,instance-C}{
  32. %       \GetKeyValue{some other key}
  33. %       'instance-B' or 'instance-C' code
  34. %    }
  35.  
  36. \makeatletter
  37. \usepackage{pgfkeys}
  38. \pgfkeys{/generic-reusable/.unknown/.code={%
  39.         \expandafter\edef\csname gr-arg-\pgfkeyscurrentkey\endcsname{\unexpanded\expandafter{\pgfkeyscurrentvalue}}%
  40. }}
  41.  
  42. \def\gr@markincluded#1{\global\expandafter\let\csname gr-file-#1-alreadyincluded\endcsname\empty}
  43. \def\gr@ifincluded#1{\@ifundefined{gr-file-#1-alreadyincluded}}
  44.  
  45. \def\gr@OneTimeOnly{%
  46.     \gr@ifincluded{\gr@filename}%
  47.     {\@firstofone}%
  48.     {\@gobble}%
  49. }
  50.  
  51.  
  52. \def\gr@OnlyFor#1{%
  53.     \gr@OnlyFor@#1,\gr@nil
  54. }
  55.  
  56. \def\gr@OnlyFor@#1,{%
  57.     \def\gr@temp{#1}%
  58.     \def\next{\@ifnextchar\gr@nil{\@gobbletwo}{\gr@OnlyFor@}}%
  59.     \ifx\gr@temp\gr@requestedinstance
  60.     \let\next\gr@OnlyFor@dobody
  61.     \fi
  62.     \next
  63. }
  64.  
  65. \def\gr@GetKeyValue#1{\csname gr-arg-/generic-reusable/#1\endcsname}
  66.  
  67. \def\gr@OnlyFor@dobody#1\gr@nil#2{#2}
  68.  
  69. \def\InsertModule#1#2{%
  70.     \@ifnextchar[{\gr@InsertModule@{#1}{#2}}{\gr@InsertModule@{#1}{#2}[]}%
  71. }
  72. \def\gr@InsertModule@#1#2[#3]{%
  73.     \begingroup
  74.     \let\OneTimeOnly\gr@OneTimeOnly
  75.     \let\OnlyFor\gr@OnlyFor
  76.     \let\GetKeyValue\gr@GetKeyValue
  77.     \pgfqkeys{/generic-reusable}{#3}
  78.     \def\gr@requestedinstance{#2}%
  79.     \let\Instance\gr@requestedinstance
  80.     \def\gr@filename{#1}%
  81.     \input #1 \relax
  82.     \endgroup
  83.     \gr@markincluded{#1}
  84. }
  85. \makeatother
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement