Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. thescgiventoks \
  2. When & @when \
  3.  
  4. documentclass[letterpaper,12pt]{article}
  5. usepackage[utf8]{inputenc}
  6.  
  7. title{Test Tokens in Environment}
  8.  
  9. % Counter for scenarios in this story
  10. newcounter{scenariocnt}
  11. newtoks{scgiventoks} scgiventoks={}
  12. newtoks{scthentoks} scthentoks={}
  13.  
  14. makeatletter
  15. newenvironment{scenario}[1][no title provided]{%
  16. % Token registers for givens and thens
  17. scgiventoks={}
  18. scthentoks={}
  19. defgiven##1{scgiventoks=expandafter{thescgiventoks @formatgiven{##1}}ignorespaces}%
  20. defwhen##1{gdef@when{##1}ignorespaces}%
  21. def@when{@latex@error{No noexpandwhen provided.}@ehc}%
  22. defthen##1{scthentoks=expandafter{thescthentoks @formatthen{##1}}ignorespaces}%
  23.  
  24. stepcounter{scenariocnt}%
  25. subsection{Scenario thescenariocnt : #1}%
  26. begin{tabular}{rp{5.5in}}%
  27. ignorespaces
  28. }{%
  29. gdef@given{Given}
  30. gdef@then{Then}
  31. letargrowsep=empty
  32. % if the next two lines are commented out, then the Then tokens get rendered
  33. % it seems that the presence of a \ empties the token lists.
  34. thescgiventoks \
  35. When & @when \
  36. letargrowsep=empty
  37. thescthentoks \
  38. end{tabular}%
  39. }%
  40.  
  41. newcommand{@formatgiven}[1]{%
  42. argrowsep % first time empty, second time a \
  43. @given & #1
  44. gdefargrowsep{\}% next time
  45. gdef@given{And}% next time
  46. }
  47.  
  48. newcommand{@formatthen}[1]{%
  49. argrowsep % first time empty, second time a \
  50. @then & #1
  51. gdefargrowsep{\}% next time
  52. gdef@then{And}% next time
  53. }
  54. makeatother
  55.  
  56. begin{document}
  57.  
  58. section{Minimum Working Example}
  59. I'm trying to create a new environment to allow a scenario to be defined.
  60.  
  61. The environment can contain exactly one verb+when+ macro, and any number of verb+given+ or
  62. verb+then+ macros. Since the macros can be given in any order, the environment uses token lists
  63. to collect the emph{given} and emph{then} invocations, and the closing block of the environment
  64. outputs them in the desired format.
  65.  
  66. The output I want from the environment should look like this:
  67. vspace{1em}
  68.  
  69. begin{tabular}{rp{5.5in}}
  70. Given & the account balance is $100.00 \
  71. And & the card is valid \
  72. And & the machine contains enough money \
  73. When & the Account Holder requests $20.00 \
  74. Then & the ATM should dispense $20.00 \
  75. And & the account balance should be $80.00 \
  76. And & the card should be returned \
  77. end{tabular}
  78.  
  79. begin{scenario}[Account has sufficient funds]
  80. when{the Account Holder requests $20.00}
  81.  
  82. given{the account balance is $100.00}
  83. then{the ATM should dispense $20.00}
  84. given{the card is valid}
  85. then{the account balance should be $80.00}
  86. given{the machine contains enough money}
  87. then{the card should be returned}
  88. end{scenario}
  89. end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement