% \iffalse meta-comment 
% An Infrastructure for Presenting Semantic Macros in sTeX
% Copyright (C) 2004-2007 Michael Kohlhase, all rights reserved
% This file is released under the LaTeX Project Public License (LPPL)
%
% The original of this file is in the public repository at 
% http://github.com/KWARC/sTeX/
% \fi
% 
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{cmath}[2019/03/20 v0.1 inline content math]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage[utf8]{inputenc}
\usepackage{url,array,float,amstext,alltt}
\usepackage{modules,cmath,stex-logo}
\usepackage[show]{ed}
\usepackage[hyperref=auto,style=alphabetic]{biblatex}
\addbibresource{kwarcpubs.bib}
\addbibresource{extpubs.bib}
\addbibresource{kwarccrossrefs.bib}
\addbibresource{extcrossrefs.bib}
\usepackage{ctangit}
\usepackage{hyperref}
\makeindex
\floatstyle{boxed}
\newfloat{exfig}{thp}{lop}
\floatname{exfig}{Example}
\def\githubissue#1{\cite{sTeX:github:on}, \hyperlink{https://github.com/KWARC/sTeX/issues/#1}{issue #1}}
\begin{document}
\RecordChanges
\DocInput{cmath.dtx}
\end{document}
%</driver>
% \fi
% 
% \CheckSum{84}
%
% \changes{v0.2}{2016/02/12}{First Version with Documentation, extracted variables stuff
% from \texttt{presentation.dtx}}
% \changes{v0.2}{2016/02/12}{reinstating id macros from \texttt{latexml.sty}}
% \GetFileInfo{cmath.sty}
% 
% \MakeShortVerb{\|}
%\def\scsys#1{{{\sc #1}}\index{#1@{\sc #1}}}
% \def\xml{\scsys{Xml}}
% \def\mathml{\scsys{MathML}}
% \def\omdoc{\scsys{OMDoc}}
% \def\openmath{\scsys{OpenMath}}
% \def\latexml{\scsys{LaTeXML}}
% \def\perl{\scsys{Perl}}
% \def\cmathml{Content-{\sc MathML}\index{Content {\sc MathML}}\index{MathML@{\sc MathML}!content}}
% \def\activemath{\scsys{ActiveMath}}
% \def\twin#1#2{\index{#1!#2}\index{#2!#1}}
% \def\twintoo#1#2{{#1 #2}\twin{#1}{#2}}
% \def\atwin#1#2#3{\index{#1!#2!#3}\index{#3!#2 (#1)}}
% \def\atwintoo#1#2#3{{#1 #2 #3}\atwin{#1}{#2}{#3}}
% \title{{\texttt{cmath.sty}}: An Infrastructure for building Inline Content Math in
% {\stex}\thanks{Version {\fileversion} (last revised {\filedate})}} 
%    \author{Michael Kohlhase \\
%    FAU Erlangen-N\"urnberg\\
%    \url{http://kwarc.info/kohlhase}\and
%    Deyan Ginev\\Authorea}
% \date{\today}
% \maketitle
%
% \begin{abstract}
%   The |cmath| package is a central part of the {\stex} collection, a version of
%   {\TeX/\LaTeX} that allows to markup {\TeX/\LaTeX} documents semantically without
%   leaving the document format, essentially turning {\TeX/\LaTeX} into a document format
%   for mathematical knowledge management (MKM).
% 
%   This package supplies an infrastructure that allows to build content math expressions
%   (strict content MathML or OpenMath objects) in the text. This is needed whenever the
%   head symbols of expressions are variables and can thus not be treated via the
%   |\symdef| mechanism in \stex.
% \end{abstract}
%
% \newpage\setcounter{tocdepth}{2}\tableofcontents\newpage
%
%\section{Introduction}\label{sec:intro}
%
% \stex allows to build content math expressions via the |\symdef|
% mechanism~\cite{KohAmb:smmssl:ctan} if their heads are constants. For instance, if we
% have defined |\symdef{lt}[2]{#1<#2}| in the module |relation1|, then an invocation of
% |\lt3a| will be transformed to
% \begin{verbatim} 
% <OMA>
%   <OMS cd="relation1" name="lt"/>
%   <OMI>3</OMI>
%   <OMV name="a"/>
% </OMA>
% \end{verbatim}
% If the head of the expression (i.e. the function symbol in this case) is a variable,
% then we cannot resort to a |\symdef|, since that would define the functional equivalent
% of a logical constant. Sometimes, {\latexml} can figure out that when we write $f(a,b)$
% that $f$ is a function (especially, if we declare them to be via the |functions=| key in
% the dominating statement environment~\cite{Kohlhase:smmtf:ctan}). But sometimes, we want
% to be explicit, especially for $n$-ary functions and in the presence of elided elements
% in argument sequences. A related problem is markup for complex variable names, such as
% $x_{\text{left}}$ or $ST^*$.
% 
% The |cmath| package supplies the {\LaTeX} bindings that allow us to
% achieve this. 
%
% \section{The User Interface}\label{sec:user}
% 
% \subsection{Variable Names}\label{sec:inter:vname}
% 
% In mathematics we often use complex variable names like $x'$, $g_n$, $f^1$,
% $\widetilde\phi_i^j$ or even $foo$; for presentation-oriented {\LaTeX}, this is not a
% problem, but if we want to generate content markup, we must show
% explicitly that those are complex identifiers (otherwise the
% variable name $foo$ might be mistaken for the product $f\cdot 
% o\cdot o$). In careful mathematical typesetting, |$sin$| is distinguished from |$\sin$|,
% but we cannot rely on this effect for variable names.
% 
% \DescribeMacro{\vname} |\vname| identifies a token sequence as a name, and allows the
% user to provide an ASCII ({\xml}-compatible) identifier for it. The optional argument is
% the identifier, and the second one the LaTeX representation. The identifier can also be
% used with \DescribeMacro{\vname} |\vnref| for referencing. So, if we have used
% |\vnname[xi]{x_i}|, then we can later use |\vnref{xi}| as a short name for
% |\vname{x_i}|. Note that in output formats that are capable of generating structure
% sharing, |\vnref{xi}| would be represented as a
% cross-reference.\ednote{DG: Do we know whether using the same name
% in two vname invocations, would refer to two instances of the same variable? Presumably so, since the names are the same? We should make this explicit in the
% text. A different variable would e.g. have a name ``xi2'', but the same body}
%
% Since indexed variable names make a significant special case of complex identifiers, we
% provides the macros \DescribeMacro{\livar}|\livar| that allows to mark up variables with
% lower indices. If |\livar| is given an optional first argument, this is taken as a
% name. Thus |\livar[foo]{x}1| is ``short'' for |\vname[foo]{x_1}|. The macros
% \DescribeMacro{\livar}|\livar|, serve the analogous purpose for variables with upper
% indices, and \DescribeMacro{\ulivar}|\ulivar| for upper and lower indices. Finally,
% \DescribeMacro{\primvar}|\primvar| and \DescribeMacro{\pprimvar}|\pprimvar| do the same
% for variables with primes and double primes (triple primes are bad style).
% 
% \subsection{Applications}\label{sec:user:appl}
% 
% To construct a content math application of the form $f(a_1,\ldots,a_n)$ with concrete
% arguments $a_i$ (i.e. without elisions), then we can use the
% \DescribeMacro{\nappa}|\nappa| macro.  If we have elisions in the arguments, then we
% have to interpret the arguments as a sequence of argument constructors applied to the
% respective positional indexes. We can mark up this situation with the
% \DescribeMacro{\nappf}|\nappf| macro:
% |\nappf{|\meta{fun}|}{|\meta{const}|}{|\meta{first}|}{|\meta{last}|}| where \meta{const}
% is a macro for the constructor is presented as
% $\meta{fun}(\meta{const}\meta{first},\ldots,\meta{const}\meta{last})$; see
% Figure~\ref{fig:application} for a concrete example, and
% Figure~\ref{fig:application-result}.\ednote{MK@MK: we need a meta-cd |cmath| with the
% respective notation definition here. It is very frustrating that we cannot even really
% write down the axiomatization of flexary constants in OpenMath.}
% 
% \begin{figure}\centering
%   \begin{tabular}{|l|l|}\hline
%     \verb|\nappa{f}{a_1,a_2,a_3}| & $\nappa{f}{a_1,a_2,a_3}$\\\hline
%     \verb|\nappe{f}{a_1}{a_n}| &  $\nappe{f}{a_1}{a_n}$\\\hline
%     \verb|\symdef{eph}[1]{e_{#1}^{\varphi(#1)}}|& $\def\foo#1{e_{#1}^{\varphi(#1)}}\nappf{g}\foo14$\\
%     \verb|\nappf{g}\eph14| 
%     & \\\hline
%     \verb|\nappli{f}a1n| & $\nappli{f}a1n$\\\hline
%     \verb|\nappui{f}a1n| & $\nappui{f}a1n$\\\hline
%   \end{tabular}
%   \caption{Application Macros}\label{fig:application}
% \end{figure}
%
% For a simple elision in the arguments, we can use
% \DescribeMacro{\nappe}|\nappe{|\meta{fun}|}{|\meta{first}|}{|\meta{last}|}| will be
% formatted as $\meta{fun}(\meta{first},\ldots,\meta{last})$. Note that this is quite
% un-semantic (we have to guess the sequence), so the use of |\nappe| is discouraged.
% 
% A solution to this situation is if we can think of the arguments as a finite sequence
% $a=:(a_i)_{l\leq i\leq h}$, then we can use \DescribeMacro
% {\nappli}|\nappli{|\meta{fun}|}{|\meta{seq}|}{|\meta{start}|}{|\meta{end}|}|, where
% \meta{seq} is the sequence, and the remaining arguments are the start and end index. The
% \DescribeMacro{\nappui} works like |\nappli|, but uses upper indices in the
% presentation.
% 
% \begin{exfig}\centering
% \begin{verbatim}
% \symdef{eph}[1]{e_{#1}^{\phi(#1)}}
% \nappf{g}\eph14
% \end{verbatim}
% currently generates
% \begin{verbatim}
% <OMA>
%   <OMS cd="cmath" name="apply-from-to"/>
%   <OMV name="g"/>
%   <OMBIND>
%     <OMS cd="fns1" name="lambda"/>
%     <OMBVAR><OMV name="x"/></OMBVAR>
%     <OMA><OMS cd="???" name="eph"/><OMV name="x"/></OMA>
%   </OMBIND>
%   <OMI>1</OMI>
%   <OMI>4</OMI>
% </OMA>
% \end{verbatim}
% \caption{Application Macros}\label{fig:application-result}
% \end{exfig}
%
% \subsection{Binders}\label{sec:user:bind}
%\ednote{MK: document}
%
% \subsection{Sharing}\label{sec:user:sharing}
% We (currently) use the 
%
% \section{Limitations}\label{sec:limitations}
% 
% In this section we document known limitations. If you want to help alleviate them,
% please feel free to contact the package author. Some of them are currently discussed in
% the \sTeX GitHub repository~\cite{sTeX:github:on}. 
% \begin{enumerate}
% \item none reported yet
% \end{enumerate}
% 
% \StopEventually{\newpage\PrintIndex\newpage\PrintChanges\printbibliography}
%
% \section{The Implementation}\label{sec:implementation}
% 
% \subsection{Package Options}\label{sec:impl:options}
%
% The |cmath| package does not take options (at the moment), but we pass any we get to the
% |presentation| package. 
%
%    \begin{macrocode}
%<*package>
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{presentation}}
\ProcessOptions
%    \end{macrocode}
%
% The next measure is to ensure that some {\sTeX} packages are loaded. For {\latexml}, we
% also initialize the package inclusions, there we do not need |ntheorem|, since the XML
% does not do the presentation.
%    \begin{macrocode}
\RequirePackage{presentation}
%    \end{macrocode}

% \subsection{Variable Names}\label{sec:impl:vname}
%
% \begin{macro}{\vname}
%   a name macro; the first optional argument is an identifier \meta{id}, this is standard
%   for {\LaTeX}, but for {\latexml}, we want to generate attributes
%   |xml:id="cvar.|\meta{id}|"| and |name="|\meta{id}|"|.  However, if no id was given in
%   we default them to |xml:id="cvar.|\meta{count}|"| and
%   |name="name.cvar.|\meta{count}|"|.
%    \begin{macrocode}
\newcommand\vname[2][]{#2%
\def\@opt{#1}%
\ifx\@opt\@empty\else\expandafter\gdef\csname MOD@name@#1\endcsname{#2}\fi}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\vnref}
%    \begin{macrocode}
\def\vnref#1{\csname MOD@name@#1\endcsname}
%    \end{macrocode}
% \end{macro}
% \ednote{the following macros are just ideas, they need to be implemented and documented}
% \begin{macro}{\uivar}
%   constructors for variables.
%    \begin{macrocode}
\newcommand\primvar[2][]{\vname[#1]{#2^\prime}}
\newcommand\pprimvar[2][]{\vname[#1]{#2^{\prime\prime}}}
\newcommand\uivar[3][]{\vname[#1]{{#2}^{#3}}}
\newcommand\livar[3][]{\vname[#1]{{#2}_{#3}}}
\newcommand\ulivar[4][]{\vname[#1]{{#2}^{#3}_{#4}}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Applications}\label{sec:impl:appl}
% 
% \begin{macro}{\napp*}
%   \ednote{document keyval args above and implement them in LaTeXML}
%    \begin{macrocode}
\newcommand\nappa[3][]{\prefix[#1]{#2}{#3}}
\newcommand\nappe[4][]{\nappa[#1]{#2}{#3,\ldots,#4}}
\newcommand\nappf[5][]{\nappe[#1]{#2}{#3{#4}}{#3{#5}}}
\newcommand\nappli[5][]{\nappe[#1]{#2}{#3_{#4}}{#3_{#5}}}
\newcommand\nappui[5][]{\nappe[#1]{#2}{#3^{#4}}{#3^{#5}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\anapp*}
%   \ednote{document anapp* and implement in LaTeXML (i.e. get the presentation
%   information into the OM/MathML).}
%    \begin{macrocode}
\newcommand\anappa[3][]{\assoc[#1]{#2}{#3}}
\newcommand\anappe[4][]{\anappa[#1]{#2}{#3,\ldots,#4}}
\newcommand\anappf[5][]{\anappe[#1]{#2}{#3{#4}}{#3{#5}}}
\newcommand\anappli[5][]{\anappe[#1]{#2}{#3_{#4}}{#3_{#5}}}
\newcommand\anappui[5][]{\anappe[#1]{#2}{#3^{#4}}{#3^{#5}}}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Binders}\label{sec:impl:bind}
%
% \subsection{Sharing}\label{sec:user:sharing}
% These macros are lifted from Bruce Miller's |latexml.sty|, we do not want the rest.  
% \begin{macro}{\LXMID}
%    \begin{macrocode}
\def\LXMID#1#2{\expandafter\gdef\csname xmarg#1\endcsname{#2}\csname xmarg#1\endcsname}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\LXMRef}
%    \begin{macrocode}
\def\LXMRef#1{\csname xmarg#1\endcsname}
%</package>
%    \end{macrocode}
% \end{macro}
% \Finale
\endinput
%
% Local Variables:
% mode: doctex
% TeX-master: t
% End:
%
%  LocalWords:  iffalse NeedsTeXFormat cmath cmath.dtx texttt presentation.dtx scsys sc
%  LocalWords:  sc mathml omdoc latexml cmathml activemath twintoo atwin atwintoo stex lt
%  LocalWords:  fileversion filedate maketitle symdef newpage setcounter tocdepth newpage
%  LocalWords:  tableofcontents ary widetilde cdot vname vname vnref vnname ednote livar
%  LocalWords:  livar ulivar ulivar primvar primvar pprimvar pprimvar ldots nappa nappa
%  LocalWords:  nappf nappf hline nappe eph varphi nappli nappli firstarg lastarg exfig
%  LocalWords:  compactenum printbibliography textsf langle rangle textsf langle rangle
%  LocalWords:  ltxml cvar newcommand ifx expandafter gdef csname endcsname ltx uivar leq
%  LocalWords:  napp nappui sequencefromto endinput seq

