%%
%% This is file `cascade.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% cascade.dtx  (with options: `package')
%% 
%% Copyright (C) 2018 by F. Pantigny
%% 
%% This file 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.
%% 
\def\myfileversion{1.1}
\def\myfiledate{2020/07/05}
\RequirePackage{l3keys2e}
\ProvidesExplPackage
  {cascade}
  {\myfiledate}
  {\myfileversion}
  {Easy presentation of demonstrations in cascades}
\RequirePackage{xparse}
%%
%% The following macro is the same as the macro of amsmath
\cs_if_free:NT \spread@equation
  {
    \cs_set_protected:Npn \spread@equation
      {
        \openup \jot
        \cs_set_protected:Npn \spread@equation { }
      }
  }
%%
%% The following registers will be used for the options.
\dim_new:N \l__cascade_interline_dim
\dim_new:N \l__cascade_interline_all_dim
\dim_new:N \l__cascade_space_between_dim
\dim_set:Nn \l__cascade_space_between_dim { 0.5 em }
\bool_new:N \l__cascade_t_bool
\bool_new:N \l__cascade_main_command_bool
\bool_new:N \l__cascade_nested_command_bool
\bool_new:N \l__cascade_first_argument_bool
%%
%% This set of options will be used by \Cascade and \ShortCascade
\keys_define:nn { cascade / command }
  {
    t .code:n =
      \bool_if:NTF \l__cascade_t_bool
        { \msg_error:nn { cascade } { t~option~already~set } }
        { \bool_set_true:N \l__cascade_t_bool } ,
    t .value_forbidden:n = true ,
    interline .dim_set:N = \l__cascade_interline_dim,
    interline .value_required:n = true ,
    interline-all .code:n =
     {
       \dim_set:Nn \l__cascade_interline_all_dim { #1 }
       \dim_set:Nn \l__cascade_interline_dim { #1 }
     } ,
    interline-all .value_required:n = true ,
    space-between .dim_set:N = \l__cascade_space_between_dim ,
    space-between .value_required:n = true
  }
%%
%% This set of options will be used by \CascadeOptions
\keys_define:nn { cascade / global }
  {
    interline-all .dim_set:N = \l__cascade_interline_all_dim ,
    interline-all .value_required:n = true ,
    space-between .dim_set:N = \l__cascade_space_between_dim ,
    space-between .value_required:n = true
  }
\NewDocumentCommand \CascadeOptions { m }
  { \keys_set:nn { cascade / global } { #1 } }
%%
%% The command \Cascade is defined with \NewDocumentCommand of xparse
\NewDocumentCommand \Cascade { O { } m m m m D < > { } }
  {
    \if_mode_math:
      \msg_error:nn { cascade } { Cascade~in~math~mode }
    \fi:
    \mode_leave_vertical:
    \bool_if:NF \l__cascade_nested_command_bool
      {
        \dim_gzero_new:N \g__cascade_yoffset_dim
        \bool_set_true:N \l__cascade_first_argument_bool
      }
    \group_begin:

    \spread@equation
    \dim_set_eq:NN \l__cascade_interline_dim \l__cascade_interline_all_dim
    \keys_set:nn { cascade / command } { #1 }
    \box_clear_new:N \l__cascade_box_one
    \box_clear_new:N \l__cascade_box_two
    \box_clear_new:N \l__cascade_box_three
    \box_clear_new:N \l__cascade_box_four
    \hbox_set:Nn \l__cascade_box_one
      {
        \bool_set_true:N \l__cascade_first_argument_bool
        \bool_set_true:N \l__cascade_nested_command_bool
        #2
      }
    \hbox_set:Nn \l__cascade_box_two { #3 }
    \hbox_set:Nn \l__cascade_box_three
      {
        \bool_set_false:N \l__cascade_first_argument_bool
        \bool_set_true:N \l__cascade_nested_command_bool
        #4
      }
    \hbox_set:Nn \l__cascade_box_four { #5 }
    \dim_zero_new:N \l__cascade_top_dim
    \dim_set:Nn \l__cascade_top_dim
      {
        \dim_max:nn
          \c_zero_dim
          { \box_ht:N \l__cascade_box_one - \box_ht:N \l__cascade_box_two }
      }
    \dim_zero_new:N \l__cascade_bottom_dim
    \dim_set:Nn \l__cascade_bottom_dim
      {
        \dim_max:nn
          \c_zero_dim
          { \box_dp:N \l__cascade_box_three - \box_dp:N \l__cascade_box_four }
      }
    \box_set_ht:Nn \l__cascade_box_one \c_zero_dim
    \box_set_dp:Nn \l__cascade_box_three \c_zero_dim
    \vbox_set:Nn \l_tmpa_box
      {
        \skip_vertical:N \l__cascade_top_dim
        \vbox_top:n
          {
            \__cascade_the_vcenter:nn { #2 } { #4 }
            \bool_if:NT \l__cascade_first_argument_bool
              {
                \dim_set:Nn \l_tmpa_dim
                  { \box_ht:N \l_tmpb_box + \box_dp:N \l_tmpb_box }
                \l_tmpa_dim = 0.5\l_tmpa_dim
                \dim_add:Nn \l_tmpa_dim { \the \fontdimen 22 \textfont2 }
                \dim_sub:Nn \l_tmpa_dim
                  { \dim_max:nn { \box_ht:N \l__cascade_box_two } { \box_ht:N \strutbox } }
                \dim_gadd:Nn \g__cascade_yoffset_dim \l_tmpa_dim
              }
            \hbox:n
              {
                \c_math_toggle_token
                \left .
                \box_use_drop:N \l_tmpb_box
                \right \}
                \c_math_toggle_token
                \bool_if:NT \l__cascade_t_bool
                  {
                     \bool_if:NF \l__cascade_nested_command_bool
                       {
                         \tl_if_empty:nF { #6 }
                           {
                             \skip_horizontal:n \l__cascade_space_between_dim
                             #6
                           }
                       }
                  }
              }
            \skip_vertical:N \l__cascade_bottom_dim
          }
      }
    \bool_if:NTF \l__cascade_nested_command_bool
      { \box_use_drop:N \l_tmpa_box }
      {
        \bool_if:NTF \l__cascade_t_bool
          { \box_move_down:nn \g__cascade_yoffset_dim { \box_use:N \l_tmpa_box } }
          { \box_use_drop:N \l_tmpa_box }
      }
    \group_end:
  }
\cs_new_protected:Npn \__cascade_the_vcenter:nn #1 #2
  {
    \hbox_set:Nn \l_tmpb_box
      {
        \c_math_toggle_token
        \vcenter
          {
            \halign
              {
                \hfil ## \cr
                \hbox:n
                  {
                    \tl_if_empty:nF { #1 }
                      {
                        \box_use_drop:N \l__cascade_box_one
                        \skip_horizontal:n \l__cascade_space_between_dim
                      }
                    \box_use:N \l__cascade_box_two
                    \strut
                  }
                \cr
                \noalign { \skip_vertical:n \l__cascade_interline_dim }
                \hbox:n
                  {
                    \tl_if_empty:nF { #2 }
                      {
                        \box_use_drop:N \l__cascade_box_three
                        \skip_horizontal:n \l__cascade_space_between_dim
                      }
                    \box_use_drop:N \l__cascade_box_four
                    \strut
                  }
                \cr
              }
          }
        \c_math_toggle_token
      }
  }
\msg_new:nnn
  { cascade }
  { Cascade~in~math~mode }
  {
    The~commands~\token_to_str:N \Cascade\
    and~\token_to_str:N \ShortCascade\
    should~be~used~in~text~mode~only.~However,~you~can~
    go~on~for~this~time.
  }
\msg_new:nnn
  { cascade }
  { t~option~already~set }
  {
    You~can't~use~the~key~'t'~here~because~it~has~been~set~
    in~an~encompassing~command.~If~you~go~on,~this~key~will~be~
    ignored.
  }
\NewDocumentCommand \ShortCascade { O { } m m }
  { \Cascade [ #1 ] { } { #2 } { } { #3 } }

\endinput
%%
%% End of file `cascade.sty'.
