\RequirePackage{paralist}
\ifcsname stexdocpath\endcsname\else\def\stexdocpath{.}\fi
\documentclass[full]{l3doc}

\usepackage[hyperref=auto,style=alphabetic]{biblatex}
\usepackage[lang={en,de}]{stex}
\usepackage[show]{ed}
\usepackage{rustex}
\usepackage{stex-highlighting,stexthm}


\srefsetin[sTeX/Documentation]{documentation}{the \stex Documentation}

\makeatletter
\providecommand{\HTML}{\textsc{html}\xspace}%
\providecommand{\XML}{\textsc{xml}\xspace}%
\providecommand{\PDF}{\textsc{pdf}\xspace}%
\providecommand\openmath{\textsc{OpenMath}\xspace}
\providecommand\OMDoc{\textsc{OMDoc}\xspace}
\DeclareRobustCommand\LaTeXML{L\kern-.36em%
        {\sbox\z@ T%
         \vbox to\ht\z@{\hbox{\check@mathfonts
                              \fontsize\sf@size\z@
                              \math@fontsfalse\selectfont
                              A}%
                        \vss}%
        }%
        \kern-.15em%
        T\kern-.1667em\lower.4ex\hbox{E}\kern-0.05em\relax
        {\scshape xml}\xspace}%
\def\mmt{\textsc{Mmt}\xspace}
\def\immt{\textsc{iMmt}\xspace}
\makeatother


\newif\ifhadtitle\hadtitlefalse

\def\stexversion{4.0.0}
\def\changedate{\today}
\def\stextoptitle#1#2{\title{#1\thanks{Version {\stexversion} (last revised {\changedate})} }\def\thispkg{#2}}

\author{Michael Kohlhase, Dennis Müller\\
 	FAU Erlangen-Nürnberg\\
 	\url{http://kwarc.info/}
}

\def\docmodule#1{
  \begin{document}
    \stexmaketitle
    \setsectionlevel{#1}
    \stexdocumentation
    \steximplementation
    \stexfooter
  \end{document}
}


\ExplSyntaxOn
\def\stexmaketitle{
  \ifhadtitle\else
    \hadtitletrue
    \maketitle
    \tableofcontents
    \int_gincr:N \l_stex_current_section_level_int

    \seq_map_inline:Nn \c_stex_mathhub_files {
      \__stex_mathhub_find_manifest:nn {##1}{ sTeX / Documentation }
      \str_if_empty:NF \l__stex_mathhub_manifest_str \seq_map_break:
    }
  %  \str_if_empty:NF \l__stex_mathhub_manifest_str {
  %    \usemodule[sTeX/Documentation]{macros?AllMacros}
  %  }
  \fi
}

\def\stexdomanual{
  \DisableDocumentation
  \DisableImplementation
  \EnableManual
  \begin{sfragment}{Manual}
    \DocInput{\jobname.dtx}
  \end{sfragment}
}

\def\stexdocumentation{
  \DisableManual
  \DisableImplementation
  \EnableDocumentation
  \begin{sfragment}{Documentation}
    \DocInput{\jobname.dtx}
  \end{sfragment}
}

\def\steximplementation{
  \DisableManual
  \DisableDocumentation
  \EnableImplementation
  \begin{sfragment}{Implementation}
    \DocInput{\jobname.dtx}
  \end{sfragment}
}

\def\stexfooter{
  \clearpage
  \PrintIndex
  \printbibliography
}

\ExplSyntaxOff

\usepackage{lststex,mdframed}
\usepackage[most]{tcolorbox}

\lstset{literate=%
    {Ö}{{\"O}}1
    {Ä}{{\"A}}1
    {Ü}{{\"U}}1
    {ß}{{\ss}}1
    {ü}{{\"u}}1
    {ä}{{\"a}}1
    {ö}{{\"o}}1
    {~}{{\textasciitilde}}1
}


\newenvironment{framed}[1][]{
  \ifstexhtml\par\vbox\bgroup
    \csname exp_args:Nne\endcsname\begin{stex_annotate_env}{%
      style:border=solid 1px black,%
      style:width=var(--this-width),%
      style:min-width=var(--this-width),%
      style:--this-width=calc(var(--current-width) - 6px),%
      style:padding=3px,%
      style:margin-top=5px,%
      style:margin-bottom=5px%
    }
    \csname stex_annotate_invisible:n\endcsname{ }%
    \begin{stex_annotate_env}{%
      style:--current-width=var(--this-width);%
    }\csname stex_annotate_invisible:n\endcsname{ }
  \else\begin{mdframed}[#1]\fi
}{%
  \ifstexhtml
    \end{stex_annotate_env}\end{stex_annotate_env}\egroup\par
  \else\end{mdframed}\fi
}
\newcommand{\scaled}[2][0.9\hsize]{\begin{center}\resizebox{#1}{!}{\begin{minipage}{\textwidth} #2 \end{minipage}}\end{center}}

\makeatletter
\ExplSyntaxOn

\def\doc_exbox:nnn#1#2#3{
  \begin{sexample}[#3]
    Input:
    \begin{framed}[linewidth=1pt,backgroundcolor=white]\small
      #1
    \end{framed}
    Output:
    \begin{framed}[linewidth=1pt,backgroundcolor=white]\small
      #2
    \end{framed}
  \end{sexample}
}


\NewDocumentCommand\stexexamplefile{O{} m O{} O{}}{
  \doc_exbox:nnn{
    \hfill File~\str_if_empty:nTF{#1}{
      \prop_if_exist:NT \l_stex_current_archive_prop {
        [\texttt{\prop_item:Nn \l_stex_current_archive_prop {id}}]
      }
    }{[#1]}\texttt{\tl_to_str:n{#2}}
    \_lststex_parse_args:n{#3}
    \stex_in_archive:nn{#1}{
      \exp_args:Nno \use:nn{\lstinputlisting[} \l_lststex_return_tl ]{\stex_source_path:n{#2}}
    }
  }{
    \inputref[#1]{#2}
  }{#4}
}

\newwrite\testoutfile
\NewDocumentCommand\stexexample{O{} O{}}{
  \begingroup 
  \catcode`\\=12\relax
  \catcode`\#=12\relax
  \catcode`\&=12\relax
  \catcode`\$=12\relax
  \catcode`\^=12\relax
  \catcode`\_=12\relax
  \catcode`\ =12\relax
  \catcode`^^J=12\relax
  \endlinechar=`^^J
  \newlinechar=-1
%^^A    \everyeof{\noexpand}
  \example_a:nnn{#1}{#2}
}
\long\def\example_a:nnn #1 #2 #3 {
  \endgroup
  \immediate\openout\testoutfile=\jobname.exmpl
  \immediate\write\testoutfile{
    \c_backslash_str begin{stexcode}[#1]
    \detokenize{^^J}#3
    \c_backslash_str end{stexcode}
  }
  \immediate\closeout\testoutfile
  \doc_exbox:nnn{
    \catcode`\#=12\relax
    \csname @ @ input\endcsname{\jobname.exmpl}
  }{
    \immediate\openout\testoutfile=\jobname.exmpl
    \immediate\write\testoutfile{#3}
    \immediate\closeout\testoutfile
    \csname @ @ input\endcsname \jobname.exmpl\relax
  }{#2}
  \peek_charcode_remove:NT ^^J
}

\ExplSyntaxOff
\makeatother

\makeatletter
\newcount\example@counter\example@counter=0
\newtcolorbox{exampleborderbox}[1][]{
  empty,
  title={Example \the\example@counter #1},
  attach boxed title to top left,
     minipage boxed title,
  boxed title style={empty,size=minimal,toprule=0pt,top=1pt,left=3mm,overlay={}},
  coltitle=blue,fonttitle=\bfseries,
  parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
     before upper=\csname @totalleftmargin\endcsname0pt, 
  overlay unbroken={\draw[blue,line width=2pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
  overlay first={\draw[blue,line width=2pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
  overlay middle={\draw[blue,line width=2pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
  overlay last={\draw[blue,line width=2pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
  outer arc=4pt%
}

\ExplSyntaxOn
\stexstyleexample{
  \global\advance\example@counter by 1
  \tl_if_empty:NTF\thistitle{
    \begin{exampleborderbox}
  }{
    \begin{exampleborderbox}[ (\thistitle)]
  }
}{
    \end{exampleborderbox}
}
\ExplSyntaxOff\makeatother

\stexcssexample[counter=example]{
    border-left:3px solid blue;
    padding-left:3px;
    margin-left: -3px;
    width:calc(var(--rustex-curr-width) + 3px);
}{
  font-weight: bold;
  font-size:inherit;
  color:blue;
  display:block;
  &::before {
    content: "Example " counter(ftml-example) " ";
  }
  &::after {
    content: ".";
  }
}


\usetikzlibrary{calc}

\def\textwarning{\includegraphics[width=1.2em]{stex-dangerous-bend}\xspace}
\newtcolorbox{dangerbox}{
  breakable,
  enhanced,
  left=0pt,
  right=0pt,
  top=8pt,
  bottom=8pt,
  colback=white,
  colframe=red,
  width=\textwidth,
  enlarge left by=0mm,
  boxsep=5pt,
  fontupper=\small,
  arc=4pt,
  outer arc=4pt,
  leftupper=1.5cm,
  overlay={
    \node[anchor=west] at ([xshift=10pt]$(frame.north west)!0.5!(frame.south west)$)
       {\includegraphics[width=1cm,height=1cm]{stex-dangerous-bend}};}
}

\protected\def\TODO#1{\textcolor{red}{TODO}\footnote{\textcolor{red}{TODO: #1}}}

\definecolor{darkgreen}{rgb}{0.0, 0.5, 0.0}

\usepackage[solutions]{problem}
\usepackage{hwexam}
\newtcolorbox{problemborderbox}[1][]{
  empty,
  title={Exercise #1},
  attach boxed title to top left,
     minipage boxed title,
  boxed title style={empty,size=minimal,toprule=0pt,top=1pt,left=3mm,overlay={}},
  coltitle=darkgreen,fonttitle=\bfseries,
  parbox=false,boxsep=0pt,left=3mm,right=0mm,top=2pt,breakable,pad at break=0mm,
     before upper=\csname @totalleftmargin\endcsname0pt, 
  overlay unbroken={\draw[darkgreen,line width=2pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
  overlay first={\draw[darkgreen,line width=2pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
  overlay middle={\draw[darkgreen,line width=2pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
  overlay last={\draw[darkgreen,line width=2pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
  outer arc=4pt%
}

\ExplSyntaxOn
\stexstyleproblem{
  \tl_if_empty:NTF\thistitle{
    \begin{problemborderbox}
  }{
    \begin{problemborderbox}[ (\thistitle)]
  }
}{
    \end{problemborderbox}
}
\ExplSyntaxOff

\stexcssproblem{
    border-left:3px darkgreen solid;
    padding-left:3px;
    margin-left: -3px;
    calc(var(--rustex-curr-width) + 3px);
}{
  font-weight: bold;
  font-size:inherit;
  color:darkgreen;
  display:block;
  &::before {
    content: "Exercise";
  }
}

\newtcolorbox{experimental}{
  breakable,
  enhanced,
  left=0pt,
  right=0pt,
  top=8pt,
  bottom=8pt,
  colback=white,
  colframe=gray,
  width=\textwidth,
  enlarge left by=0mm,
  boxsep=5pt,
  fontupper=\small,
  arc=4pt,
  outer arc=4pt,
  leftupper=1.5cm,
  overlay={
    \node[anchor=west] at ([xshift=10pt]$(frame.north west)!0.5!(frame.south west)$)
       {\includegraphics[height=1cm]{stex-experimental}};}
}


\usetikzlibrary{decorations.pathmorphing,shapes,arrows,calc}
% Taken from pgflibrarytikzmmt.code.tex
\newcommand{\mmtarrowtip}{angle 45}
\newcommand{\mmtarrowtipmonoright}{right hook}

\tikzstyle{include}=[\mmtarrowtipmonoright-\mmtarrowtip,thick]
\tikzstyle{morph}=[-\mmtarrowtip,thick]
\tikzstyle{preview}=[decorate, decoration={coil,aspect=0,amplitude=1pt,
                                                  segment length=6pt,
                                                  pre=lineto,pre length=3pt,
                                                  post=lineto,post length=5pt}, thick]
\tikzstyle{view}=[preview,-\mmtarrowtip]


% TIKZ RULES
\def\mmtlogo{
\begin{tikzpicture}

  % White Background (Margins are eyeballed)
  % This is necessary because we paste white over arrows later.
  % If somebody want's to do the full song and dance with
  % interrupted arrows to get transparent background, be my guest.

  \fill[white!] (-0.01,0.15) rectangle (1.11,-0.95);

  % Arrows
  \draw [blue, include] (0,0)     -- (1.1,0);
  \draw [green, morph] (0,-0.4)  -- (1.1,-0.4);
  \draw [red, view]   (-0,-0.8) -- (1.1,-0.8);

  % Cutout for letters
  \fill[white] (0.33,0.1) rectangle (0.66,-0.9);

  % Letters
  \node at (0.18,0)    (nodeM1) {\large M};
  \node at (0.18,-0.4) (nodeM2) {\large M};
  \node at (0.21,-0.8) (nodeT)  {\large T};

\end{tikzpicture}
}

\newtcolorbox{mmtbox}{
  breakable,
  enhanced,
  left=0pt,
  right=0pt,
  top=8pt,
  bottom=8pt,
  colback=white,
  colframe=green,
  width=\textwidth,
  enlarge left by=0mm,
  boxsep=5pt,
  fontupper=\small,
  arc=4pt,
  outer arc=4pt,
  leftupper=1.5cm,
  overlay={
    \node[anchor=west] at ([xshift=10pt]$(frame.north west)!0.5!(frame.south west)$)
       {\mmtlogo};}
}

\newtcolorbox{defibox}{
  breakable,
  enhanced,
  left=0pt,
  right=0pt,
  top=8pt,
  bottom=8pt,
  colback=white,
  colframe=orange,
  width=\textwidth,
  enlarge left by=0mm,
  boxsep=5pt,
  fontupper=\small,
  leftupper=1.5cm,
  arc=4pt,
  outer arc=4pt,
  overlay={\node[anchor=west] at ([xshift=10pt]$(frame.north west)!0.5!(frame.south west)$)
       {\textcolor{orange}{\Large\sTeX}};}
}

\stexstyleparagraph[defibox]{
  \begin{defibox}
}{
  \end{defibox}
}
\stexcssparagraph[defibox]{
    border:3px solid orange;
    padding:3px;
    margin-left: -6px;
    width: calc(var(--rustex-curr-width) + 6px);
}{
  font-weight: bold;
  font-size:inherit;
  color:orange;
  display:block;
  &::before {
    font-size:larger;
    content: "sTeX ";
  }
  &::after {
    content: "";
  }
}

\AtBeginDocument{\catcode`_=8}

\ExplSyntaxOn

\bool_new:N \g_stexdoc_typeset_manual_bool
\NewDocumentCommand \EnableManual {}{
  \bool_gset_true:N \g_stexdoc_typeset_manual_bool
  \renewenvironment{sfunction}{\manualsfunction}{\endmanualsfunction}
}

\NewDocumentCommand \DisableManual {}{
  \bool_gset_false:N \g_stexdoc_typeset_manual_bool
  \renewenvironment{sfunction}{\realsfunction}{\endrealsfunction}
}

\NewDocumentEnvironment {stexmanual} {} {
  \bool_if:NTF \g_stexdoc_typeset_manual_bool
    {\bool_set_false:N \l__codedoc_in_implementation_bool}
    {\comment}
}{
  \bool_if:NF \g_stexdoc_typeset_manual_bool {\endcomment}
}


\stex_sms_allow_escape:N \stexmacro
\protected\def\stexmacro#1{
  \stex_if_in_module:T{
    \stex_execute_in_module:e{\tl_set:cn{l_stex_stexmacro_\tl_to_str:n{#1}_path}{\exp_args:Ne\stex_new_symbol_uri:n{\tl_to_str:n{#1}}}}
    \exp_args:NNe\symdecl*{\tl_to_str:n{#1}}[type=\sn{TeX?macro}]
    \exp_args:Ne\notation{\tl_to_str:n{#1}}{\comp{\texttt{\textbackslash\detokenize{#1}}}}
  }
}

\ProvideDocumentEnvironment {manualsfunction}{m O{} +v}{
  \seq_set_split:Nnn \_stexdoc_sfunction_seq , {#1}
  \seq_map_inline:Nn \_stexdoc_sfunction_seq {
    \stex_pseudogroup_with:nn{\stex_smsmode_do:}{\def\stex_smsmode_do:{}\stexmacro{##1}}
  }
  \cs_set_eq:NN \dcs \_stexdoc_dcs:
  \ifstexhtml 
    \def\box_move_up:nn##1##2{\tex_raise:D\__box_dim_eval:n{0pt}##2}
    \renewenvironment{syntax}{\noindent}{\par}
  \fi
  \begin{sparagraph}[style=symdoc,for={#1}]
    \__codedoc_function:nnw{#2}{#3}
}{
  \stex_if_smsmode:F{\__codedoc_function_end:}
  \end{sparagraph}
}

\NewDocumentEnvironment{ realsfunction }{ O{} +v m }{
    \protected\def \StartImpl {\_start_impl_fun:nn{#1}{#2}}
    \bool_if:NTF \g__codedoc_typeset_documentation_bool {
      \__codedoc_function:nnw{#1}{#2}
      \tl_if_empty:nF{#3}{ \begin{syntax} #3 \end{syntax} }
    }{
      \bool_if:NTF \g_stexdoc_typeset_manual_bool {
        \__codedoc_function:nnw{#1}{#2}
        \tl_if_empty:nF{#3}{ \begin{syntax} #3 \end{syntax} }
      }
      {
        \exp_after:wN \StartImpl \_skip_doc:
      }
    }
}{
    \bool_if:NT \g_stexdoc_in_comment_bool {
      \bool_gset_false:N \g_stexdoc_in_comment_bool
      \endcomment
    }
    \bool_if:NTF \g__codedoc_typeset_implementation_bool {
      \__codedoc_macro_end:
    }
}
\newenvironment{sfunction}{\realsfunction}{\endrealsfunction}
\stex_sms_allow_env:n{sfunction}

\NewDocumentEnvironment{ svariable }{ O{} +v }{
  \protected\def \StartImpl {\_start_impl_fun:nn{var, #1}{#2}}
  \bool_if:NTF \g__codedoc_typeset_documentation_bool {
    \bool_set_false:N \l__codedoc_in_implementation_bool
    \__codedoc_function:nnw{#1}{#2}
  }{
    \exp_after:wN \StartImpl \_skip_doc:
  }
}{
  \bool_if:NTF \g__codedoc_typeset_implementation_bool {
    \__codedoc_macro_end:
  }{
    \endcomment
  }
}

\NewDocumentEnvironment{ senvironment }{ +v }{
  \protected\def \StartImpl {\_start_env_fun:n{#1}}
  \bool_if:NTF \g__codedoc_typeset_documentation_bool {
    \bool_set_false:N \l__codedoc_in_implementation_bool
    \vskip10pt
    \DescribeEnv{#1}
  }{
    \exp_after:wN \StartImpl \_skip_doc:
  }
}{
  \bool_if:NTF \g__codedoc_typeset_implementation_bool {
    \endenvironment
  }{
    \endcomment
  }
}

\cs_new:Npn \_skip_doc: #1 \StartImpl {}

\cs_new_protected:Nn \_start_impl_fun:nn {
  \bool_if:NT \g__codedoc_typeset_documentation_bool {
    \__codedoc_function_end:
  }
  \bool_if:NTF \g__codedoc_typeset_implementation_bool {
    \__codedoc_macro:nnw{#1}{#2}
  }{ 
    \bool_gset_true:N \g_stexdoc_in_comment_bool
    \comment 
  }
}

\cs_new_protected:Nn \_start_env_fun:n {
  \bool_if:NT \g__codedoc_typeset_documentation_bool {
    \par
  }
  \bool_if:NTF \g__codedoc_typeset_implementation_bool {
    \environment{#1}
  }{ 
    \bool_gset_true:N \g_stexdoc_in_comment_bool
    \comment 
  }
}

\bool_new:N \g_stexdoc_in_comment_bool

\ExplSyntaxOff