Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- thescgiventoks \
- When & @when \
- documentclass[letterpaper,12pt]{article}
- usepackage[utf8]{inputenc}
- title{Test Tokens in Environment}
- % Counter for scenarios in this story
- newcounter{scenariocnt}
- newtoks{scgiventoks} scgiventoks={}
- newtoks{scthentoks} scthentoks={}
- makeatletter
- newenvironment{scenario}[1][no title provided]{%
- % Token registers for givens and thens
- scgiventoks={}
- scthentoks={}
- defgiven##1{scgiventoks=expandafter{thescgiventoks @formatgiven{##1}}ignorespaces}%
- defwhen##1{gdef@when{##1}ignorespaces}%
- def@when{@latex@error{No noexpandwhen provided.}@ehc}%
- defthen##1{scthentoks=expandafter{thescthentoks @formatthen{##1}}ignorespaces}%
- stepcounter{scenariocnt}%
- subsection{Scenario thescenariocnt : #1}%
- begin{tabular}{rp{5.5in}}%
- ignorespaces
- }{%
- gdef@given{Given}
- gdef@then{Then}
- letargrowsep=empty
- % if the next two lines are commented out, then the Then tokens get rendered
- % it seems that the presence of a \ empties the token lists.
- thescgiventoks \
- When & @when \
- letargrowsep=empty
- thescthentoks \
- end{tabular}%
- }%
- newcommand{@formatgiven}[1]{%
- argrowsep % first time empty, second time a \
- @given & #1
- gdefargrowsep{\}% next time
- gdef@given{And}% next time
- }
- newcommand{@formatthen}[1]{%
- argrowsep % first time empty, second time a \
- @then & #1
- gdefargrowsep{\}% next time
- gdef@then{And}% next time
- }
- makeatother
- begin{document}
- section{Minimum Working Example}
- I'm trying to create a new environment to allow a scenario to be defined.
- The environment can contain exactly one verb+when+ macro, and any number of verb+given+ or
- verb+then+ macros. Since the macros can be given in any order, the environment uses token lists
- to collect the emph{given} and emph{then} invocations, and the closing block of the environment
- outputs them in the desired format.
- The output I want from the environment should look like this:
- vspace{1em}
- begin{tabular}{rp{5.5in}}
- Given & the account balance is $100.00 \
- And & the card is valid \
- And & the machine contains enough money \
- When & the Account Holder requests $20.00 \
- Then & the ATM should dispense $20.00 \
- And & the account balance should be $80.00 \
- And & the card should be returned \
- end{tabular}
- begin{scenario}[Account has sufficient funds]
- when{the Account Holder requests $20.00}
- given{the account balance is $100.00}
- then{the ATM should dispense $20.00}
- given{the card is valid}
- then{the account balance should be $80.00}
- given{the machine contains enough money}
- then{the card should be returned}
- end{scenario}
- end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement