\documentclass[12pt]{cdblatex}
\usepackage{examples}
\usepackage{fancyhdr}
\usepackage{footer}

\begin{document}

% ============================================================================================
\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   import cdblib
   checkpoint_file = 'tests/semantic/output/example-09.json'
   cdblib.create (checkpoint_file)
   checkpoint = []
\end{cadabra}
\egroup

\clearpage

% ============================================================================================
\section*{Example 9 The Gauss equation}

\begin{cadabra}
   {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u#}::Indices(position=independent).

   \nabla{#}::Derivative.

   K_{a b}::Symmetric.
   g^{a}_{b}::KroneckerDelta.

   # define the projection operator

   hab:=h^{a}_{b} -> g^{a}_{b} - n^{a} n_{b}.

   # 3-covariant derivative obtained by projection on 4-covariant derivative

   vpq:=v_{p q} -> h^{a}_{p} h^{b}_{q} \nabla_{b}{v_{a}}.

   # compute 3-curvature by commutation of covariant derivatives

   vpqr:= h^{a}_{p} h^{b}_{q} h^{c}_{r} ( \nabla_{c}{v_{a b}} - \nabla_{b}{v_{a c}} ).

   substitute (vpq,hab)
   substitute (vpqr,vpq)

   distribute   (vpqr)
   product_rule (vpqr)
   distribute   (vpqr)
   eliminate_kronecker (vpqr)

   # standard substitutions

   substitute (vpqr,$h^{a}_{b} n^{b} -> 0$)
   substitute (vpqr,$h^{a}_{b} n_{a} -> 0$)
   substitute (vpqr,$\nabla_{a}{g^{b}_{c}} -> 0$)
   substitute (vpqr,$n^{a} \nabla_{b}{v_{a}} -> -v_{a} \nabla_{b}{n^{a}}$)
   substitute (vpqr,$v_{a} \nabla_{b}{n^{a}} -> v_{p} h^{p}_{a}\nabla_{b}{n^{a}}$)
   substitute (vpqr,$h^{p}_{a} h^{q}_{b} \nabla_{p}{n_{q}} -> K_{a b}$)
   substitute (vpqr,$h^{p}_{a} h^{q}_{b} \nabla_{p}{n^{b}} -> K_{a}^{q}$)  # cdb(ex-09.095,vpqr)

   # tidy up

   {v_{a},h^{a}_{b},K_{a}^{b},K_{a b},R^{a}_{b c d},\nabla_{a}{v_{b}}}::SortOrder.

   sort_product   (vpqr)                                     # cdb(ex-09.096,vpqr)
   rename_dummies (vpqr)                                     # cdb(ex-09.097,vpqr)
   canonicalise   (vpqr)                                     # cdb(ex-09.098,vpqr)
   factor_out     (vpqr,$h^{a?}_{b?}$)                       # cdb(ex-09.099,vpqr)
   factor_out     (vpqr,$v_{a?}$)                            # cdb(ex-09.101,vpqr)

   checkpoint.append (vpqr)
\end{cadabra}

\begin{dgroup*}[spread={3pt}]
   \Dmath*{( D_{r}D_{q} - D_{q}D_{r} ) v_{p} = \Cdb*[\hskip 1.0cm\hfill]{ex-09.095}
                                             = \Cdb*[\hskip 1.0cm\hfill]{ex-09.096}
                                             = \Cdb*[\hskip 1.0cm\hfill]{ex-09.097}
                                             = \Cdb*{ex-09.098}
                                             = \Cdb*{ex-09.099}
                                             = \Cdb*{ex-09.101}}
\end{dgroup*}

\clearpage

\begin{cadabra}
   R{#}::LaTeXForm("{{\strut}^g R}").

   gRabcd := \nabla_{c}{\nabla_{b}{v_{a}}}
            -\nabla_{b}{\nabla_{c}{v_{a}}} -> R^{d}_{a b c} v_{d}.

   substitute     (vpqr,gRabcd)                              # cdb(ex-09.102,vpqr)
   distribute     (vpqr)                                     # cdb(ex-09.103,vpqr)
   substitute     (vpqr,$v_{a} -> h^{b}_{a} v_{b}$)          # cdb(ex-09.104,vpqr)
   substitute     (vpqr,$h^{b}_{a} K_{c}^{a} -> K_{c}^{b}$)  # cdb(ex-09.105,vpqr)
   sort_product   (vpqr)                                     # cdb(ex-09.106,vpqr)
   rename_dummies (vpqr)                                     # cdb(ex-09.107,vpqr)
   canonicalise   (vpqr)                                     # cdb(ex-09.108,vpqr)
   factor_out     (vpqr,$v_{a?}$)                            # cdb(ex-09.109,vpqr)
   substitute     (vpqr,$v_{a}->1$)                          # cdb(ex-09.110,vpqr)
   sort_product   (vpqr)                                     # cdb(ex-09.111,vpqr)

   checkpoint.append (vpqr)
\end{cadabra}

\begin{dgroup*}[spread={3pt}]
   \Dmath*{( D_{r}D_{q} - D_{q}D_{r} ) v_{p} = \Cdb*{ex-09.101}
                                             = \Cdb*{ex-09.102}
                                             = \Cdb*{ex-09.103}
                                             = \Cdb*{ex-09.104}
                                             = \Cdb*{ex-09.105}
                                             = \Cdb*{ex-09.106}
                                             = \Cdb*{ex-09.107}
                                             = \Cdb*{ex-09.108}
                                             = \Cdb*{ex-09.109}}
\end{dgroup*}

\begin{dgroup*}[spread={3pt}]
   \Dmath*{{\strut}^hR^{a}{}_{pqr} = \Cdb*{ex-09.110}}
   \Dmath*{{\strut}^hR^{a}{}_{pqr} = \Cdb*{ex-09.111}}
\end{dgroup*}

\clearpage

% ============================================================================================
% export to json format

\bgroup
\CdbSetup{action=hide}
\begin{cadabra}
   for i in range( len(checkpoint) ):
      cdblib.put ('check{:03d}'.format(i),checkpoint[i],checkpoint_file)
\end{cadabra}
\egroup

\end{document}
