% \iffalse meta-comment
%
%% File: latex-lab-math.dtx
%
% Copyright (C) 2022-2024 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
%
% The development version of the bundle can be found below
%
% https://github.com/latex3/latex2e/required/latex-lab
%
% for those people who are interested or want to report an issue.
%
%
\def\ltlabmathdate{2024-04-16}
\def\ltlabmathversion{0.5j}
%
%<*driver>
\documentclass{l3doc}
\EnableCrossrefs
\CodelineIndex
\usepackage{todonotes}
\begin{document}
\DocInput{latex-lab-math.dtx}
\end{document}
%
%
% \fi
%
%
% \title{The \texttt{latex-lab-math} code\thanks{}}
% \author{Frank Mittelbach, Joseph Wright, \LaTeX{} Project}
% \date{v\ltlabmathversion\ \ltlabmathdate}
%
% \maketitle
%
% \newcommand\NEW[1]{\marginpar{\mbox{}\hfill\fbox{New: #1}}}
% \providecommand\class[1]{\texttt{#1.cls}}
% \providecommand\pkg[1]{\texttt{#1}}
%
% \providecommand\hook[1]{\texttt{#1\DescribeHook[noprint]{#1}}}
% \providecommand\socket[1]{\texttt{#1\DescribeSocket[noprint]{#1}}}
% \providecommand\plug[1]{\texttt{#1\DescribePlug[noprint]{#1}}}
%
% \NewDocElement[printtype=\textit{socket},idxtype=socket,idxgroup=Sockets]{Socket}{socketdecl}
% \NewDocElement[printtype=\textit{hook},idxtype=hook,idxgroup=Hooks]{Hook}{hookdecl}
% \NewDocElement[printtype=\textit{plug},idxtype=plug,idxgroup=Plugs]{Plug}{plugdecl}
%
% ^^A \car {...} for marginal comments
% ^^A \car*{...} for longer inline comments
%
% \NewDocumentCommand\car{sO{}m}
% {\IfBooleanTF{#1}{\todo[inline,color=blue!10,#2]{#3}}^^A
% {\todo[color=blue!10,#2]{#3}}}
%
% \NewDocumentCommand\fmi{sO{}m}
% {\IfBooleanTF{#1}{\todo[inline,#2]{#3}}^^A
% {\todo[#2]{#3}}}
%
% \begin{abstract}
% This is an experimental prototype. It captures math material
% (basically okay, but the interfaces for packages aren't yet
% there) and tags the material (which is not yet anywhere near the
% final state). That part is provided for experimentation and to
% gather feedback, etc.
% \end{abstract}
%
% \tableofcontents
%
% \section{Introduction}
% \car*{Todo: update all the documentation! Both here and
% (what little there is!) in the implementation section.}
%
% Tagging math involves a variety of tasks that require that math is captured before the
% typesetting
% \begin{itemize}
% \item When typesetting the math MC-tags and structure commands must
% be inserted at the begin and the end, and perhaps also around lines
% or other subparts of the equation.
% \item The source and/or a mathml-representation of the source must be available
% so that it can be (perhaps after some preprocessing) be used in an associated file
% or in an alternate text
% \item It must be possible to measure the math for e.g. a bbox setting.
% \end{itemize}
%
% This file implements capture of all math mode material at the outer
% level, i.e., a formula is captured in its entirety with inner text
% blocks (possibly containing further math) absorbed as part of the
% formula. For example,
%\begin{verbatim}
% \[ a \in A \text{ for all $a<5$} \]
%\end{verbatim}
% would only result in a single capture of the tokens
% ``\verb*/a \in A \text{ for all $a<5$}/''.
%
%
% \section{Math capture} \label{sec:mathcapture}
% In the current setup
% \begin{itemize}
% \item |$|, |\(...\)| and |$$| grab (through a command in \cs{everymath}/cs{everydisplay})
% if the boolean \cs{l_@@_collected_bool} is false.
% If the boolean is true they behave normally and can for example contain verbatim.
%
% \item All (registered) environments grab their body
% regardless of the state of the boolean. For
% |equation|, |equation*| and |math| this is a change as they no longer can
% contain verbatim.
%
% \item BUG: |\[...\]| grabs if \cs{l_@@_collected_bool} is false. If it is
% true it falls back to |equation*| and then errors because this can't find the end.
% \end{itemize}
%
% \subsection{Code level interfaces}
%
% \begin{function}{\math_register_env:n, \math_register_env:nn}
% \begin{syntax}
% \cs{math_register_env:n} \Arg{env}
% \cs{math_register_env:nn} \Arg{env} \Arg{options}
% \end{syntax}
% Registers the \meta{env} as a math environment which should be captured
% and made available. This is necessary for all top-level math mode
% environments: low-level errors may result if these are not correct
% set up. One or more key--value \meta{options} may also be given:
% \begin{itemize}
% \item[\texttt{arg-spec}] The argument specification taken by the
% beginning of the environment; this is used to remove non-mathematical
% material.
% \end{itemize}
% \end{function}
%
% \begin{function}{\math_processor:n}
% \begin{syntax}
% \cs{math_processor:n} \Arg{tokens}
% \end{syntax}
% Declares that the captured math content should be passed to the
% \meta{tokens}, which will receive the environment type as |#1| and
% the content as |#2|. The processing is done before the typesetting. It is not
% applied if \cs{ifmeasuring@} is true.
% \end{function}
%
% \subsection{Document level interfaces}
%
% \begin{function}{\RegisterMathEnvironment}
% \begin{syntax}
% \cs{RegisterMathEnvironment} \oarg{options} \Arg{env}
% \end{syntax}
% Registers the \meta{env} as a math environment which should be captured
% and made available. This is necessary for all top-level math mode
% environments: low-level errors may result if these are not correct
% set up. One or more key--value \meta{options} may also be given:
% \begin{itemize}
% \item[\texttt{arg-spec}] The argument specification taken by the
% beginning of the environment; this is used to remove non-mathematical
% material.
% \end{itemize}
% \end{function}
%
% \section{Math tagging}
%
% The tagging code has to handle
% \begin{itemize}
% \item the embedding into the surrounding. This means
% \begin{itemize}
% \item closing and reopening MC-chunks
% \item closing and reopening text/P-structures
% \item handling interferences of the tagging code with penalties and spacing.
% \end{itemize}
% \item the actual tagging which means to do some or all of the following tasks:
% \begin{itemize}
% \item setup content for an associated source file
% \item setup content for an associated mathml file
% \item setup content for the /Alt key
% \item setup content for the /ActualText key
% \item setup attributes
% \item add associated files
% \item add a Formula structure
% \item surround subparts (e.g. lines) with Formula sub structures
% (perhaps with their own set of additional content)
% \item surround elements of the equation with mathml structure elements
% (currently only luatex with luamml)
% \end{itemize}
% \end{itemize}
%
% \subsection{Inline math}
%
% The embedding code is added through
% the sockets
% \begin{itemize}
% \item |tagsupport/math/inline/begin|
% \item |tagsupport/math/inline/end|
% \end{itemize}
% The sockets simply push and pop the MC currently. Without
% tagging they use the noop-plug.
%
% The actual tagging is in done through the sockets
% \begin{itemize}
% \item |tagsupport/math/inline/formula/begin|
% This socket takes the math as argument and its code
% should output it for typesetting. Without tagging the
% socket uses the identity plug.
% The |default| plug of the socket calls these three internal sockets
% for the tagging support:
% \begin{itemize}
% \item |tagsupport/math/content| This should set up the various
% content variables (empty variables are ignored by the structure code
% and so can be used to suppress a setting).
% \item |tagsupport/math/struct/begin| This calls \cs{tag_struct_begin:n}.
% It should also write the associated files if needed.
% \item |tagsupport/math/substruct/begin| this handles subparts.
% TODO: does it really make sense in inline math to have that??
% \end{itemize}
% \item |tagsupport/math/inline/formula/end|
% This socket ends the formula structure(s). The |default|
% plug calls these internal sockets:
% \begin{itemize}
% \item |tagsupport/math/substruct/end|
% \item |tagsupport/math/struct/end|
% \end{itemize}
% \end{itemize}
%
% \subsection{Display math}
%
% \textit{to be written}
%
% \subsection{Associated Files}
%
% The current code allows the attachment of two types of associated file to the
% Formula structure:
% the \LaTeX\ source and a MathML representation.
% Technically both can be attached---AF is an array
% of file references---practically there can be problems with PDF consumers:
% e.g. ngpdf used both and so showed the equation twice
% (this has been corrected in the newest version) and
% Foxit seems to see only the first AF in the array (so we attach the
% mathml as first file).
%
% The \LaTeX\ source can be (and is) attached automatically.
% It can be suppressed by an option with
% \texttt{math/tex/AF=false}, see below.
%
% The MathML is attached if a file |\jobname-mathml.html| is found and
% if it contains a suitable MathML snippet for the current formula.
% The MathML processing can be suppressed globally by emptying the list of
% mathml files with |math/mathml/sources=|. Locally for a formula |math/mathml/AF=false|
% can be used.
%
% For a MathML representation a file with such representations must be provided.
% If the equation is numbered the numbering should be part of the MathML as
% the |Lbl| substructure is ignored if an MathML is used (see https://github.com/foxitsoftware/PDF_UA-2).
%
% The MathML representation is given in a special format.
% It is meant to be a valid html file
% that can be viewed in a browser.
% For this it can start with || and end with ||
% It should have the extension \texttt{.html}. The \meta{mathml} content
% is read with special catcodes, so can contain ambersands, hashes, comment chars
% and unmatched braces such as |{|
%
% The file should contain a number of representations in this format:
% \begin{quote}
% |
| \\
% |
\mml| \meta{key}|
|\\
% |
|\meta{source}|
| \\
% |
|\meta{hash}|
| \\
% |
|\\
% |
|
% \end{quote}
% The keywords || are required as
% they are used to delimit the arguments by the \LaTeX{} code.
%
% \meta{key} and \meta{source} are only used for debugging, they help to identify
% the equation referred by this representation. The source should be used correctly escaped
% |&| and |<| so that if gives valid html!
%
% \meta{attributes} is not required either, but can e.g. contain attributes
% to improve the display in a browser:
% \begin{verbatim}
%