summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ribbonproofs
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/ribbonproofs
Initial commit
Diffstat (limited to 'macros/latex/contrib/ribbonproofs')
-rw-r--r--macros/latex/contrib/ribbonproofs/README3
-rw-r--r--macros/latex/contrib/ribbonproofs/ribbonproofs.sty1696
-rw-r--r--macros/latex/contrib/ribbonproofs/ribbonproofsmanual.pdfbin0 -> 335721 bytes
-rw-r--r--macros/latex/contrib/ribbonproofs/ribbonproofsmanual.tex776
4 files changed, 2475 insertions, 0 deletions
diff --git a/macros/latex/contrib/ribbonproofs/README b/macros/latex/contrib/ribbonproofs/README
new file mode 100644
index 0000000000..d9de194a3d
--- /dev/null
+++ b/macros/latex/contrib/ribbonproofs/README
@@ -0,0 +1,3 @@
+The ribbonproofs package provides a way to draw "ribbon proofs" in LaTeX.
+
+A ribbon proof is a diagrammatic representation of a mathematical proof that a computer program meets its specification. These diagrams are more human-readable, more scalable, and more easily modified than the corresponding textual proofs. Ribbon proofs were introduced by John Wickerson, Mike Dodds and Matthew Parkinson in a paper entitled "Ribbon Proofs for Separation Logic" and presented at the European Symposium on Programming (ESOP) 2013. \ No newline at end of file
diff --git a/macros/latex/contrib/ribbonproofs/ribbonproofs.sty b/macros/latex/contrib/ribbonproofs/ribbonproofs.sty
new file mode 100644
index 0000000000..e304a97e8c
--- /dev/null
+++ b/macros/latex/contrib/ribbonproofs/ribbonproofs.sty
@@ -0,0 +1,1696 @@
+%% ribbonproofs.sty
+%% Copyright 2013 John Wickerson
+%
+% This work may be distributed and/or modified under the
+% conditions of the LaTeX Project Public License, either version 1.3
+% of this license or (at your option) any later version.
+% The latest version of this license is in
+% http://www.latex-project.org/lppl.txt
+% and version 1.3 or later is part of all distributions of LaTeX
+% version 2005/12/01 or later.
+%
+% This work has the LPPL maintenance status `maintained'.
+%
+% The Current Maintainer of this work is John Wickerson.
+%
+% This work consists of the files ribbonproofs.sty,
+% ribbonproofsmanual.tex, and ribbonproofsmanual.pdf.
+
+% Title: Ribbon Proofs
+% Description: A package for drawing ribbon proofs
+% Author: John Wickerson
+% Creation date: 20-Apr-2013
+% Last modified: 8-Jul-2013
+
+
+% ===================================================================
+% TODO LIST
+%
+% * more error detection, e.g. start/fit overlap,
+% finish/fit overlap, repetition within start,
+% fit or finish ribbons.
+%
+%
+% * Consider the case where we have a var label and a
+% rotated main label. Should we write
+% label=p&, rotated label=q
+% or
+% label=p&\rotate{q}
+% ?
+
+
+% ===================================================================
+% LOAD REQUIRED PACKAGES
+%
+% For colours
+\RequirePackage[svgnames,usenames]{xcolor}
+% For drawing stuff
+\RequirePackage{tikz}
+% For using the `let` construct
+\usetikzlibrary{calc}
+% For drawing shadows
+\usetikzlibrary{fadings}
+% For drawing zigzag lines
+\usetikzlibrary{decorations.pathmorphing}
+% For splitting a string at a given character
+\RequirePackage{xstring}
+% For expansion control
+\RequirePackage{etextools}
+
+% ===================================================================
+% GENERAL-PURPOSE COMMANDS
+
+% Extension of the ExpandNextTwo command provided by etextools.
+% ExpandNextTwo\foo{arg1}{arg2} fully expands arg1 and arg2
+% and then passes them to the \foo command.
+\newcommand*\ExpandNextThree[4]{%
+ \ExpandNext{%
+ \ExpandNext{%
+ \ExpandNext{#1}{#2}%
+ }{#3}%
+ }{#4}%
+}
+
+% Another extension of the ExpandNextTwo command provided by
+% etextools. ExpandNextThree\foo{arg1}{arg2}{arg3} fully expands
+% arg1, arg2 and arg3 and then passes them to the \foo command.
+\newcommand*\ExpandNextFour[5]{%
+ \ExpandNext{%
+ \ExpandNext{%
+ \ExpandNext{%
+ \ExpandNext{#1}{#2}%
+ }{#3}%
+ }{#4}%
+ }{#5}%
+}
+
+% Append to the beginning of a comma-separated list.
+% If \list={} then \cons{\list}{a} defines \list={a}.
+% If \list={p,e} then \cons{\list{a} defines \list={a,p,e}.
+% The optional argument can be used to change the comma into
+% something else. The element being appended is not
+% expanded, and neither is the list.
+\newcommand*\cons[3][,]{%
+ \ifx#2\@empty
+ \xdef#2{\expandonce{#3}}
+ \else
+ \xdef#2{\expandonce{#3}#1\expandonce{#2}}
+ \fi
+}
+
+% Append to the end of a comma-separated list. (The name
+% is "cons" backwards.) If \list={} then \snoc{\list}{a} defines
+% \list={a}. If \list={p,e} then \snoc{\list{a} defines \list={p,e,a}.
+% The optional argument can be used to change the comma into
+% something else. The element being appended is not
+% expanded, and neither is the list.
+\newcommand*\snoc[3][,]{%
+ \ifx#2\@empty
+ \xdef#2{\expandonce{#3}}
+ \else
+ \xdef#2{\expandonce{#2}#1\expandonce{#3}}
+ \fi
+}
+
+% Test whether a comma-separated list contains a given element. If
+% element el is in list L, then
+% \@ifListContains{el}{L}{<true code>}{<false code>} will execute
+% <true code>, and if it is not, it will execute <false code>.
+\newcommand*\@ifListContains[4]{%
+ \gdef\myflag{0}
+ \edef\mylist{\expandonce{#2}}
+ \foreach\j in \mylist {
+ \IfStrEq{#1}{\j}{
+ \gdef\myflag{1}
+ #3
+ \breakforeach
+ }{}
+ }
+ \ifnum\myflag=0\relax
+ #4
+ \fi
+}
+
+% Test whether a comma-separated list of key-value pairs contains
+% a given key. If key k is in the "domain" of list L, then
+% \@ifPairListContainsKey{k}{L}{<true code>}{<false code>} will
+% execute <true code>, and if it is not, it will execute <false code>.
+\newcommand*\@ifPairListContainsKey[4]{%
+ \gdef\myflag{0}
+ \edef\mylist{\expandonce{#2}}
+ \foreach\j/\values in \mylist {
+ \IfStrEq{#1}{\j}{
+ \gdef\myflag{1}
+ #3
+ \breakforeach
+ }{}
+ }
+ \ifnum\myflag=0\relax
+ #4
+ \fi
+}
+
+% \undefine{foo} makes the command \foo undefined. The effect is
+% that \ifcsname foo\endcsname will henceforth return false.
+\newcommand*\undefine[1]{%
+ \expandafter\global%
+ \expandafter\let\csname #1\endcsname=\@undefined%
+}
+
+% A foreach command that iterates over two lists (of the same
+% length) simultaneously, plucking an element from both lists
+% on each iteration. For instance, \foreachtwo\foo{1,2}{a,b}
+% executes \foo{1}{a} and then \foo{2}{b}.
+\def\@@foreachtwo#1#2,#3;#4,#5;{%
+ #1{#2}{#4}%
+ \@foreachtwo{#1}{#3}{#5}%
+}
+\newcommand*\@foreachtwo[3]{%
+ \ifboolexpr{test{\IfSubStr{#2}{,}} and test{\IfSubStr{#3}{,}}}{%
+ \@@foreachtwo{#1}#2;#3;%
+ }{%
+ \ifboolexpr{test{\notblank{#2}} and test{\notblank{#3}}}{%
+ #1{#2}{#3}%
+ }{}%
+ }%
+}
+\newcommand*\foreachtwo[3]{%
+ \def\lenI{0}
+ \def\lenII{0}
+ \foreach\x in {#2} {
+ \pgfmathtruncatemacro\lenI{\lenI+1}
+ \global\let\lenI=\lenI
+ }
+ \foreach\x in {#3} {
+ \pgfmathtruncatemacro\lenII{\lenII+1}
+ \global\let\lenII=\lenII
+ }
+ \ifnum\lenI=\lenII \else
+ \errmessage{The lists {#2} (length \lenI) and
+ {#3} (length \lenII) are not the same length}
+ \fi
+ \@foreachtwo{#1}{#2}{#3}
+}
+
+% ===================================================================
+% CONSTANTS
+
+% background colour of "justification" steps
+\newcommand\jusColor{black!50}
+% background colour of "command" steps
+\newcommand\comColor{black}
+% background colour of ribbons
+\newcommand\ribColor{black!10}
+% background colour of vars-as-res ribbons
+\newcommand\varribColor{black!20}
+% foreground colour of ribbons
+\newcommand\ribTextColor{black}
+% foreground colour of boxes
+\newcommand\boxTextColor{black}
+% foreground colour of "guide" text
+\newcommand\guideTextColor{red!40}
+% default step height
+\newcommand\defaultStepHeight{5}
+% default row height (includes the step)
+\newcommand\defaultRowHeight{11}
+% distance from top of ribbon to its label
+\newcommand\ribTextVOffset{4}
+% distance from top of box to its label
+\newcommand\boxTextVOffset{3}
+% horizontal adjustment of box label
+\newcommand\boxTextHOffset{1}
+% distance from bottom of row to guide text on ribbons
+\newcommand\guideTextVOffset{2}
+% radius of rounded corners on block steps
+\newcommand\roundingRadius{2}
+% radius of rounded corners on boxes
+\newcommand\boxRoundingRadius{1}
+% thickness of vertical lines delimiting block steps
+\newcommand\blockLineWidth{0.5}
+% thickness of lines delimiting existential boxes
+\newcommand\boxLineWidth{0.8pt}
+% height of the shadows that are drawn when a step
+% passes over a catalyst ribbon
+\newcommand\shadowHeight{2}
+% size of the jagged edges on "else" steps
+\newcommand\zigzagHeight{1}
+\newcommand\zigzagLength{3}
+% color of [the darkest point of] a shadow
+\newcommand\shadowColor{black!40}
+% adjust the shape of the ribbon twists (value between 0 and 1)
+\newcommand\twistiness{0.7}
+
+% ===================================================================
+% ERROR MESSAGES
+
+\newcommand\printActiveRibbons{%
+ The currently active ribbons are {\@activeRibbons}%
+}
+
+\newcommand\printActiveBoxes{%
+ The currently active existential boxes are {\@activeBoxes}%
+}
+
+\newcommand\errorI[1]{\PackageError{RibbonProofs}{%
+ Ribbon #1\space is specified as a start ribbon,
+ but is already active. \printActiveRibbons}}
+
+\newcommand\errorII[1]{\PackageError{RibbonProofs}{%
+ Existential box #1\space is specified as a start
+ box, but is already active. \printActiveBoxes}}
+
+\newcommand\errorIII[1]{\PackageError{RibbonProofs}{%
+ Ribbon #1\space is specified as a fit ribbon,
+ but is not currently active. \printActiveRibbons}}
+
+\newcommand\errorIV[1]{\PackageError{RibbonProofs}{%
+ Ribbon #1\space is specified as a finish ribbon,
+ but is not currently active. \printActiveRibbons}}
+
+\newcommand\errorV[1]{\PackageError{RibbonProofs}{%
+ Existential box #1\space is specified as a fit
+ box, but is not currently active. \printActiveBoxes}}
+
+\newcommand\errorVI[1]{\PackageError{RibbonProofs}{%
+ Existential box #1\space is specified as an finish
+ box, but is not currently active. \printActiveBoxes}}
+
+\newcommand\errorVII[1]{\PackageError{RibbonProofs}{%
+ Ribbon #1\space is not active, so cannot be moved.
+ \printActiveRibbons}}
+
+\newcommand\errorVIII[1]{\PackageError{RibbonProofs}{%
+ Existential box #1\space is not active, so cannot be moved.
+ \printActiveBoxes}}
+
+\newcommand\errorIX[1]{\PackageError{RibbonProofs}{%
+ Existential box #1\space is not active, so cannot be
+ extended. \printActiveBoxes}}
+
+% ===================================================================
+% STORAGE
+% Data is stored in pgf keys, in macros, and in counters.
+% The "execute style" and "execute code" keys are due to
+% Andrew Stacey: http://tex.stackexchange.com/q/85637/86.
+
+\pgfqkeys{/ribbons/ribbon}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ left/.estore in= {\ribbons@left},
+ right/.estore in= {\ribbons@right},
+ var width/.estore in= {\ribbons@varwidth},
+ label/.store in= {\ribbons@label},
+ rotated label/.store in= {\ribbons@rotatedlabel},
+ execute style/.style = {#1},
+ execute macro/.style = {execute style/.expand once=#1},
+}
+\pgfqkeys{/ribbons/box}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ left/.estore in= {\ribbons@left},
+ right/.estore in= {\ribbons@right},
+ label/.store in= {\ribbons@label},
+ color/.store in= {\ribbons@boxcolor},
+ vertical offset/.store in= {\ribbons@boxvoffset},
+ horizontal offset/.store in= {\ribbons@boxhoffset},
+ execute style/.style = {#1},
+ execute macro/.style = {execute style/.expand once=#1},
+}
+\pgfqkeys{/ribbons/step}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ pale/.code= {\def\ribbons@color{\jusColor}},
+ height/.store in= {\ribbons@stepheight},
+ extra left/.store in= {\ribbons@extraleft},
+ extra right/.store in= {\ribbons@extraright},
+ finish ribbons/.store in= {\ribbons@finishribbons},
+ start ribbons/.store in= {\ribbons@startribbons},
+ start boxes/.store in= {\ribbons@startboxes},
+ finish boxes/.store in= {\ribbons@finishboxes},
+ text/.store in= {\ribbons@text},
+}
+\pgfqkeys{/ribbons/startblock}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ height/.store in= {\ribbons@stepheight},
+ extra left/.store in= {\ribbons@extraleft},
+ extra right/.store in= {\ribbons@extraright},
+ finish ribbons/.store in= {\ribbons@finishribbons},
+ start ribbons/.store in= {\ribbons@startribbons},
+ finish boxes/.store in= {\ribbons@finishboxes},
+ start boxes/.store in= {\ribbons@startboxes},
+ fit ribbons/.store in= {\ribbons@fitribbons},
+ fit boxes/.store in= {\ribbons@fitboxes},
+ text/.store in= {\ribbons@text},
+}
+\pgfqkeys{/ribbons/continueblock}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ height/.store in= {\ribbons@stepheight},
+ start ribbons/.store in={\ribbons@startribbons},
+ start boxes/.store in= {\ribbons@startboxes},
+ text/.store in= {\ribbons@text},
+ jagged/.code= {\def\ribbons@jagged{1}},
+ repeat labels/.code= {\def\ribbons@repeatlabels{1}},
+}
+\pgfqkeys{/ribbons/finishblock}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ height/.store in= {\ribbons@stepheight},
+ finish ribbons/.store in={\ribbons@finishribbons},
+ start ribbons/.store in= {\ribbons@startribbons},
+ finish boxes/.store in= {\ribbons@finishboxes},
+ start boxes/.store in= {\ribbons@startboxes},
+ extra left/.store in= {\ribbons@extraleft},
+ extra right/.store in= {\ribbons@extraright},
+ text/.store in= {\ribbons@text},
+}
+\pgfqkeys{/ribbons/ribbonproof}{
+ .unknown/.code={\errmessage{Unknown key: \pgfkeyscurrentkeyRAW}},
+ scale/.store in= {\ribbons@scale},
+ extra height/.store in= {\ribbons@extraheight},
+ start boxes/.store in= {\ribbons@startboxes},
+ start ribbons/.store in= {\ribbons@startribbons},
+ draw grid/.code= {%
+ \tikzset{every picture/.append style={%
+ execute at end picture={\@drawNotches},%
+ show grid=all}}
+ \@showidstrue}
+}
+
+% For tracking the distance from the top of the diagram.
+\newcounter{VCursor}
+
+% Height of the current row.
+\newcounter{RowHeight}
+
+% ===================================================================
+% SHOW GRID
+%
+% This code is based on Martin Scharrer's showgrid TikZ
+% library, which can be downloaded from his website:
+% http://latex.scharrer-online.de/general/wiki/showgrid
+% I have changed the colour of the grid, provided minor
+% and major grid lines, and added a numeric scale to
+% each of the four edges.
+
+\pgfdeclarelayer{background}
+\pgfdeclarelayer{foreground}
+\pgfsetlayers{background,main,foreground}
+
+\newif\if@showgrid@grid
+\newif\if@showgrid@left
+\newif\if@showgrid@right
+\newif\if@showgrid@below
+\newif\if@showgrid@above
+\newif\if@showids
+\tikzset{%
+ every show grid/.style={},
+ show grid/.style={%
+ execute at end picture={\@showgrid{grid=true,#1}}},%
+ show grid/.default={true},
+ show grid/.cd,
+ labels/.style={font={\sffamily\small},help lines,text=red!40},
+ xlabels/.style={},
+ ylabels/.style={},
+ keep bb/.code={%
+ \useasboundingbox (current bounding box.south west)
+ rectangle (current bounding box.north east);},
+ true/.style={left,below},
+ false/.style={left=false,right=false,above=false,
+ below=false,grid=false},
+ none/.style={left=false,right=false,above=false,below=false},
+ all/.style={left=true,right=true,above=true,below=true},
+ grid/.is if=@showgrid@grid,
+ left/.is if=@showgrid@left,
+ right/.is if=@showgrid@right,
+ below/.is if=@showgrid@below,
+ above/.is if=@showgrid@above,
+ false,
+}
+
+\def\@showgrid#1{%
+ \begin{scope}[every show grid,show grid/.cd,#1]
+ \if@showgrid@grid
+ \begin{pgfonlayer}{background}
+ \draw [help lines, draw=red!10, step=1mm]
+ let \p1 = (current bounding box.south west),
+ \p2 = (current bounding box.north east)
+ in (\x1-2mm,\y1-2mm) grid (\x2+2mm,\y2+2mm);
+ \draw [help lines, draw=red!20, step=10mm]
+ (current bounding box.south west) grid
+ (current bounding box.north east);
+ \pgfpointxy{1}{1}%
+ \edef\xs{\the\pgf@x}%
+ \edef\ys{\the\pgf@y}%
+ \pgfpointanchor{current bounding box}{south west}
+ \edef\xa{\the\pgf@x}%
+ \edef\ya{\the\pgf@y}%
+ \pgfpointanchor{current bounding box}{north east}
+ \edef\xb{\the\pgf@x}%
+ \edef\yb{\the\pgf@y}%
+ \pgfmathtruncatemacro\xbeg{ceil(\xa/\xs)}
+ \pgfmathtruncatemacro\xend{floor(\xb/\xs)}
+ \if@showgrid@below
+ \foreach \X in {\xbeg,...,\xend} {
+ \pgfmathparse{int(mod(\X,10))}
+ \ifnum\pgfmathresult=0
+ \node [below,show grid/labels,show grid/xlabels]
+ at (\X,\ya) {\X};
+ \else\fi
+ }
+ \fi
+ \if@showgrid@above
+ \foreach \X in {\xbeg,...,\xend} {
+ \pgfmathparse{int(mod(\X,10))}
+ \ifnum\pgfmathresult=0
+ \node [above,show grid/labels,show grid/xlabels]
+ at (\X,\yb) {\X};
+ \else\fi
+ }
+ \fi
+ \pgfmathtruncatemacro\ybeg{ceil(\ya/\ys)}
+ \pgfmathtruncatemacro\yend{floor(\yb/\ys)}
+ \if@showgrid@left
+ \foreach \Y in {\ybeg,...,\yend} {
+ \pgfmathparse{int(mod(\Y,10))}
+ \ifnum\pgfmathresult=0
+ \node [left,show grid/labels,show grid/ylabels]
+ at (\xa,\Y) {\Y};
+ \else\fi
+ }
+ \fi
+ \if@showgrid@right
+ \foreach \Y in {\ybeg,...,\yend} {
+ \pgfmathparse{int(mod(\Y,10))}
+ \ifnum\pgfmathresult=0
+ \node [right,show grid/labels,show grid/ylabels]
+ at (\xb,\Y) {\Y};
+ \else\fi
+ }
+ \fi
+ \end{pgfonlayer}
+ \fi
+ \end{scope}
+}
+% End TikZ showgrid code
+
+% ===================================================================
+% MINOR COMMANDS
+
+% The expression
+% \@defineRibbon{foo}{34}{57}{10}{x=5}
+% expands to the following definitions
+% \xdef\RIBfooLEFT{34}
+% \xdef\RIBfooRIGHT{57}
+% \xdef\RIBfooVARWIDTH{10}
+% \gdef\RIBfoo@label{x=5}
+% and saves "foo" in the list of defined ribbons.
+\newcommand*\@defineRibbon[5]{%
+ \snoc{\@allDefinedRibbons}{#1}
+ \expandafter\xdef\csname RIB#1LEFT\endcsname{#2}
+ \expandafter\xdef\csname RIB#1RIGHT\endcsname{#3}
+ \expandafter\xdef\csname RIB#1VARWIDTH\endcsname{#4}
+ \expandafter\xdef\csname RIB#1@label\endcsname{\expandonce{#5}}
+}
+
+% The expression
+% \@defineBox{foo}{34}{57}{green}
+% expands to the following definitions
+% \xdef\BOXfooLEFT{34}
+% \xdef\BOXfooRIGHT{57}
+% \xdef\BOXfoo@color{green}
+% and saves "foo" in the list of defined boxes.
+\newcommand*\@defineBox[5]{%
+ \snoc{\@allDefinedBoxes}{#1}
+ \expandafter\xdef\csname BOX#1LEFT\endcsname{#2}
+ \expandafter\xdef\csname BOX#1RIGHT\endcsname{#3}
+ \expandafter\xdef\csname BOX#1@color\endcsname{#4}
+ \expandafter\xdef\csname BOX#1@label\endcsname{#5}
+}
+
+% In a state where
+% \RIBfooLEFT = 88
+% \RIBfooRIGHT = 99
+% \RIBfooVARWIDTH = 10
+% the expression
+% \@rememberPrevRibbon{foo}
+% expands to the following definitions
+% \xdef\RIBfoo@prevleft{88}
+% \xdef\RIBfoo@prevright{99}
+% \xdef\RIBfoo@prevvarwidth{10}
+\newcommand*\@rememberPrevRibbon[1]{%
+ \expandafter\xdef\csname RIB#1@prevleft\endcsname{%
+ \csname RIB#1LEFT\endcsname}
+ \expandafter\xdef\csname RIB#1@prevright\endcsname{%
+ \csname RIB#1RIGHT\endcsname}
+ \expandafter\xdef\csname RIB#1@prevvarwidth\endcsname{%
+ \csname RIB#1VARWIDTH\endcsname}
+}
+
+% In a state where
+% \BOXfooLEFT = 88
+% \BOXfooRIGHT = 99
+% the expression
+% \@rememberPrevBox{foo}
+% expands to the following definitions
+% \xdef\BOXfoo@prevleft{88}
+% \xdef\BOXfoo@prevright{99}
+\newcommand*\@rememberPrevBox[1]{%
+ \expandafter\xdef\csname BOX#1@prevleft\endcsname{%
+ \csname BOX#1LEFT\endcsname}
+ \expandafter\xdef\csname BOX#1@prevright\endcsname{%
+ \csname BOX#1RIGHT\endcsname}
+}
+
+% Various projection functions
+\newcommand*\@getRibbonLeft[1]{%
+ \csname RIB#1LEFT\endcsname}
+\newcommand*\@getRibbonRight[1]{%
+ \csname RIB#1RIGHT\endcsname}
+\newcommand*\@getRibbonVarWidth[1]{%
+ \csname RIB#1VARWIDTH\endcsname}
+\newcommand*\@getRibbonLabel[1]{%
+ \expandafter\expandonce\csname RIB#1@label\endcsname}
+\newcommand*\@getRibbonPrevLeft[1]{%
+ \csname RIB#1@prevleft\endcsname}
+\newcommand*\@getRibbonPrevRight[1]{%
+ \csname RIB#1@prevright\endcsname}
+\newcommand*\@getRibbonPrevVarWidth[1]{%
+ \csname RIB#1@prevvarwidth\endcsname}
+\newcommand*\@getBoxLeft[1]{%
+ \csname BOX#1LEFT\endcsname}
+\newcommand*\@getBoxRight[1]{%
+ \csname BOX#1RIGHT\endcsname}
+\newcommand*\@getBoxColor[1]{%
+ \csname BOX#1@color\endcsname}
+\newcommand*\@getBoxLabel[1]{%
+ \csname BOX#1@label\endcsname}
+\newcommand*\@getBoxPrevLeft[1]{%
+ \csname BOX#1@prevleft\endcsname}
+\newcommand*\@getBoxPrevRight[1]{%
+ \csname BOX#1@prevright\endcsname}
+
+% Add the given identifier to the list of active ribbons.
+\newcommand*\@addToActiveRibbons[1]{%
+ \snoc\@activeRibbons{#1}
+}
+
+% Add the given identifier to the list of active boxes.
+\newcommand*\@addToActiveBoxes[1]{%
+ \snoc\@activeBoxes{#1}
+}
+
+% Add the given numbers to the lists of left- and
+% right-positions of blocks.
+\newcommand*\@addToBlocks[2]{%
+ \cons\@blocks@left{#1}
+ \cons\@blocks@right{#2}
+}
+
+% Return the left-/right-position of the current
+% block step
+\newcommand*\@getBlockLeft{%
+ \expandafter\headOfList\@blocks@left
+}
+\newcommand*\@getBlockRight{%
+ \expandafter\headOfList\@blocks@right
+}
+
+% Remove the given identifier from the list of active ribbons.
+\newcommand*\@removeFromActiveRibbons[1]{%
+ \@expandtwoargs\@removeelement{#1}\@activeRibbons\@activeRibbons
+ \global\let\@activeRibbons=\@activeRibbons
+}
+
+% Remove the given identifier from the list of active boxes.
+\newcommand*\@removeFromActiveBoxes[1]{%
+ \@expandtwoargs\@removeelement{#1}\@activeBoxes\@activeBoxes
+ \global\let\@activeBoxes=\@activeBoxes
+}
+
+% Split a label into a variable resource and a remainder. For
+% instance:
+% \@splitLabel{foo&bar}{\l}{\r} defines \l=foo and \r=bar.
+% \@splitLabel{foo}{\l}{\r} defines \l={} and \r=foo.
+% \@splitLabel{&foo}{\l}{\r} defines \l={} and \r=foo.
+% \@splitLabel{foo&}{\l}{\r} defines \l=foo and \r={}.
+\newcommand\@splitLabel[3]{
+{ % This brace-group is to protect the ampersand from
+% a possibly-enclosing tabular or array environment.
+ \noexpandarg
+ \IfSubStr{#1}{&}{
+ \StrCut{#1}{&}{#2}{#3}
+ }{
+ \gdef #2{}
+ \gdef #3{#1}
+ }
+}
+}
+
+% Draw a label on a ribbon
+\newcommand*\@drawRibbonLabel{%
+ {
+ %
+ % \expandarg arranges that each argument to xstring macros
+ % is expanded once, rather than fully or not at all.
+ \expandarg
+ \IfSubStr{\ribbons@label}{&}{
+ \StrCut{\ribbons@label}{&}{\ribbons@varlabel}{\ribbons@label}
+ }{
+ \gdef \ribbons@varlabel{}
+ }
+ \pgfmathsetmacro\@ribbonCentre{%
+ 0.5*(\ribbons@left+\ribbons@varwidth+\ribbons@right)}
+ \pgfmathsetmacro\@ribbonVarCentre{%
+ \ribbons@left+(0.5*\ribbons@varwidth)}
+ \begin{pgfonlayer}{foreground}
+ \node[text=\ribTextColor, anchor=base] at
+ (\@ribbonCentre,\theVCursor+\ribbons@stepheight+\ribTextVOffset)
+ {$\begin{array}[t]{@{}l@{}}\ribbons@label\end{array}$};
+ \node[rotate=-90,text=\ribTextColor, anchor=west] at
+ (\@ribbonCentre,\theVCursor+\ribbons@stepheight)
+ {$\begin{array}[t]{@{}l@{}}\ribbons@rotatedlabel\end{array}$};
+ \node[text=\ribTextColor, anchor=base] at
+ (\@ribbonVarCentre,\theVCursor+\ribbons@stepheight+\ribTextVOffset)
+ {$\begin{array}[t]{@{}l@{}}\ribbons@varlabel\end{array}$};
+ \end{pgfonlayer}
+ }
+}
+
+% Draw a label on a box
+\newcommand*\@drawBoxLabel{%
+ \begin{pgfonlayer}{foreground}
+ \node[text=\ribbons@boxcolor, anchor=east,%
+ inner sep=0mm, fill=white] at
+ (\ribbons@left+\boxTextHOffset+\ribbons@boxhoffset,%
+ \theVCursor+\ribbons@stepheight+%
+ \boxTextVOffset+\ribbons@boxvoffset)%
+ {\strut $\ribbons@label$};
+ \end{pgfonlayer}
+}
+
+% For each ribbon in \ribbons@startribbons, add its identifier to
+% the list of active ribbons, store its horizontal position, and
+% draw a label on it.
+\newcommand*\@processStartRibbons{%
+ \foreach \i/\values in \ribbons@startribbons {
+ %
+ % If ribbon \i is already active, raise an error.
+ \@ifListContains{\i}{\@activeRibbons}{\errorI\i}{}
+ %
+ % NB. The space after \i is important, otherwise
+ % the command \iLEFT will be expanded.
+ \ifcsname RIB\i LEFT\endcsname
+ \gdef\defaultLeft{\@getRibbonLeft\i}
+ \gdef\defaultRight{\@getRibbonRight\i}
+ \gdef\defaultVarWidth{\@getRibbonVarWidth\i}
+ \else
+ \gdef\defaultLeft{0}
+ \gdef\defaultRight{0}
+ \gdef\defaultVarWidth{0}
+ \fi
+ \pgfqkeys{/ribbons/ribbon}{%
+ left=\defaultLeft,right=\defaultRight,%
+ label={}, rotated label={},%
+ var width=\defaultVarWidth,%
+ execute macro=\values}
+ \ExpandNextFour\@defineRibbon\i\ribbons@left%
+ \ribbons@right\ribbons@varwidth\ribbons@label
+ \@rememberPrevRibbon\i
+ \ExpandNext\@addToActiveRibbons\i
+ \@drawRibbonLabel
+ }
+}
+
+% For each box in \ribbons@startboxes, add its identifier to
+% the list of active boxes, store its horizontal position, and
+% draw a label on it.
+\newcommand*\@processStartBoxes{%
+ \foreach \i/\values in \ribbons@startboxes {
+ %
+ % If existential box \i is already active, raise an error.
+ \@ifListContains{\i}{\@activeBoxes}{\errorII\i}{}
+ %
+ % NB. The space after \i is important, since otherwise
+ % the command \iLEFT would be expanded.
+ \ifcsname BOX\i LEFT\endcsname
+ \gdef\defaultLeft{\@getBoxLeft\i}
+ \gdef\defaultRight{\@getBoxRight\i}
+ \gdef\defaultColor{\@getBoxColor\i}
+ \else
+ \gdef\defaultLeft{0}
+ \gdef\defaultRight{0}
+ \gdef\defaultColor{black}
+ \fi
+ \pgfqkeys{/ribbons/box}{%
+ left=\defaultLeft,right=\defaultRight,%
+ color=\defaultColor,label={},%
+ vertical offset=0, horizontal offset=0,%
+ execute macro=\values}
+ \ExpandNextThree\@defineBox%
+ \i\ribbons@left\ribbons@right\ribbons@boxcolor\ribbons@label
+ \@rememberPrevBox\i
+ \ExpandNext\@addToActiveBoxes\i
+ \@drawBoxLabel
+ }
+}
+
+% Draw a rectangle whose northwest and northeast corners are
+% rounded. Usage:
+% \topRoundedRectangle{<left>}{<top>}{<right>}{<bottom>}
+\newcommand\topRoundedRectangle[4]{%
+ \fill[\ribbons@color] (#1+\roundingRadius,#2)
+ to[bend right=45] (#1,#2+\roundingRadius)
+ -- (#1,#4) -- (#3,#4) -- (#3,#2+\roundingRadius)
+ to[bend right=45] (#3-\roundingRadius,#2) -- cycle;
+}
+
+% Draw a rectangle whose southwest and southeast corners are
+% rounded. Usage:
+% \botRoundedRectangle{<left>}{<top>}{<right>}{<bottom>}
+\newcommand\botRoundedRectangle[4]{%
+ \fill[\ribbons@color] (#1+\roundingRadius,#4)
+ to[bend left=45] (#1,#4-\roundingRadius)
+ -- (#1,#2) -- (#3,#2) -- (#3,#4-\roundingRadius)
+ to[bend left=45] (#3-\roundingRadius,#4) -- cycle;
+}
+
+%\@roundedElbow{<radius>}{<y>}{<oldx>}{<newx>}
+%
+% y .------------'
+% newx oldx
+\newcommand\@roundedElbow[4]{%
+ \def\radius{#1}
+ \def\y{#2}
+ \def\oldx{#3}
+ \def\newx{#4}
+ \pgfmathsetmacro\oldxsuc{\oldx+1}
+ \pgfmathsetmacro\newxsuc{\newx+1}
+ \ifnum\newx=\oldx
+ \draw(\oldx,\y-\radius) -- (\newx,\y+\radius);
+ \fi
+ \ifnum\oldx>\newx
+ \ifnum\oldx=\newxsuc
+ \draw(\oldx,\y-\radius)
+ to[in=90, out=270] (\newx,\y+\radius);
+ \else
+ \draw(\oldx,\y-\radius)
+ to[bend left=45] (\oldx-\radius,\y)
+ -- (\newx+\radius,\y)
+ to[bend right=45] (\newx,\y+\radius);
+ \fi
+ \fi
+ \ifnum\newx>\oldx
+ \ifnum\newx=\oldxsuc
+ \draw(\oldx,\y-\radius)
+ to[in=90, out=270] (\newx,\y+\radius);
+ \else
+ \draw(\oldx, \y-\radius)
+ to[bend right=45] (\oldx+\radius,\y)
+ -- (\newx-\radius,\y)
+ to[bend left=45] (\newx,\y+\radius);
+ \fi
+ \fi
+}
+
+% Check that each fit ribbon is currently active. If
+% not, raise an error.
+\newcommand*\@checkFitRibbons{%
+ \foreach\i in \ribbons@fitribbons {
+ \@ifListContains{\i}{\@activeRibbons}{}{\errorIII\i}
+ }
+}
+
+% Check that each incoming ribbon is currently active. If
+% not, raise an error.
+\newcommand*\@checkFinishRibbons{%
+ \foreach\i in \ribbons@finishribbons {
+ \@ifListContains{\i}{\@activeRibbons}{}{\errorIV\i}
+ }
+}
+
+% Check that each fit box is currently active. If
+% not, raise an error.
+\newcommand*\@checkFitBoxes{%
+ \foreach\i in \ribbons@fitboxes {
+ \@ifListContains{\i}{\@activeBoxes}{}{\errorV\i}
+ }
+}
+
+% Check that each incoming box is currently active. If
+% not, raise an error.
+\newcommand*\@checkFinishBoxes{%
+ \foreach\i in \ribbons@finishboxes {
+ \@ifListContains{\i}{\@activeBoxes}{}{\errorVI\i}
+ }
+}
+
+% Draw some notches down the left-hand edge of the picture
+% (only when "draw grid" is enabled).
+\newcommand\@drawNotches{%
+ \coordinate (originalbb) at (current bounding box.north west);
+ \foreach \yvalue in \notchPositions {%
+ \draw[\guideTextColor, ultra thick]
+ let \p1 = (originalbb) in
+ (\x1-1mm,\yvalue) -- ++(-3,0);
+ }
+}
+
+% Draw shadows over "catalyst" ribbons, that is, those
+% ribbons that pass underneath a step without being a
+% precondition of that step.
+\newcommand*\@drawShadowsOverCatalysts{%
+ \pgfmathsetmacro\@shadowTop{\theVCursor+\ribbons@stepheight}
+ \foreach\i in \ribbons@catalystribbons {
+ \pgfmathsetmacro\@shadowLeft{\@getRibbonLeft\i}
+ \pgfmathsetmacro\@shadowRight{\@getRibbonRight\i}
+ \fill[\shadowColor, path fading=south]
+ (\@shadowLeft,\@shadowTop) rectangle
+ (\@shadowRight,\@shadowTop+\shadowHeight);
+ }
+}
+
+% Draw a segment of a box
+\newcommand*\@drawExistentialBox[2]{%
+ \pgfmathsetmacro\xtopleft{\@getBoxPrevLeft{#1}}
+ \pgfmathsetmacro\xtopright{\@getBoxPrevRight{#1}}
+ \pgfmathsetmacro\xbotleft{\@getBoxLeft{#1}}
+ \pgfmathsetmacro\xbotright{\@getBoxRight{#1}}
+ \def\lineColor{\@getBoxColor{#1}}
+ \def\ytop{#2}
+ \def\ybot{\theVCursor+\theRowHeight}
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isRectangular{%
+ (\xtopleft==\xbotleft)&&(\xtopright==\xbotright)}
+ \begin{scope}[draw=\lineColor,line width=\boxLineWidth]
+ \ifnum\isRectangular=1\relax
+ \draw[xshift=0.5*\boxLineWidth]
+ (\xtopleft,\ytop) -- (\xbotleft,\ybot);
+ \draw[xshift=-0.5*\boxLineWidth]
+ (\xtopright,\ytop) -- (\xbotright,\ybot);
+ \else
+ \def\controlLength{\twistiness*\theRowHeight}
+ \draw[xshift=-0.5*\boxLineWidth]
+ (\xtopright,\ytop)
+ .. controls (\xtopright,\ytop+\controlLength)
+ and (\xbotright,\ybot-\controlLength)
+ .. (\xbotright,\ybot);
+ \draw[xshift=0.5*\boxLineWidth]
+ (\xbotleft,\ybot)
+ .. controls (\xbotleft,\ybot-\controlLength)
+ and (\xtopleft,\ytop+\controlLength)
+ .. (\xtopleft,\ytop);
+ \fi
+ \end{scope}
+ }
+}
+
+% Draw a segment of a ribbon
+\newcommand*\@drawRibbon[1]{%
+ \pgfmathsetmacro\xtopleft{\@getRibbonPrevLeft{#1}}
+ \pgfmathsetmacro\xtopmid{%
+ \@getRibbonPrevLeft{#1}+\@getRibbonPrevVarWidth{#1}}
+ \pgfmathsetmacro\xtopright{\@getRibbonPrevRight{#1}}
+ \pgfmathsetmacro\xbotleft{\@getRibbonLeft{#1}}
+ \pgfmathsetmacro\xbotmid{%
+ \@getRibbonLeft{#1}+\@getRibbonVarWidth{#1}}
+ \pgfmathsetmacro\xbotright{\@getRibbonRight{#1}}
+ \def\ytop{\theVCursor}
+ \def\ybot{\theVCursor+\theRowHeight}
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isRectangular{%
+ (\xtopleft==\xbotleft)&&%
+ (\xtopmid==\xbotmid)&&%
+ (\xtopright==\xbotright)}
+ \ifnum\isRectangular=1\relax
+ \fill[\varribColor] (\xtopleft,\ytop)
+ rectangle (\xbotmid,\ybot);
+ \fill[\ribColor] (\xtopmid,\ytop)
+ rectangle (\xbotright,\ybot);
+ \else
+ \def\controlLength{\twistiness*\theRowHeight}
+ \def\rightDownwardCurve{(\xtopright,\ytop)
+ .. controls (\xtopright,\ytop+\controlLength)
+ and (\xbotright,\ybot-\controlLength)
+ .. (\xbotright,\ybot)}
+ \def\midDownwardCurve{(\xtopmid,\ytop)
+ .. controls (\xtopmid,\ytop+\controlLength)
+ and (\xbotmid,\ybot-\controlLength)
+ .. (\xbotmid,\ybot)}
+ \def\midUpwardCurve{(\xbotmid,\ybot)
+ .. controls (\xbotmid,\ybot-\controlLength)
+ and (\xtopmid,\ytop+\controlLength)
+ .. (\xtopmid,\ytop)}
+ \def\leftUpwardCurve{(\xbotleft,\ybot)
+ .. controls (\xbotleft,\ybot-\controlLength)
+ and (\xtopleft,\ytop+\controlLength)
+ .. (\xtopleft,\ytop)}
+ \draw[white, line width=0.5mm] \rightDownwardCurve;
+ \draw[white, line width=0.5mm] \leftUpwardCurve;
+ \fill[\ribColor] \rightDownwardCurve
+ -- \midUpwardCurve -- cycle;
+ \fill[\varribColor] \midDownwardCurve
+ -- \leftUpwardCurve -- cycle;
+ \fi
+ }
+ \if@showids
+ \begin{pgfonlayer}{foreground}
+ \def\guideY{\theVCursor+\theRowHeight-\guideTextVOffset}
+ \node[anchor=west,text=\guideTextColor] at
+ (\@getRibbonLeft\i, \guideY) {\strut \i};
+ \end{pgfonlayer}
+ \fi
+}
+
+% ===================================================================
+% MAIN COMMANDS
+% Accessible to the user
+
+% Finish the current row. A synonym for "\\".
+\newcommand*\finishrow[1][0]{%
+ \setcounter{RowHeight}{\defaultRowHeight}
+ \addtocounter{RowHeight}{#1}
+ %
+ % For each active ribbon, draw it and its guide text
+ \foreach\i in \@activeRibbons {%
+ \@drawRibbon\i
+ }
+ %
+ % For each active existential box, draw it
+ \foreach\i in \@activeBoxes {%
+ \@drawExistentialBox\i\theVCursor
+ }
+ %
+ % For each block step, draw the side lines that delimit it.
+ \foreach\x in \@blocks@left {%
+ \fill[\comColor] (\x,\theVCursor) rectangle
+ (\x+\blockLineWidth,\theVCursor+\theRowHeight);
+ }
+ \foreach\x in \@blocks@right {%
+ \fill[\comColor] (\x,\theVCursor) rectangle
+ (\x-\blockLineWidth,\theVCursor+\theRowHeight);
+ }
+ %
+ % If there is a block step in "purgatory", i.e. one that
+ % has been added during the current row, then draw in its
+ % side lines, and add it to the list of block steps.
+ \ifx\@blocksPurgatory@left\@empty
+ \else
+ \pgfmathsetmacro\xleft{\@blocksPurgatory@left}
+ \pgfmathsetmacro\xright{\@blocksPurgatory@right}
+ \pgfmathsetmacro\ytop{\theVCursor+\ribbons@stepheight}
+ \pgfmathsetmacro\ybot{\theVCursor+\theRowHeight}
+ \fill[\comColor] (\xleft,\ytop) rectangle
+ (\xleft+\blockLineWidth,\ybot);
+ \fill[\comColor] (\xright,\ytop) rectangle
+ (\xright-\blockLineWidth,\ybot);
+ \@addToBlocks\xleft\xright
+ \xdef\@blocksPurgatory@left{}
+ \xdef\@blocksPurgatory@right{}
+ \fi
+ %
+ % If there are existential boxes in "purgatory", i.e. some
+ % that have been extended during the current row, then draw
+ % them a little lower than normal (to leave vertical space
+ % for the extension operation), take them out of purgatory,
+ % and put them back into the set of active boxes.
+ \foreach \i in \@boxPurgatory {
+ \ExpandNext\@addToActiveBoxes\i
+ \@drawExistentialBox{\i}{\theVCursor+2*\boxRoundingRadius}
+ }
+ \xdef\@boxPurgatory{}
+ %
+ % Update i@prevleft and i@prevright for each active ribbon i
+ \foreach\i in \@activeRibbons {\@rememberPrevRibbon\i}
+ \foreach\i in \@activeBoxes {\@rememberPrevBox\i}
+ %
+ % If the grid is on, draw a little notch to show where
+ % the row ends
+ \snoc\notchPositions{\theVCursor}
+ % \if@showids
+ % \draw[\guideTextColor, ultra thick]
+ % (0,\theVCursor) -- (3,\theVCursor);
+ % \fi
+ %
+ % Advance the vertical cursor by the height of this row
+ \addtocounter{VCursor}{\theRowHeight}%
+ %
+ % Reset the step height to the default value
+ \xdef\ribbons@stepheight{\defaultStepHeight}
+}
+
+% A command step or a justification step
+\newcommand*\step[2][]{%
+ %
+ % Set default values for the step colour and
+ % whether it is the start/end of a block step
+ \def\ribbons@color{\comColor}
+ %
+ % Process keys
+ \pgfqkeys{/ribbons/step}{%
+ extra left=0,extra right=0,text={},%
+ finish ribbons={},start ribbons={},%
+ finish boxes={},start boxes={},%
+ height=\defaultStepHeight,#1,text={#2}}
+ %
+ \@checkFinishRibbons
+ \@checkFinishBoxes
+ %
+ % Build lists of all the left positions and right positions
+ % of the incoming ribbons (we must do this early, because
+ % their positions might get overwritten shortly).
+ \gdef\@leftPositions{}
+ \gdef\@rightPositions{}
+ \foreach\i in \ribbons@finishribbons {
+ \xdef\@leftPositions{\@getRibbonLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getRibbonRight\i,\@rightPositions}
+ }
+ \foreach\i in \ribbons@finishboxes {
+ \xdef\@leftPositions{\@getBoxLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getBoxRight\i,\@rightPositions}
+ }
+ %
+ % remove the incoming ribbons from the list of active
+ % ribbons.
+ \foreach\i in \ribbons@finishribbons {
+ \ExpandNext\@removeFromActiveRibbons\i
+ }
+ \foreach\i in \ribbons@finishboxes {
+ \ExpandNext\@removeFromActiveBoxes\i
+ }
+ %
+ % Store the data for the outgoing ribbons, draw their labels,
+ % and add them to the list of active ribbons
+ \@processStartRibbons
+ \@processStartBoxes
+ %
+ % Continue the lists of all the left positions and right
+ % positions, by adding the data for the outgoing ribbons
+ \foreach \i/\values in \ribbons@startribbons {
+ \xdef\@leftPositions{\@getRibbonLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getRibbonRight\i,\@rightPositions}
+ }
+ \foreach \i/\values in \ribbons@startboxes {
+ \xdef\@leftPositions{\@getBoxLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getBoxRight\i,\@rightPositions}
+ }
+ %
+ % Calculate the minimum left position and the maximum
+ % right position
+ \pgfmathsetmacro\@minPosition{min(\@leftPositions)}
+ \pgfmathsetmacro\@maxPosition{max(\@rightPositions)}
+ %
+ % Calculate the size and position of the step
+ \pgfmathsetmacro\@stepLeft{\@minPosition-\ribbons@extraleft}
+ \pgfmathsetmacro\@stepRight{\@maxPosition+\ribbons@extraright}
+ %
+ \pgfmathsetmacro\@stepCentre{\@stepLeft*0.5+\@stepRight*0.5}
+ \pgfmathsetmacro\@stepTop{\theVCursor}
+ \pgfmathsetmacro\@stepMid{\theVCursor+0.5*\ribbons@stepheight}
+ \pgfmathsetmacro\@stepBot{\theVCursor+\ribbons@stepheight}
+ %
+ % Draw the step
+ \begin{pgfonlayer}{foreground}
+ \fill[\ribbons@color] (\@stepLeft,\@stepTop)
+ rectangle (\@stepRight,\@stepBot);
+ %
+ % Draw the text on the step
+ \node[text=white,anchor=west] at (\@stepLeft, \@stepMid)
+ {\begin{tabular}{@{}l@{}}\ribbons@text\end{tabular}};
+ %
+ % Work out which ribbons are the catalysts and draw
+ % shadows over them.
+ \gdef\ribbons@catalystribbons{}
+ \foreach\i in \@activeRibbons {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getRibbonLeft\i)) &&
+ ((\@getRibbonRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \@ifPairListContainsKey\i\ribbons@startribbons{}{%
+ \cons\ribbons@catalystribbons\i
+ }
+ \fi}
+ }
+ \@drawShadowsOverCatalysts
+ \end{pgfonlayer}
+}
+
+% A justification step
+\newcommand*\jus[2][]{%
+ \step[pale,#1]{#2}}
+
+% A command step
+\newcommand*\com[2][]{%
+ \step[#1]{\texttt{#2}}}
+
+% The start of a block
+\newcommand*\startblock[2][] {%
+ %
+ % Set default value for the step colour
+ \def\ribbons@color{\comColor}
+ %
+ % Process keys
+ \pgfqkeys{/ribbons/startblock}{%
+ extra left=0,extra right=0,text={},%
+ finish ribbons={},start ribbons={},fit ribbons={},%
+ finish boxes={},start boxes={}, fit boxes={},%
+ height=\defaultStepHeight,#1,text={#2}}
+ %
+ \@checkFitRibbons
+ \@checkFinishRibbons
+ \@checkFitBoxes
+ \@checkFinishBoxes
+ %
+ % Build lists of all the left positions and right positions
+ % of the incoming/fit ribbons (we must do this early, because
+ % the positions of the incoming ribbons might get overwritten
+ % shortly).
+ \gdef\@leftPositions{}
+ \gdef\@rightPositions{}
+ \foreach\i in \ribbons@finishribbons {
+ \xdef\@leftPositions{\@getRibbonLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getRibbonRight\i,\@rightPositions}
+ }
+ \foreach\i in \ribbons@fitribbons {
+ \xdef\@leftPositions{\@getRibbonLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getRibbonRight\i,\@rightPositions}
+ }
+ \foreach\i in \ribbons@finishboxes {
+ \xdef\@leftPositions{\@getBoxLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getBoxRight\i,\@rightPositions}
+ }
+ \foreach\i in \ribbons@fitboxes {
+ \xdef\@leftPositions{\@getBoxLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getBoxRight\i,\@rightPositions}
+ }
+ %
+ % Remove the incoming ribbons from the list of active
+ % ribbons.
+ \foreach\i in \ribbons@finishribbons {
+ \ExpandNext\@removeFromActiveRibbons\i
+ }
+ \foreach\i in \ribbons@finishboxes {
+ \ExpandNext\@removeFromActiveBoxes\i
+ }
+ %
+ % Store the data for the outgoing ribbons, draw their labels,
+ % and add them to the list of active ribbons
+ \@processStartRibbons
+ \@processStartBoxes
+ %
+ % Continue the lists of all the left positions and right
+ % positions, by adding the data for the outgoing ribbons
+ \foreach \i/\values in \ribbons@startribbons {
+ \xdef\@leftPositions{\@getRibbonLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getRibbonRight\i,\@rightPositions}
+ }
+ \foreach \i/\values in \ribbons@startboxes {
+ \xdef\@leftPositions{\@getBoxLeft\i,\@leftPositions}
+ \xdef\@rightPositions{\@getBoxRight\i,\@rightPositions}
+ }
+ %
+ % Calculate the minimum left position and the maximum
+ % right position
+ \pgfmathsetmacro\@minPosition{min(\@leftPositions)}
+ \pgfmathsetmacro\@maxPosition{max(\@rightPositions)}
+ %
+ % Calculate the size and position of the step
+ \pgfmathsetmacro\@stepLeft{\@minPosition-\ribbons@extraleft}
+ \pgfmathsetmacro\@stepRight{\@maxPosition+\ribbons@extraright}
+ %
+ \pgfmathsetmacro\@stepCentre{\@stepLeft*0.5+\@stepRight*0.5}
+ \pgfmathsetmacro\@stepTop{\theVCursor}
+ \pgfmathsetmacro\@stepMid{\theVCursor+0.5*\ribbons@stepheight}
+ \pgfmathsetmacro\@stepBot{\theVCursor+\ribbons@stepheight}
+ %
+ % Draw a top-rounded rectangle, and add it to
+ % purgatory (the list of almost-ready block steps).
+ \begin{pgfonlayer}{foreground}
+ \topRoundedRectangle\@stepLeft\@stepTop\@stepRight\@stepBot
+ \xdef\@blocksPurgatory@left{\@stepLeft}
+ \xdef\@blocksPurgatory@right{\@stepRight}
+ %
+ % Draw the text on the step
+ \node[text=white,anchor=west] at (\@stepLeft, \@stepMid)
+ {\begin{tabular}{@{}l@{}}\texttt{\ribbons@text}\end{tabular}};
+ %
+ % Work out which ribbons and boxes are the catalysts
+ \gdef\ribbons@catalystribbons{}
+ \foreach\i in \@activeRibbons {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getRibbonLeft\i)) &&
+ ((\@getRibbonRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \@ifPairListContainsKey\i\ribbons@startribbons{}{%
+ \cons\ribbons@catalystribbons\i
+ }
+ \fi}
+ }
+ \gdef\ribbons@catalystboxes{}
+ \foreach\i in \@activeBoxes {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getBoxLeft\i)) &&
+ ((\@getBoxRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \@ifPairListContainsKey\i\ribbons@startboxes{}{%
+ \cons\ribbons@catalystboxes\i
+ }
+ \fi}
+ }
+ %
+ % Save the position of each catalyst ribbon/box.
+ % These positions will be restored each
+ % time we enter an "else" branch.
+ \def\@savedRibbonPositions{}
+ \foreach\i in \ribbons@catalystribbons {
+ \edef\stuffToSave{%
+ \i/\@getRibbonLeft\i/\@getRibbonRight\i/%
+ {{\@getRibbonLabel\i}}}
+ \snoc\@savedRibbonPositions{\stuffToSave}
+ }
+ \cons[;]\@blocks@state{\@savedRibbonPositions}
+ \def\@savedBoxPositions{}
+ \foreach\i in \ribbons@catalystboxes {
+ \edef\stuffToSave{%
+ \i/\@getBoxLeft\i/\@getBoxRight\i/\@getBoxColor\i/%
+ {{\@getBoxLabel\i}}}
+ \snoc\@savedBoxPositions{\stuffToSave}
+ }
+ \cons[;]\@blocks@boxstate{\@savedBoxPositions}
+ %
+ \@drawShadowsOverCatalysts
+ \end{pgfonlayer}
+}
+
+% The middle of a block
+\newcommand*\continueblock[2][]{%
+ %
+ % Set default value for the step colour
+ \def\ribbons@color{\comColor}
+ %
+ % Set "jagged" flag to 0 (off)
+ \def\ribbons@jagged{0}
+ %
+ % Set "repeat labels" flag to 0 (off)
+ \def\ribbons@repeatlabels{0}
+ %
+ % Process keys
+ \pgfqkeys{/ribbons/continueblock}{%
+ start ribbons={},start boxes={},%
+ height=\defaultStepHeight,#1,text={#2}}
+ %
+ % Add to the set of start ribbons those ribbons that were saved
+ % at the start of this block step, and draw shadows
+ % over them.
+ {\expandarg
+ \StrCut{\@blocks@state}{;}{\@restoredRibbonCatalysts}{\foo}
+ \begin{pgfonlayer}{foreground}
+ \foreach \i/\xleft/\xright/\lab in \@restoredRibbonCatalysts {
+ \ifnum\ribbons@repeatlabels=1
+ \edef\@tmp{%
+ \i/{left=\xleft,right=\xright,label={\expandonce\lab}}}
+ \else
+ \edef\@tmp{\i/{left=\xleft,right=\xright,label={}}}
+ \fi
+ \cons\ribbons@startribbons\@tmp
+ \fill[\shadowColor, path fading=south]
+ (\xleft,\theVCursor+\ribbons@stepheight) rectangle
+ (\xright,\theVCursor+\ribbons@stepheight+\shadowHeight);
+ }
+ \end{pgfonlayer}
+ }
+ {\expandarg
+ \StrCut{\@blocks@boxstate}{;}{\@restoredBoxCatalysts}{\foo}
+ \foreach \i/\xleft/\xright/\color/\lab in \@restoredBoxCatalysts {
+ \ifnum\ribbons@repeatlabels=1
+ \edef\@tmp{\i/{left=\xleft,right=\xright,color=\color,%
+ label={\expandonce\lab}}}
+ \else
+ \edef\@tmp{\i/{left=\xleft,right=\xright,color=\color,%
+ label={}}}
+ \fi
+ \cons\ribbons@startboxes\@tmp
+ }
+ }
+ %
+ % Left and right positions are extracted from the
+ % stack of block left/right positions
+ \StrCut{\@blocks@left}{,}{\@stepLeft}{\foo}
+ \global\let\@stepLeft=\@stepLeft
+ \StrCut{\@blocks@right}{,}{\@stepRight}{\foo}
+ \global\let\@stepRight=\@stepRight
+ %
+ % Obliterate the ribbons/boxes inside the footprint of the
+ % block step.
+ \foreach\i in \@activeRibbons {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getRibbonLeft\i)) &&
+ ((\@getRibbonRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \ExpandNext\@removeFromActiveRibbons\i
+ \fi}
+ }
+ \foreach\i in \@activeBoxes {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getBoxLeft\i)) &&
+ ((\@getBoxRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \ExpandNext\@removeFromActiveBoxes\i
+ \fi}
+ }
+ %
+ % Store the data for the outgoing ribbons, draw their labels,
+ % and add them to the list of active ribbons
+ \@processStartRibbons
+ \@processStartBoxes
+ %
+ \pgfmathsetmacro\@stepCentre{\@stepLeft*0.5+\@stepRight*0.5}
+ \pgfmathsetmacro\@stepTop{\theVCursor}
+ \pgfmathsetmacro\@stepMid{\theVCursor+0.5*\ribbons@stepheight}
+ \pgfmathsetmacro\@stepBot{\theVCursor+\ribbons@stepheight}
+ %
+ % Draw a rectangle for this step. If "jagged" is set, then
+ % draw a saw-tooth top to indicate that there is a
+ % discontinuity at this step.
+ \begin{pgfonlayer}{foreground}
+ \ifnum\ribbons@jagged=1
+ \fill[\ribbons@color] (\@stepLeft,\@stepTop)
+ decorate [decoration=saw, segment length=\zigzagLength mm] {
+ -- (\@stepRight,\@stepTop)
+ }
+ -- (\@stepRight,\@stepBot)
+ -- (\@stepLeft,\@stepBot)
+ -- cycle;
+ \else
+ \fill[\ribbons@color] (\@stepLeft,\@stepTop)
+ rectangle (\@stepRight,\@stepBot);
+ \fi
+ %
+ % Draw the text on the step
+ \node[text=white,anchor=west] at (\@stepLeft, \@stepMid)
+ {\begin{tabular}{@{}l@{}}\texttt{\ribbons@text}\end{tabular}};
+ %
+ \end{pgfonlayer}
+}
+
+% The end of a block
+\newcommand*\finishblock[2][]{%
+ %
+ % Set default values for the step colour and
+ % whether it is the start/end of a block step
+ \def\ribbons@color{\comColor}
+ %
+ % Process keys
+ \pgfqkeys{/ribbons/finishblock}{%
+ finish ribbons={},start ribbons={},%
+ finish boxes={},start boxes={},%
+ height=\defaultStepHeight,#1,text={#2}}
+ %
+ \@checkFinishRibbons
+ \@checkFinishBoxes
+ %
+ % Left and right positions are extracted from the
+ % stack of block left/right positions
+ \StrCut{\@blocks@left}{,}{\@stepLeft}{\foo}
+ \StrCut{\@blocks@right}{,}{\@stepRight}{\foo}
+ %
+ % Make the ribbons that are inside the footprint of the
+ % block step, but are not start ribbons, into
+ % catalyst ribbons.
+ \gdef\ribbons@catalystribbons{}
+ \foreach\i in \@activeRibbons {
+ { % a group to hide the &&-operator from a
+ % possibly-enclosing tabular environment.
+ \pgfmathtruncatemacro\isInside{%
+ (\@stepLeft<=(\@getRibbonLeft\i)) &&
+ ((\@getRibbonRight\i)<=\@stepRight)}
+ \ifnum\isInside=1
+ \@ifPairListContainsKey\i\ribbons@startribbons{}{%
+ \cons\ribbons@catalystribbons\i
+ }
+ \fi}
+ }
+ %
+ % Remove the incoming ribbons from the list of active
+ % ribbons.
+ \foreach\i in \ribbons@finishribbons {
+ \ExpandNext\@removeFromActiveRibbons\i
+ }
+ \foreach\i in \ribbons@finishboxes {
+ \ExpandNext\@removeFromActiveBoxes\i
+ }
+ %
+ % Store the data for the outgoing ribbons, draw their labels,
+ % and add them to the list of active ribbons
+ \@processStartRibbons
+ \@processStartBoxes
+ %
+ \pgfmathsetmacro\@stepCentre{\@stepLeft*0.5+\@stepRight*0.5}
+ \pgfmathsetmacro\@stepTop{\theVCursor}
+ \pgfmathsetmacro\@stepMid{\theVCursor+0.5*\ribbons@stepheight}
+ \pgfmathsetmacro\@stepBot{\theVCursor+\ribbons@stepheight}
+ %
+ % Draw a bottom-rounded rectangle, and remove it from the
+ % the stack of block steps.
+ \begin{pgfonlayer}{foreground}
+ \botRoundedRectangle\@stepLeft\@stepTop\@stepRight\@stepBot
+ \StrBehind{\@blocks@left}{,}[\@blocks@left]
+ \global\let\@blocks@left=\@blocks@left
+ \StrBehind{\@blocks@right}{,}[\@blocks@right]
+ \global\let\@blocks@right=\@blocks@right
+ {\expandarg
+ \StrBehind{\@blocks@state}{;}[\@blocks@state]
+ \global\let\@blocks@state=\@blocks@state
+ }
+ %
+ % Draw the text on the step
+ \node[text=white,anchor=west] at (\@stepLeft, \@stepMid)
+ {\begin{tabular}{@{}l@{}}\texttt{\ribbons@text}\end{tabular}};
+ %
+ \@drawShadowsOverCatalysts
+ \end{pgfonlayer}
+}
+
+% Move ribbons
+\newcommand*\moveribbons[1]{%
+ \foreach \i/\values in {#1} {
+ %
+ % If ribbon \i is not already active, raise an error.
+ \@ifListContains{\i}{\@activeRibbons}{}{\errorVII\i}
+ %
+ \pgfqkeys{/ribbons/ribbon}{%
+ left={\@getRibbonLeft\i}, right={\@getRibbonRight\i},%
+ var width={\@getRibbonVarWidth\i},%
+ execute macro=\values}
+ \ExpandNextFour\@defineRibbon\i\ribbons@left%
+ \ribbons@right\ribbons@varwidth{\@getRibbonLabel\i}
+ % WORRY HERE ABOUT ERROR
+ %
+ % Remove and then re-add ribbon \i. This gives the user control
+ % over the z-order of the ribbons when they are being moved.
+ \ExpandNext\@removeFromActiveRibbons\i
+ \ExpandNext\@addToActiveRibbons\i
+ }
+}
+
+% Move boxes
+\newcommand*\moveboxes[1]{%
+ \foreach \i/\values in {#1} {
+ %
+ % If box \i is not already active, raise an error.
+ \@ifListContains{\i}{\@activeBoxes}{}{\errorVIII\i}
+ %
+ \pgfqkeys{/ribbons/box}{%
+ left={\@getBoxLeft\i},right={\@getBoxRight\i},%
+ color={\@getBoxColor\i},execute macro=\values}
+ \ExpandNextThree\@defineBox%
+ \i\ribbons@left\ribbons@right\ribbons@boxcolor{\@getBoxLabel\i}
+ }
+}
+
+% Permute ribbons
+\newcommand*\swapribbons[2]{%
+ \foreach \i in {#1} {
+ \expandafter\xdef\csname RIB\i @oldleft\endcsname{%
+ \csname RIB\i LEFT\endcsname}
+ \expandafter\xdef\csname RIB\i @oldright\endcsname{%
+ \csname RIB\i RIGHT\endcsname}
+ \expandafter\xdef\csname RIB\i @oldvarwidth\endcsname{%
+ \csname RIB\i VARWIDTH\endcsname}
+ }
+ \def\loopbody##1##2{%
+ \moveribbons{##2/{%
+ left=\csname RIB##1@oldleft\endcsname,%
+ right=\csname RIB##1@oldright\endcsname,%
+ var width=\csname RIB##1@oldvarwidth\endcsname}%
+ }%
+ }
+ \foreachtwo\loopbody{#1}{#2}
+}
+
+% Move boxes (an alternative to \moveboxes)
+\newcommand*\extendboxes[1]{%
+ \foreach \i/\values in {#1} {
+ %
+ % If box \i is not already active, raise an error.
+ \@ifListContains{\i}{\@activeBoxes}{}{\errorIX\i}
+ %
+ \pgfmathtruncatemacro\oldleft{\@getBoxLeft\i}
+ \pgfmathtruncatemacro\oldright{\@getBoxRight\i}
+ \def\boxcolor{\@getBoxColor\i}
+ \pgfqkeys{/ribbons/box}{%
+ left=\oldleft,right=\oldright,%
+ color=\boxcolor,execute macro=\values}
+ \ExpandNextThree\@defineBox%
+ \i\ribbons@left\ribbons@right%
+ \ribbons@boxcolor{\@getBoxLabel\i}
+ \pgfmathtruncatemacro\newleft{\@getBoxLeft\i}
+ \pgfmathtruncatemacro\newright{\@getBoxRight\i}
+ \begin{pgfonlayer}{foreground}
+ \begin{scope}[color=\boxcolor, line width=\boxLineWidth]
+ \begin{scope}[xshift=0.5*\boxLineWidth]
+ \@roundedElbow{\boxRoundingRadius}%
+ {\theVCursor+\boxRoundingRadius}{\oldleft}{\newleft}
+ \end{scope}
+ \begin{scope}[xshift=-0.5*\boxLineWidth]
+ \@roundedElbow{\boxRoundingRadius}%
+ {\theVCursor+\boxRoundingRadius}{\oldright}{\newright}
+ \end{scope}
+ \end{scope}
+ \end{pgfonlayer}
+ %
+ % Update i@prevleft and i@prevright
+ \@rememberPrevBox\i
+ %
+ % Put box i in purgatory, and take it out of the list
+ % of active boxes
+ \ExpandNext\@removeFromActiveBoxes\i
+ \cons\@boxPurgatory{\i}
+ }
+}
+
+\newcommand\ribbonpagebreak{%
+ \end{tikzpicture}
+ \pagebreak
+ % Nudge vertical cursor up a bit. This is a hack to
+ % counteract the fact that the first row does not have
+ % any steps in it. Without this hack, the labels in
+ % the first row would be printed too far down.
+ \addtocounter{VCursor}{-\defaultStepHeight}%
+ \xdef\ribbons@stepheight{\defaultStepHeight}%
+ \begin{tikzpicture}[x=1mm,y=-1mm]
+ %
+ \foreach\i in \@activeRibbons {
+ \edef\ribbons@left{\@getRibbonLeft\i}
+ \edef\ribbons@right{\@getRibbonRight\i}
+ \edef\ribbons@varwidth{\@getRibbonVarWidth\i}
+ \edef\ribbons@label{\@getRibbonLabel\i}
+ \edef\ribbons@rotatedlabel{}
+ \@drawRibbonLabel
+ }
+ \foreach\i in \@activeBoxes {
+ \edef\ribbons@left{\@getBoxLeft\i}
+ \edef\ribbons@right{\@getBoxRight\i}
+ \edef\ribbons@boxcolor{\@getBoxColor\i}
+ \edef\ribbons@label{\@getBoxLabel\i}
+ \edef\ribbons@boxhoffset{0}
+ \edef\ribbons@boxvoffset{0}
+ \@drawBoxLabel
+ }
+ %
+ % Finish this row, making it a shorter row than
+ % usual.
+ \addtocounter{VCursor}{\defaultStepHeight}%
+ \finishrow[\numexpr\ribbons@extraheight-\defaultStepHeight]
+}
+
+% The main environment
+\newenvironment{ribbonproof}[1][]{%
+ %
+ % Create state
+ %
+ % A comma-separated list of active ribbons. Each element is a ribbon
+ % identifier.
+ \gdef\@activeRibbons{}%
+ \gdef\@activeBoxes{}%
+ %
+ % Remember all the ribbons and boxes that we define over
+ % the course of this ribbon proof, so that they can be
+ % undefined at the end.
+ \gdef\@allDefinedRibbons{}%
+ \gdef\@allDefinedBoxes{}%
+ %
+ % Comma-separated lists of the left-positions and right-positions
+ % of block steps
+ \gdef\@blocks@left{}%
+ \gdef\@blocks@right{}%
+ \gdef\@blocks@state{}%
+ \gdef\@blocks@boxstate{}%
+ %
+ % Block steps in purgatory are those that will
+ % be in the proper list from the next row onwards.
+ \gdef\@blocksPurgatory@left{}%
+ \gdef\@blocksPurgatory@right{}%
+ %
+ % Existential boxes in purgatory are those that will
+ % be in the proper list from the next row onwards.
+ \gdef\@boxPurgatory{}%
+ %
+ % List of notches
+ \gdef\notchPositions{}%
+ %
+ % Process keys
+ \pgfqkeys{/ribbons/ribbonproof}{%
+ scale=1,start ribbons={},%
+ extra height=0,start boxes={},#1}%
+ %
+ % Nudge vertical cursor up a bit. This is a hack to
+ % counteract the fact that the first row does not have
+ % any steps in it. Without this hack, the labels in
+ % the first row would be printed too far down.
+ \setcounter{VCursor}{-\defaultStepHeight}%
+ \xdef\ribbons@stepheight{\defaultStepHeight}%
+ %
+ % Make the \\ command a synonym for \finishrow. The
+ % reason for this is mainly to exploit the syntax
+ % highlighting in AucTeX, which emphasises \\ commands.
+ \renewcommand\\{\finishrow}%
+ %
+ % Draw the picture "upside-down", with mm as the units
+ \begin{tikzpicture}[x=1mm,y=-1mm]
+ %
+ % Add the initial ribbons to the list of active
+ % ribbons, and draw their labels
+ \@processStartRibbons
+ \@processStartBoxes
+ %
+ % Prepare the vertical cursor for the first
+ % proper row.
+ \setcounter{VCursor}{0}
+ %
+ % Finish this row, making it a shorter row than
+ % usual.
+ \finishrow[\numexpr\ribbons@extraheight-\defaultStepHeight]
+}{%
+ \end{tikzpicture}%
+ %
+ % Undefine all the ribbons and boxes that were globally
+ % defined over the course of this picture.
+ \foreach\i in \@allDefinedRibbons{%
+ \undefine{RIB\i LEFT}%
+ \undefine{RIB\i RIGHT}%
+ \undefine{RIB\i VARWIDTH}%
+ }%
+ \foreach\i in \@allDefinedBoxes{%
+ \undefine{BOX\i LEFT}%
+ \undefine{BOX\i RIGHT}%
+ \undefine{BOX\i @color}%
+ }%
+}
diff --git a/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.pdf b/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.pdf
new file mode 100644
index 0000000000..c8b2389b23
--- /dev/null
+++ b/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.pdf
Binary files differ
diff --git a/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.tex b/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.tex
new file mode 100644
index 0000000000..44e9ab1f99
--- /dev/null
+++ b/macros/latex/contrib/ribbonproofs/ribbonproofsmanual.tex
@@ -0,0 +1,776 @@
+\documentclass[12pt, a4paper]{article}
+\usepackage[margin=1in]{geometry}
+\usepackage[T1]{fontenc}
+\renewcommand\floatpagefraction{.9}
+\renewcommand\rmdefault{ptm}
+
+\newcommand\COLORcom{MediumPurple}
+\newcommand\COLORkey{SeaGreen}
+\newcommand\COLORenv{MediumPurple}
+\newcommand\COLORpkg{Chocolate}
+\newcommand\COLORdef{blue}
+
+\renewcommand{\refname}{Bibliography}
+\usepackage[nottoc]{tocbibind}
+
+\usepackage{makeidx}
+\makeindex
+\newcommand*\indexkey[1]{\index{%
+ #1 key@\texttt{\color{\COLORkey}#1} key}}
+\newcommand*\indexenv[1]{\index{%
+ #1 environment@\texttt{\color{\COLORenv}#1} environment}}
+\newcommand*\indexcom[1]{\index{%
+ #1 command@\texttt{\color{\COLORcom}\textbackslash #1} command}}
+
+\newcommand*\packagename[1]{\textsf{\color{\COLORpkg}#1}}
+
+\newcommand*\varsasresonly{\emph{[Variables-as-resource only.]}\index{variables-as-resource}}
+
+\newcommand*\Ienv[1]{%
+ {\color{\COLORenv}\ttfamily #1}\indexenv{#1}}
+\newcommand*\Icom[1]{%
+ {\color{\COLORcom}\ttfamily\textbackslash #1}\indexcom{#1}}
+\newcommand*\Ikey[1]{%
+ {\color{\COLORkey}\ttfamily #1}\indexkey{#1}}
+
+
+\usepackage{ribbonproofs}
+
+\usepackage{framed}
+\definecolor{shadecolor}{rgb}{0.95,0.95,1}
+
+\usepackage{tkzexample}
+\colorlet{graphicbackground}{white}
+\colorlet{codebackground}{shadecolor}
+
+%
+\usepackage{listings}
+\lstset{%
+ numbers=left,
+ numberstyle=\tiny,
+ stepnumber=5,
+ basicstyle=\tt,
+ columns=flexible
+}
+%
+
+\usepackage{standalone}
+\usepackage{filecontents} % Otherwise filecontents is
+ % only available in preamble.
+
+\usepackage{fancyvrb}
+\makeatletter
+\renewcommand\FV@FontFamily{\color{\COLORdef}\ttfamily}
+\makeatother
+\DefineShortVerb{\"}
+
+\newcommand{\dotneq}{\mathrel{\not\doteq}}
+\newcommand\ls{\mathit{ls}}
+\newcommand\x{\texttt{x}}
+\newcommand\y{\texttt{y}}
+\renewcommand\t{\texttt{t}}
+\renewcommand\u{\texttt{u}}
+\newcommand\List{\mathit{list}}
+\newcommand*\rev[1]{#1^{\dagger}}
+\newcommand\z{\texttt{z}}
+
+\title{The \packagename{ribbonproofs} package}
+\author{John Wickerson}
+\date{Version 1.0 -- 26 June 2013}
+
+\begin{document}
+
+\maketitle
+
+\begin{abstract}
+\noindent The \packagename{ribbonproofs} package provides a mechanism for drawing ribbon proofs in \LaTeX{}. Ribbon proofs form a diagrammatic proof system, due to Wickerson, Dodds and Parkinson~\cite{wickerson+13}, for \emph{separation logic}~\cite{reynolds02}\index{separation logic}. They build on an earlier system, due to Bean~\cite{bean06}, for \emph{bunched implications logic}~\cite{ohearn+99}.
+\end{abstract}
+
+\tableofcontents
+
+\begin{figure}
+\begin{tkzexample}[]
+\documentclass{standalone}
+\usepackage{ribbonproofs}
+\begin{document}
+\def\to{\mapsto}
+\begin{ribbonproof}[start ribbons={
+ a/{left=0, right=16, label={\tt x}\to0},
+ b/{left=18, right=34, label={\tt y}\to0},
+ c/{left=36, right=52, label={\tt z}\to0}}]
+\com[finish ribbons={a}, start ribbons={
+ a/{label={\tt x}\to1}}]{[x]:=1} \\
+\com[finish ribbons={b}, start ribbons={
+ b/{label={\tt y}\to1}}]{[y]:=1} \\
+\com[finish ribbons={c}, start ribbons={
+ c/{label={\tt z}\to1}}]{[z]:=1} \\
+\end{ribbonproof}
+\end{document}
+\end{tkzexample}
+\caption{A test document and its expected result}
+\label{fig:threeassign}
+\end{figure}
+
+\section{Getting started}
+
+\subsection{Installation}
+
+Place "ribbonproofs.sty" into the same directory as the ".tex" file you are compiling. To test that the package is working correctly, you might try to compile the little document given in Fig.~\ref{fig:threeassign}. The code is given on the lefthand side, with the expected output to the right.
+
+\subsection{A simple example}
+
+Figure~\ref{fig:threeassign} provides a good starting point for examining how the \packagename{ribbonproofs} package works.
+
+All the action takes place within the \Ienv{ribbonproof} environment. Upon entering this environment, we provide the \Ikey{start ribbons} key, in order to introduce some ribbons at the top of the diagram. We name them "a", "b" and "c", give the horizontal positions of their \Ikey{left} and \Ikey{right} edges, and provide a \Ikey{label}.
+
+We then proceed to build up the diagram in a series of rows, each terminated with \Icom{\textbackslash}. In this example, each row comprises a single \Icom{com} step (\Icom{com} stands for `command'). Each \Icom{com} has a number of optional parameters. The \Ikey{finish ribbons} key lists those ribbons that end at this step; they form the step's precondition. The \Ikey{start ribbons} key defines ribbons that begin at this step; they form the step's postcondition. Note that we used the same syntax (\Ikey{start ribbons}) to introduce some ribbons at the beginning of the proof. Each \Icom{com} has one mandatory parameter: the text, which is printed in white on the step.
+
+\section{Examples}
+
+\subsection{List append}
+
+Figure~\ref{fig:listappend} shows the result.
+
+\begin{filecontents*}{temp_example_listappend.tex}
+\begin{ribbonproof}[start ribbons={
+ c/{left=35,right=53,label=\ls\,\x\,0},
+ e/{left=74,right=86,label=\ls\,\y\,0}}]
+\startblock[extra left=33, extra right=2,
+ fit ribbons={c,e}, start ribbons={
+ d/{left=\RIBcRIGHT+2,right=\RIBeLEFT-4,label=\x\doteq 0}
+ }]{if (x==0) \{}
+\\
+\jus[finish ribbons={c,d}]{Unfold $\ls$ def}
+\com[finish ribbons={e}, start ribbons={
+ e/{label=\ls\,\x\,0}}]{x:=y}
+\\[-6]
+\moveribbons{e/{left=4}}
+\\[-1]
+\continueblock[repeat labels, start ribbons={
+ d/{label=\x\dotneq 0}}]{\} else \{}
+\\[-1]
+\jus[start ribbons={
+ a/{left=6,right=\RIBcLEFT-2,label=\ls\,\x\,\x}}]{Fold $\ls$ def}
+\\[-4]
+\moveribbons{c/{left=20},a/{right=18}}
+\\[-4]
+\com[finish ribbons={a,c,d}, start ribbons={
+ a/{label=\ls\,\x\,\t},
+ c/{label=\ls\,\t\,0},
+ d/{label=\t\dotneq 0}}]{t:=x}
+\\
+\jus[finish ribbons={c,d}, start ribbons={
+ c/{label=\exists U\ldotp\t\mapsto U * \ls\,U\,0}}]{Unfold $\ls$ def}
+\\
+\com[finish ribbons={c}, start ribbons={
+ b/{left=\RIBcLEFT,right=33,label=\t\mapsto\u},
+ c/{left=35,right=\RIBcRIGHT,label=\ls\,\u\,0}}]{u:=[t]}
+\\
+\startblock[fit ribbons={a},
+ extra left=2, extra right=2,start ribbons={
+ d/{label=\u\dotneq 0}}]{while (u!=0) \{}
+\\
+\jus[finish ribbons={a,b,c}, height=10, start ribbons={
+ a/{right=\RIBbRIGHT, label=\ls\,\x\,\u},
+ c/{label=\ls\,\u\,0}}]{Lemma: $\ls\,a\,b *\ls\,b\,c * \ls\,c\,0$ \\
+ implies $\ls\,a\,c * \ls\,c\,0$}
+\\
+\moveribbons{a/{right=18},c/{left=20}}
+\\[-3]
+\com[finish ribbons={a,c,d}, start ribbons={
+ a/{label=\ls\,\x\,\t},
+ c/{label=\ls\,\t\,0},
+ d/{label=\t\dotneq 0}}]{t:=u}
+\\
+\jus[finish ribbons={c,d}, start ribbons={
+ c/{label=\exists U\ldotp\t\mapsto U * \ls\,U\,0}}]{Unfold $\ls$ def}
+\\
+\com[finish ribbons={c}, start ribbons={
+ b/{left=\RIBcLEFT,right=33,label=\t\mapsto\u},
+ c/{left=35,right=\RIBcRIGHT,label=\ls\,\u\,0}}]{u:=[t]}
+\\
+\finishblock[start ribbons={
+ d/{label=\u\doteq 0}}]{\}}
+\\
+\jus[finish ribbons={b,c,d}, start ribbons={
+ b/{label=\t\mapsto0}}]{Unfold $\ls$ def}
+\\
+\com[finish ribbons={b}, start ribbons={
+ b/{label=\t\mapsto\y}}]{[t]:=y}
+\\
+\jus[finish ribbons={b,e}, start ribbons={
+ b/{right=\RIBeRIGHT,label=\ls\,\t\,0}}]{Fold $\ls$ def}
+\\
+\jus[finish ribbons={a,b}, start ribbons={
+ a/{right=\RIBeRIGHT,label=\ls\,\x\,0}}]{
+ Lemma: $\ls\,a\,b * \ls\,b\,0$ implies $\ls\,a\,0$}
+\\
+\finishblock{\}}
+\\
+\end{ribbonproof}
+\end{filecontents*}
+
+\begin{shaded}
+\lstinputlisting{temp_example_listappend.tex}
+\end{shaded}
+
+\begin{figure}
+\small\centering
+\include{temp_example_listappend}
+\caption{A ribbon proof of list append}
+\label{fig:listappend}
+\end{figure}
+
+\clearpage
+
+\subsection{List reverse}
+
+Figure~\ref{fig:listrev} shows the result.
+
+\begin{filecontents*}{temp_example_listrev.tex}
+\begin{ribbonproof}[start ribbons={
+ b/{left=20,right=38,label=\List\,\delta\,\x}}]
+\com[start ribbons={
+ c/{left=40,right=78,label=\List\,\varepsilon\,\y}}]{y:=0};
+\\
+\jus[finish ribbons={b,c}, start ribbons={
+ b/{label=\List\,\alpha\,\x},
+ d/{label=\delta\doteq \rev{\beta}\cdot\alpha,
+ left=60, right=\RIBcRIGHT},
+ c/{label=\List\,\beta\,\y, right=58}
+}, start boxes={
+ alpha/{left=14,right=82,label=\exists\alpha,color=SeaGreen},
+ beta/{left=18,right=80,label=\exists\beta,color=MediumPurple}
+}]{Choose $\alpha:=\delta$ and $\beta:=\varepsilon$}
+\\
+\startblock[fit boxes={alpha},
+ extra left=10, extra right=2, start ribbons={
+ a/{left=0,right=10,label=\x\dotneq 0}}]{while (x!=0) \{}
+\\
+\extendboxes{alpha/{left=-4}}
+\\[-9]
+\extendboxes{beta/{left=-8}}
+\\[-9]
+\jus[finish ribbons={a,b}, start ribbons={
+ ab/{left=\RIBaLEFT, right=\RIBbRIGHT,
+ label={\exists\alpha',i,Z\ldotp\x\mapsto i,Z \\
+ {} * \List\,\alpha'\,Z * \alpha \doteq i \cdot \alpha'}}
+ }]{Unfold $\List$ def}
+\\[-11]
+\moveribbons{
+ c/{left=70,right=\RIBdRIGHT},
+ d/{left=40,right=64}}
+\moveboxes{
+ alpha/{right=\RIBdRIGHT+4}}
+\\[4]
+\jus[finish boxes={alpha}, start boxes={
+ alpha/{label=\exists\alpha},
+ i/{left=\RIBabLEFT-2,right=\RIBdRIGHT+2,color=Chocolate,
+ vertical offset=10,label=\exists i}
+ }, finish ribbons={ab,d},
+ start ribbons={
+ ab/{label={\exists Z\ldotp\x \mapsto i,Z * \List\,\alpha\,Z}},
+ d/{label=\delta\doteq \rev{\beta}\cdot(i\cdot\alpha)}
+ }]{Choose $\alpha:=\alpha'$}
+\\
+\com[finish ribbons={ab}, start ribbons={
+ a/{right=14,label=\List\,\alpha\,\z},
+ b/{label={\x\mapsto i,\z}}
+}]{z:=[x+1]}
+\moveribbons{
+ c/{left=\RIBdLEFT, right=50},
+ d/{left=52, right=76}}
+\moveboxes{
+ alpha/{left=-8,right=82},
+ beta/{left=-6},
+ i/{right=78}}
+\\
+\com[finish ribbons={b}, start ribbons={
+ b/{label={\x\mapsto i,\y}}}]{[x+1]:=y}
+\jus[finish ribbons={d}, start ribbons={
+ d/{label={\delta\doteq \rev{(i\cdot\beta)}\cdot\alpha}}}]
+ {Reassociate $i$}
+\moveboxes{
+ i/{left=\RIBbLEFT-2},
+ beta/{left=\RIBbLEFT-4}}
+\\
+\jus[finish ribbons={b,c}, start ribbons={
+ c/{left=\RIBbLEFT,label={\List (i\cdot\beta)\,\x}}}]{Fold $\List$ def}
+\\
+\jus[finish ribbons={c,d}, finish boxes={i,beta},
+ start boxes={
+ beta/{vertical offset=10,horizontal offset=2,label=\exists\beta}},
+ start ribbons={
+ c/{label={\List\,\beta\,\x}},
+ d/{label={\delta\doteq\rev{\beta}\cdot\alpha}}}]
+ {Choose $\beta:=(i\cdot\beta)$}
+\\[-6]
+\moveribbons{
+ d/{left=60,right=78},
+ c/{left=40,right=58},
+ a/{left=20,right=38}}
+\moveboxes{
+ alpha/{left=14},
+ beta/{left=18}}
+\\
+\com[finish ribbons={c}, start ribbons={
+ c/{label={\List\,\beta\,\y}}}]{y:=x}
+\\[-5]
+\com[finish ribbons={a}, start ribbons={
+ b/{left=\RIBaLEFT,right=\RIBaRIGHT,label={\List\,\alpha\,\z}}}]{x:=z}
+\\
+\finishblock[start ribbons={
+ a/{left=0,right=10,label={\x\doteq 0}}
+}]{\}}
+\\
+\extendboxes{alpha/{left=-8}}
+\\[-9]
+\extendboxes{beta/{left=-6}}
+\\[-9]
+\jus[finish ribbons={a,b},start ribbons={
+ a/{right=\RIBbRIGHT,label=\alpha\doteq \varepsilon}}]
+ {Unfold $\List$ def}
+\swapribbons{c,d}{d,c}
+\\
+\jus[finish ribbons={a,d}, start ribbons={
+ a/{right=\RIBdRIGHT,label=\delta\doteq\rev{\beta}}}]
+ {Concatenate empty sequence}
+\\
+\jus[finish ribbons={a,c}, finish boxes={alpha,beta},
+ start ribbons={
+ a/{right=\RIBcRIGHT,label={\List\,\rev{\delta}\,\y}}}]
+ {Fold $\List$ def}
+\\
+\end{ribbonproof}
+\end{filecontents*}
+
+\begin{shaded}
+\lstinputlisting{temp_example_listrev.tex}
+\end{shaded}
+
+\begin{figure}
+\small\centering
+\include{temp_example_listrev}
+\caption{A ribbon proof of list reverse}
+\label{fig:listrev}
+\end{figure}
+
+\clearpage
+
+\subsection{List reverse with variables-as-resource}
+
+Figure~\ref{fig:listrevvar} shows the result.
+
+\begin{filecontents*}{temp_example_listrevvar.tex}
+\begin{ribbonproof}[start ribbons={
+ b/{left=20,right=38,var width=5,label=\x&\List\,\delta\,\x},
+ e/{left=40,right=45, var width=5,label=\z&},
+ c/{left=50,right=55, var width=5,label=\y&}}]
+\jus[finish ribbons={b}, start ribbons={
+ a/{left=-5,right=0,var width=5, label={\frac12\x&}},
+ b/{label={\frac12\x&\List\,\delta\,\x}}
+}]{Split $\x$}
+\com[finish ribbons={c},start ribbons={
+ c/{left=50,right=88,label=\y&\List\,\varepsilon\,\y}}]{y:=0};
+\\
+\jus[finish ribbons={b,c}, start ribbons={
+ b/{label=\frac12\x&\List\,\alpha\,\x},
+ c/{label=\y&\List\,\beta\,\y, right=68},
+ d/{label=\delta\doteq \rev{\beta}\cdot\alpha, left=70, right=88}
+}, start boxes={
+ alpha/{left=14,right=94,label=\exists\alpha,color=SeaGreen},
+ beta/{left=18,right=92,label=\exists\beta,color=MediumPurple}
+}]{Choose $\alpha:=\delta$ and $\beta:=\varepsilon$}
+\\
+\startblock[fit boxes={alpha},
+ extra left=10, extra right=2, finish ribbons={a}, start ribbons={
+ a/{left=-5,right=10,label=\frac12\x&\x\dotneq 0}}]{while (x!=0) \{}
+\\
+\extendboxes{alpha/{left=-9}}
+\\[-9]
+\extendboxes{beta/{left=-13}}
+\\[-9]
+\jus[finish ribbons={a,b}, start ribbons={
+ ab/{left=\RIBaLEFT, right=\RIBbRIGHT, var width=5,
+ label={\x&\exists\alpha',i,Z\ldotp\x\mapsto i,Z \\{}
+ * \List\,\alpha'\,Z * \alpha \doteq i \cdot \alpha'}}}]
+ {Unfold $\List$ def}
+\moveboxes{
+ alpha/{right=92},
+ beta/{right=94}}
+\\[4]
+\jus[finish boxes={alpha}, start boxes={
+ alpha/{label=\exists\alpha},
+ i/{left=-7,right=90,color=Chocolate,
+ vertical offset=11,label=\exists i}
+ }, finish ribbons={ab,d},
+ start ribbons={
+ ab/{label={\x&\exists Z\ldotp\x \mapsto i,Z * \List\,\alpha\,Z}},
+ d/{label=\delta\doteq{}\\\rev{\beta}\cdot(i\cdot\alpha)}
+ }]{Choose $\alpha:=\alpha'$}
+\\
+\com[finish ribbons={ab,e}, start ribbons={
+ a/{right=14,label=\frac12\z&\List\,\alpha\,\z},
+ b/{var width=10,left=16,right=\RIBeRIGHT,
+ label={\x,\frac12\z&\x\mapsto i,\z}}
+}]{z:=[x+1]}
+\jus[finish ribbons={c}, start ribbons={
+ cc/{left=57, right=\RIBcRIGHT, var width=5,
+ rotated label={\List\,\beta\,\y}, label=\frac12\y&},
+ c/{right=55, label=\frac12\y&}
+}]{Split $\y$}
+\moveboxes{alpha/{left=-11}}
+\\
+\com[finish ribbons={b,c}, start ribbons={
+ b/{var width=5,right=21, label=\frac12\z&},
+ c/{left=27, var width=10,label={\x,\frac12\y&\x\mapsto i,\y}}
+}]{[x+1]:=y}
+\jus[finish ribbons={d}, start ribbons={
+ d/{label={\delta\doteq {}\\
+ \rev{(i\cdot\beta)}\cdot\alpha}}}]{Reassoc. $i$}
+\\[-6]
+\moveboxes{
+ i/{left=25},
+ beta/{left=23,right=92},
+ alpha/{right=94}}
+\\
+\jus[finish ribbons={a,b}, start ribbons={
+ a/{right=\RIBbRIGHT, label={\z&\List\,\alpha\,\z}}
+}]{Combine $\z$}
+\jus[finish ribbons={c,cc}, start ribbons={
+ c/{var width=5, label={\x&\List (i\cdot\beta)\,\x}},
+ cc/{left=63, label=\y&}
+}]{Fold $\List$ def}
+\\
+\jus[finish ribbons={c,d}, finish boxes={i,beta},
+ start boxes={
+ beta/{horizontal offset=2,label=\exists\beta}},
+ start ribbons={
+ c/{label={\List\,\beta\,\x}, label=\x&},
+ d/{label={\delta\doteq\rev{\beta}\cdot\alpha}}}]
+ {Choose $\beta:=(i\cdot\beta)$}
+\\[-2]
+\moveribbons{c/{left=43,right=61},a/{left=20,right=41}}
+\moveboxes{alpha/{left=14},beta/{left=18}}
+\\
+\com[finish ribbons={c,cc}, start ribbons={
+ e/{left=\RIBcLEFT, right=\RIBcLEFT+5, label=\x&},
+ c/{left=50,right=\RIBccRIGHT,label={\List\,\beta\,\y}, label=\y&}
+}]{y:=x}
+\\
+\com[finish ribbons={a,e}, start ribbons={
+ b/{left=\RIBaLEFT,right=\RIBaRIGHT,label=\frac12\x&\List\,\alpha\,\x},
+ a/{left=-5,right=0,var width=5, label=\frac12\x&},
+ e/{label=\z&}
+}]{x:=z}
+\\
+\finishblock[finish ribbons={a},start ribbons={
+ a/{right=10,label={\frac12\x&\x\doteq 0}}
+}]{\}}
+\\
+\extendboxes{alpha/{left=-8}}
+\\[-9]
+\extendboxes{beta/{left=-6}}
+\\[-9]
+\jus[finish ribbons={a,b},start ribbons={
+ a/{right=0, label=\x&},
+ b/{left=6,label=\alpha\doteq \varepsilon, var width=0}
+}]{Unfold $\List$ def}
+\moveribbons{
+ d/{left=43,right=48},
+ e/{left=74,right=79}
+}
+\moveboxes{
+ alpha/{right=72},
+ beta/{right=70}}
+\\
+\jus[finish ribbons={b,d}, start ribbons={
+ b/{right=\RIBdRIGHT,label=\delta\doteq\rev{\beta}}
+ }]{Concatenate empty seq.}
+\moveboxes{alpha/{left=2},beta/{left=4}}
+\\
+\jus[finish ribbons={b,c}, finish boxes={alpha,beta},
+ start ribbons={
+ c/{label={\y&\List\,\rev{\delta}\,\y}}
+ }]{Fold $\List$ def}
+\\
+\end{ribbonproof}
+\end{filecontents*}
+
+\begin{shaded}
+\lstinputlisting{temp_example_listrevvar.tex}
+\end{shaded}
+
+\begin{figure}
+\small\centering
+\include{temp_example_listrevvar}
+\caption{A ribbon proof of list reverse using variables-as-resource}
+\label{fig:listrevvar}
+\end{figure}
+
+\clearpage
+
+\section{Reference}
+
+\subsection{Package dependencies}
+
+The \packagename{ribbonproofs} package relies upon:
+\begin{itemize}
+\item the \packagename{xcolor} package for colours;
+\item the \packagename{tikz} package, plus its \packagename{calc}, \packagename{fadings} and \packagename{decorations.pathmorphing} libraries, for drawing;
+\item the \packagename{xstring} package for manipulating strings; and
+\item the \packagename{etextools} package for controlling the expansion of \TeX{} macros.
+\end{itemize}
+
+\subsection{The \Ienv{ribbonproof} environment}
+
+\begin{description}
+\item "\begin{"\Ienv{ribbonproof}"}["$\mathit{options}$"]"$\ldots$"\end{"\Ienv{ribbonproof}"}" \quad This is the top-level environment for creating ribbon proofs. All of the ribbon-proof-building commands must only appear inside this environment. Available $\mathit{options}$ are:
+\begin{description}
+\item \Ikey{extra height}"="$n$ \quad (Default: "0".) This option adds $n$ (an integer or integer expression) to the height of the diagram's first row.
+\item \label{key:startribbons}\Ikey{start ribbons}"={"$\mathit{ribbon}_1$","$\dots$","$\mathit{ribbon}_k$"}" \quad (Default: empty.) This defines a list of $k$ ribbons that henceforth are \emph{active}. Each $\mathit{ribbon}$ takes the form
+\begin{center}
+$\mathit{name}$"/{"$\mathit{options}$"}"
+\end{center}
+where $\mathit{name}$ is an alphabetic string that henceforth identifies the ribbon, and the available $\mathit{options}$ are:
+\begin{description}
+\item \Ikey{left}"="$n$ \quad (Default: "0".) The horizontal position of the ribbon's left edge. The value $n$ must either be an integer or an expression that evaluates to an integer. The value is stored in the command \Icom{RIBnameLEFT}, where "name" is the name of the ribbon. Ribbons can be positioned relatively by accessing this value. For instance, one can write "left=\RIBfooLEFT-20" to set the current ribbon's left edge 20 units to the left of the ribbon "foo"'s left edge.
+\item \Ikey{right}"="$n$ \quad (Default: "0".) The horizontal position of the ribbon's right edge. The value $n$ must either be an integer or an expression that evaluates to an integer. The value is stored in the command \Icom{RIBnameRIGHT}, where "name" is the name of the ribbon.
+\item \Ikey{label}"="$L$ \quad (Default: empty.) The label that is written on the ribbon. The value $L$ is parsed in math-mode. There are three complications here.
+\begin{itemize}
+\item If $L$ contains a comma or an equals-sign, it must be wrapped in braces; otherwise the parser will become confused. For instance, be sure to write "label={0<x,y<5}" instead of "label=0<x,y<5".
+\item \varsasresonly{} If $L$ contains an \texttt{\color{\COLORcom}\&} symbol\index{\& command@\texttt{\color{\COLORcom}\&} command}, the portion to the left of the \texttt{\color{\COLORcom}\&} is used as the `variable resource'. Here are some examples.
+
+\begin{tkzexample}[]
+\begin{ribbonproof}[start ribbons={
+ a/{left=5, right=25, var width=5,
+ label=\texttt{x}&\texttt{x}<a},
+ b/{left=30, right=35, var width=5,
+ label=\texttt{y}&},
+ c/{left=40, right=80,
+ label={\exists b\ldotp a=2^b \wedge b<9}}}]
+\end{ribbonproof}
+\end{tkzexample}
+
+\end{itemize}
+\item \Ikey{var width}"="$n$ \quad (Default: "0".) \varsasresonly{} The width of the `variable resource' part of the ribbon. The value is stored in the command \Icom{RIBnameVARWIDTH}, where "name" is the name of the ribbon. In the case where
+\[
+n=\mbox{\Ikey{right}}-\mbox{\Ikey{left}}
+\]
+the ribbon consists \emph{only} of the variable resource.
+\item \Ikey{rotated label}"="$L$ \quad (Default: empty.) If this key is given instead of \Ikey{label}, the label $L$ will be rotated through 90$^\circ$ clockwise. This feature can be handy when horizontal space is tight.\par \varsasresonly{} It is not currently possible to rotate the variable resource. However, a variable resource can be coupled with a rotated assertion by using both the \Ikey{label} and the \Ikey{rotated label} keys in the following manner.
+
+\begin{tkzexample}[latex=2cm]
+\begin{ribbonproof}[extra height=9, start ribbons={
+ a/{left=0, right=11, var width=5,
+ label=\texttt{x}&, rotated label=\texttt{x}<a}}]
+\end{ribbonproof}
+\end{tkzexample}
+\end{description}
+
+\item \label{key:startboxes}\Ikey{start boxes}"={"$\mathit{box}_1$","$\dots$","$\mathit{box}_k$"}" \quad (Default: empty.) This defines a list of $k$ boxes that henceforth are \emph{active}. Each $\mathit{box}$ takes the form
+\begin{center}
+$\mathit{name}$"/{"$\mathit{options}$"}"
+\end{center}
+where $\mathit{name}$ is an alphabetic string that henceforth identifies the box, and the available $\mathit{options}$ are:
+\begin{description}
+\item \Ikey{left}"="$n$ \quad (Default: "0".) The horizontal position of the box's left edge. The value $n$ must either be an integer or an expression that evaluates to an integer. The value is stored in the command \Icom{BOXnameLEFT}, where "name" is the name of the box. Boxes can be positioned relatively by accessing this value. For instance, one can write "left=\BOXfooLEFT-5" to set the current box's left edge 5 units to the left of the box "foo"'s left edge.
+\item \Ikey{right}"="$n$ \quad (Default: "0".) The horizontal position of the box's right edge. The value $n$ must either be an integer or an expression that evaluates to an integer. The value is stored in the command \Icom{BOXnameRIGHT}, where "name" is the name of the box.
+\item \Ikey{label}"="$L$ \quad (Default: empty.) The label that is written on the left edge of the box. The value $L$ is parsed in math-mode.
+\item \Ikey{vertical offset}"="$n$ \quad (Default: "0".) Shifts the \Ikey{label} down by $n$. The value $n$ must either be an integer or an expression that evaluates to an integer.
+\item \Ikey{horizontal offset}"="$n$ \quad (Default: "0".) Shifts the \Ikey{label} to the right by $n$. The value $n$ must either be an integer or an expression that evaluates to an integer.
+\item \Ikey{color}"="$c$ \quad (Default: "black".) The colour of the box's lines and of its label text.
+\end{description}
+
+\item \Ikey{draw grid} \quad (Default: "false".) This option lays a grid underneath the picture. The grid looks like graph paper, and is intended to help the user to position the elements of the diagram. As additional visual aids, it places little `ticks' down the left-hand edge of the diagram, to show where each row begins, and it labels each ribbon with its name. Here is an example.
+\begin{tkzexample}[]
+\begin{ribbonproof}[draw grid,
+ start ribbons={
+ a/{left=0, right=10, label=A},
+ b/{left=12, right=22, label=B},
+ c/{left=24, right=34, label=C}}]
+\com[finish ribbons={a,b},
+ start ribbons={
+ a/{label=D},
+ b/{label=E}}]{foo} \\
+\com[finish ribbons={b,c},
+ start ribbons={
+ b/{label=F},
+ c/{label=G}}]{baz} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\end{description}
+
+\end{description}
+
+\subsection{Commands}
+
+\begin{description}
+\item \label{command:com}\Icom{com}"["$\mathit{options}$"]{"$\mathit{text}$"}"\quad This creates a `command step' in the proof; that is, a black rectangle, labelled with $\mathit{text}$, that has a precondition above it and a postcondition below it. Available $\mathit{options}$ are:
+\begin{description}
+\item \Ikey{height}"="$n$ \quad (Default: {\tt\color{\COLORdef}\defaultStepHeight}.) The height of the step. The value $n$ must either be an integer or an expression that evaluates to an integer. The default step height can be changed by renewing the \Icom{defaultStepHeight} command.
+\item \Ikey{finish ribbons}"={"$\mathit{name}_1$","$\ldots$","$\mathit{name}_k$"}" \quad (Default: empty.) The $k$ ribbons that are identified by the given names are preconditions of this step. They are henceforth \emph{inactive}.
+\item \Ikey{start ribbons}\quad These ribbons are postconditions of this step. See page~\pageref{key:startribbons}.
+\item \Ikey{finish boxes}"={"$\mathit{name}_1$","$\ldots$","$\mathit{name}_k$"}" \quad (Default: empty.) The $k$ boxes that are identified by the given names are preconditions of this step. They are henceforth \emph{inactive}.
+\item \Ikey{start boxes}\quad These boxes are postconditions of this step. See page~\pageref{key:startboxes}.
+\item \Ikey{extra left}"="$n$ \quad (Default: "0".) Additional width on the lefthand side of the step. The value $n$ must either be an integer or an expression that evaluates to an integer.
+\item \Ikey{extra right}"="$n$ \quad (Default: "0".) Additional width on the righthand side of the step. The value $n$ must either be an integer or an expression that evaluates to an integer.
+\end{description}
+\item \Icom{jus}"["$\mathit{options}$"]{"$\mathit{text}$"}"\quad This creates a `justification step' in the proof; that is, a grey rectangle, labelled with $\mathit{text}$. The \Icom{jus} command has exactly the same behaviour and options as the \Icom{com} command, except:
+\begin{itemize}
+\item \Icom{com} creates a black rectangle, but \Icom{jus} creates a grey rectangle;
+\item \Icom{com} sets its label in a monospaced font, but \Icom{jus} sets its in a roman font; and
+\item \Icom{com} sets its label on the left of the rectangle, but \Icom{jus} sets its at the centre.
+\end{itemize}
+
+\item\Icom{\textbackslash}"["$\mathit{height}$"]"\quad Finish the current row and move to the next. The optional $\mathit{height}$ parameter (a positive or negative integer, default "0") is added to the default row height of \texttt{\color{\COLORdef}{\defaultRowHeight}}. This default row height can be changed by renewing the command \Icom{defaultRowHeight}.
+
+\item\Icom{ribbonpagebreak}\quad Insert a page break at this point in the proof. When the proof restarts on the next page, the labels on the ribbons and boxes are printed again, so that the reader does not have to turn back several pages to find the label of a long-lived ribbon or box. Here is an example. (The page break appears as a horizontal gap in the middle of the proof.)
+\begin{tkzexample}[latex=50mm]
+\begin{ribbonproof}[start ribbons={
+ a/{left=0, right=18, label=P},
+ b/{left=20, right=38, label=R}}, start boxes={
+ x/{left=-2, right=40, label=\exists x}}]
+\com[finish ribbons={a}, start ribbons={
+ a/{label=Q}}]{c()} \\
+\ribbonpagebreak
+\jus[finish ribbons={b}, start ribbons={
+ b/{label=S}}]{Lemma 1} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\item \Icom{startblock}"["$\mathit{options}$"]{"$\mathit{text}$"}"\quad This command begins a new \emph{block}\index{block}. A block is a compound step such as an if-statement or a while-loop. Blocks may contain further steps, or even further blocks (which must be well-nested). The first segment of a block resembles an ordinary command step -- a black rectangle labelled with $\mathit{text}$ -- except that its top corners are rounded. (The radius of this rounding defaults to {\tt\color{\COLORdef}{\roundingRadius}}, but this can be adjusted by renewing the \Icom{roundingRadius} command.) Available $\mathit{options}$ are \Ikey{height}, \Ikey{finish ribbons}, \Ikey{start ribbons}, \Ikey{finish boxes}, \Ikey{start boxes}, \Ikey{extra left} and \Ikey{extra right}, which are the same as for the \Icom{com} command: see page~\pageref{command:com}. Additional options are:
+\begin{description}
+\item \Ikey{fit ribbons}"={"$\mathit{name}_1$","$\ldots$","$\mathit{name}_k$"}" \quad (Default: empty.) The $k$ ribbons that are identified by the given names are used to calculate the width of this block. That is, the block must be wide enough to cover each of the named ribbons. Being one of the \Ikey{fit ribbons} does not affect a ribbon in any way.
+\item \Ikey{fit boxes}"={"$\mathit{name}_1$","$\ldots$","$\mathit{name}_k$"}" \quad (Default: empty.) As above, but for boxes instead of ribbons.
+\end{description}
+
+\item \Icom{continueblock}"["$\mathit{options}$"]{"$\mathit{text}$"}"\quad This command continues a block. If the block represents an if-statement, then \Icom{continueblock} corresponds to the `else' keyword. Every ribbon or box inside the block is deactivated, and every ribbon or box that was inside the block at the beginning is restored. This segment of a block resembles an ordinary command step -- a black rectangle labelled with $\mathit{text}$ -- except that its top edge is jagged (when the \Ikey{jagged} key is provided) to suggest the discontinuity at this point in the proof. Here is an example.
+\begin{tkzexample}[latex=65mm]
+\begin{ribbonproof}[extra height=2,
+ start ribbons={
+ q/{left=12, right=26, label=q},
+ r/{left=30, right=45, label=r}},
+ start boxes={
+ x/{left=10, right=28, color=red,
+ label=\exists x}}]
+\startblock[fit ribbons={r},
+ extra left=5, extra right=2,
+ start ribbons={p/{left=-10, right=5,
+ label=b}}]{if ($b$) \{} \\
+\com[finish ribbons={p,q,r},
+ finish boxes={x}, start ribbons={
+ s/{left=-8, right=21, label=s},
+ t/{left=25, right=45, label=t}},
+ start boxes={
+ y/{left=-10, right=23, color=green,
+ label=\exists y}}]{foo()} \\[2]
+\continueblock[jagged, repeat labels,
+ start ribbons={
+ p/{label=\neg b}}]{\} else \{} \\
+\end{ribbonproof}
+\end{tkzexample}
+Available $\mathit{options}$ are \Ikey{height}, \Ikey{start ribbons} and \Ikey{start boxes}, which are the same as for the \Icom{com} command: see page~\pageref{command:com}. Additional options are:
+\begin{description}
+\item \Ikey{jagged}\quad (Default: none.) If this option is present, the top edge of the rectangle is drawn jagged. If not, the top edge is simply a straight line.
+\item \Ikey{repeat labels}\quad (Default: none.) If this option is present, each ribbon or box that is restored by this step is given a new label, as a reminder to the reader. If not, labels are only printed for those ribbons and boxes given explicitly in the \Ikey{start ribbons} or \Ikey{start boxes} lists.
+\end{description}
+
+\item \Icom{finishblock}"["$\mathit{options}$"]{"$\mathit{text}$"}"\quad This command ends a block. If the block represents an if-statement or a while-loop, then \Icom{finishblock} corresponds to the `end-if' or `end-while' keyword. Unlike the \Icom{continueblock} command, \Icom{finishblock} does not deactivate or restore any ribbons or boxes -- except for those explicitly given as \Ikey{start ribbons}, \Ikey{start boxes}, \Ikey{finish ribbons} or \Ikey{finish boxes}. This final segment of a block resembles an ordinary command step -- a black rectangle labelled with $\mathit{text}$ -- except that its bottom corners are rounded. (The radius of this rounding defaults to {\tt\color{\COLORdef}{\roundingRadius}}, but this can be adjusted by renewing the \Icom{roundingRadius} command.) Available $\mathit{options}$ are \Ikey{height}, \Ikey{finish ribbons}, \Ikey{start ribbons}, \Ikey{finish boxes}, \Ikey{start boxes}, \Ikey{extra left} and \Ikey{extra right}, which are the same as for the \Icom{com} command: see page~\pageref{command:com}.
+\item \Icom{moveribbons}"{"$\mathit{ribbon}_1$","$\dots$","$\mathit{ribbon}_k$"}" \quad This command provides an opportunity to redefine the \Ikey{left} and \Ikey{right} positions of some ribbons. Each $\mathit{ribbon}$ takes the form
+\begin{center}
+$\mathit{name}$"/{"$\mathit{options}$"}"
+\end{center}
+where $\mathit{name}$ identifies an active ribbon, and the available $\mathit{options}$ are \Ikey{left}, \Ikey{right} and \Ikey{var width}, which are the same as for the \Ikey{start ribbons} key: see page~\pageref{key:startribbons}. At the end of the current row, the left and right positions of each ribbon are examined, and if they have changed as the result of a \Icom{moveribbons} command, a smooth shape is drawn from the old positions to the new positions. A thin white border is added to the left and right edges of each ribbon, to make overlapping ribbons easier to distinguish. The z-order of ribbons can be controlled by their position in the list provided to \Icom{moveribbons}: later ribbons are drawn over earlier ones. Here is an example that illustrates how to control overlapping in this way.
+\begin{tkzexample}[latex=22mm]
+\begin{ribbonproof}[start ribbons={
+ p/{left=0, right=8, label=p},
+ q/{left=10, right=18, label=q}}]
+\moveribbons{p/{left=10, right=18}, q/{left=0, right=8}} \\
+\moveribbons{q/{left=10, right=18}, p/{left=0, right=8}} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\item \Icom{swapribbons}"{"$\mathit{name}_1$","$\dots$","$\mathit{name}_k$"}{"$\mathit{name}'_1$","$\dots$","$\mathit{name}'_k$"}" \quad This command provides an easy way to permute a number of ribbons. Each $\mathit{name}$ identifies an active ribbon. The second argument is expected to contain the same names as the first, but perhaps in a different order. The effect is that for each $i$ simultaneously, the new position of the ribbon identified by $\mathit{name}'_i$ becomes the old position of the ribbon identified by $\mathit{name}_i$. The z-order of ribbons can be controlled by their position in the second argument: later ribbons are drawn over earlier ones. Here is an example that illustrates how to control overlapping in this way.
+
+\begin{tkzexample}[latex=22mm]
+\begin{ribbonproof}[start ribbons={
+ p/{left=0, right=8, label=p},
+ q/{left=10, right=18, label=q}}]
+\swapribbons{q,p}{p,q} \\
+\swapribbons{p,q}{q,p} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\item \Icom{moveboxes}"{"$\mathit{box}_1$","$\dots$","$\mathit{box}_k$"}" \quad This command provides an opportunity to redefine the \Ikey{left} and \Ikey{right} positions of some boxes. Each $\mathit{box}$ takes the form
+\begin{center}
+$\mathit{name}$"/{"$\mathit{options}$"}"
+\end{center}
+where $\mathit{name}$ identifies an active box, and the available $\mathit{options}$ are \Ikey{left} and \Ikey{right}, which are the same as for the \Ikey{start boxes} key: see page~\pageref{key:startboxes}. At the end of the current row, the left and right positions of each box are examined, and if they have changed as the result of a \Icom{moveboxes} command, a smooth shape is drawn from the old positions to the new positions. Here is an example.
+\begin{tkzexample}[latex=27mm]
+\begin{ribbonproof}[start ribbons={
+ p/{left=7, right=15, label=p}},
+ start boxes={
+ x/{left=5, right=17, color=red, label=\exists x},
+ y/{left=0, right=19, color=green, label=\exists y}}]
+\moveboxes{x/{left=3, right=19}, y/{left=5, right=17}} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\item \Icom{extendboxes}"{"$\mathit{box}_1$","$\dots$","$\mathit{box}_k$"}" \quad This command accepts the same input as the \Icom{moveboxes} command. Like the \Icom{moveboxes} command, it allows the \Ikey{left} and \Ikey{right} positions of some boxes to be modified. However, where \Icom{moveboxes} draws smooth curves between the old and new positions, \Icom{extendboxes} draws a `rounded elbow' shape between them. Such a shape may be preferable when extending a box's scope to include additional ribbons. Here is an example.
+\begin{tkzexample}[latex=32mm]
+\begin{ribbonproof}[start ribbons={
+ p/{left=8, right=16, label=p},
+ q/{left=20, right=28, label=q}},
+ start boxes={
+ x/{left=6, right=18, color=red, label=\exists x}}]
+\extendboxes{x/{right=30}} \\
+\end{ribbonproof}
+\end{tkzexample}
+
+\end{description}
+
+\subsection{Adjustable parameters}
+
+Each of the following parameters can be adjusted using \texttt{\color{\COLORcom}\textbackslash renewcommand}.
+
+\newcommand\explainCom[1]{%
+\item \Icom{#1}\quad
+(Default: \texttt{\color{\COLORdef}\csname #1\endcsname}).}
+
+\begin{description}
+
+\explainCom{jusColor} Background colour of `justification' steps.
+\explainCom{comColor} Background colour of `command' steps.
+\explainCom{ribColor} Background colour of ribbons.
+\explainCom{varribColor} \varsasresonly{} Background colour of the variable resource part of a ribbon.
+\explainCom{ribTextColor} Colour of the textual label on ribbons.
+\explainCom{boxTextColor} Colour of the textual label on boxes.
+\explainCom{guideTextColor} Colour of the `guide' text (that is, the ribbon names and numeric scale that are printed when \Ikey{draw grid} is enabled).
+\explainCom{defaultStepHeight} Default height of a step.
+\explainCom{defaultRowHeight} Default height of a row. (This includes the step.)
+\explainCom{ribTextVOffset} Vertical adjustment of ribbon labels.
+\explainCom{boxTextVOffset} Vertical adjustment of box labels.
+\explainCom{boxTextHOffset} Horizontal adjustment of box labels.
+\explainCom{guideTextVOffset} Vertical adjustment of guide text on ribbons.
+\explainCom{roundingRadius} Radius of rounded corners on blocks.
+\explainCom{boxRoundingRadius} Radius of rounded corners on boxes. (Applies to the \Icom{extendboxes} command.)
+\explainCom{blockLineWidth} Width of the vertical lines that delimit blocks.
+\explainCom{boxLineWidth} Width of the vertical lines that delimit boxes.
+\explainCom{shadowHeight} Height of the shadows that are drawn when a
+step passes over a ribbon without affecting it.
+\explainCom{shadowColor} Colour of the darkest point of a shadow.
+\explainCom{zigzagHeight} Height of the jagged edges that are drawn on the top edge of each \Icom{continueblock} step.
+\explainCom{zigzagLength} Length of the jagged edges that are drawn on the top edge of each \Icom{continueblock} step.
+\explainCom{twistiness} For adjusting the shape of the ribbon twists. Must be a value between 0 and 1 inclusive.
+
+\end{description}
+
+
+\bibliographystyle{abbrv}
+\bibliography{../../John}
+
+\printindex
+
+\end{document} \ No newline at end of file