\environment examples-style

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\registermathfunction[𝑓]
\registermathfunction[𝑔]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\definemathgroupset[demob][every]

% \setupnote[mathnote][location=page]
% \enabletrackers[math.textblobs]

\disabledirectives[structures.tags.shipout]

\setuptagging[state=start]

\enabledirectives[structures.tags.math.standalone]

\disabledirectives[structures.tags.math.strip]

\setmathgroupset[demob]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% \definefont[MyMathFont][MathRoman]

\definefontfallback
  [MonoX]
  [concrete-math.otf]
  [0x0000-0x1FFFF]

\definefontsynonym
  [MonoF]
% [name:lmmono10-regular.otf]
  [name:lmmonoprop10-regular.otf]
  [fallbacks=MonoX]

\definefont[MyMathFont][MonoF]

\setuptyping[style=MyMathFont]

\def\ExampleLanguages{\currentmainlanguage}
\def\ExampleLanguages{en,sv}

\setupframedtext
  [result]
  [align=flushleft]

\starttexdefinition protected ShowExampleMeaning #1
    \start
       \doif{#1}{\currentmainlanguage}{\darkblue}
       \gettextblob{#1}\currentmathblob
    \stop
    \par
\stoptexdefinition

\starttexdefinition protected StartExample
    \dostartbuffer[example][StartExample][StopExample]
\stoptexdefinition

\starttexdefinition protected StopExample
    \startresult[none]
        \getbuffer[example]
    \stopresult
    \vskip5mm
    \savemathexport[buffer=mmldata,xoption=ascii]
    \start
        \MyMathFont
        \processcommacommand[\ExampleLanguages]\ShowExampleMeaning
    \stop
    \start
%         \switchtobodyfont[6pt]
        \switchtobodyfont[7pt]
        \typebuffer[example][option=TEX]
        \scratchcounterone\nofbufferlines{mmldata}
        \ifnum\scratchcounterone>\zerocount
            \scratchcountertwo\maxbufferwidth{mmldata}
            \scratchcounterthree\ctxlua{context(
                \number\textwidth//(\number\scratchcountertwo*\number\emwidth)
            )}\relax % columns needed
            \scratchcounterfour\ctxlua{context(
                \number\textheight//\number\pagetotal
            )}\relax % available
            \ifnum\scratchcounterthree=\zerocount
                \typebuffer[mmldata][option=XML,n=\the\currentmathblob]%
            \orelse\ifnum\scratchcounterone>\scratchcounterfour
                \startcolumns[n=\scratchcounterthree,balance=no]
                    \typebuffer[mmldata][option=XML,n=\the\currentmathblob]%
                \stopcolumns
            \else
                \typebuffer[mmldata][option=XML,n=\the\currentmathblob]%
            \fi
        \fi
    \stop
    \page
\stoptexdefinition

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\startbuffer [colophon]

\startsubject[title=About this document]

This document is used by Mikael Sundqvist and Hans Hagen to check out how
well a formula translates to a verbose meaning. It's an experiment with
accessibility on the one hand but also a way to get documents validated.

This feature is only available in \CONTEXT\ \MKXL, aka \LMTX. You can enable
tracking in your document by for instance:

\starttyping[option=TEX]
\setuptagging
  [state=start]

\definemathgroupset
  [mydemogroup]
  [every]

\setmathgroupset
  [mydemogroup]

\setupnote
  [mathnote]
  [location=page]

\enabletrackers
  [math.textblobs]
\stoptyping

By default a \type {mathnote} is set up to be an endnote in which case you need
to place them with:

\starttyping[option=TEX]
\placenote[mathnode]
\stoptyping

\stopsubject
\stopbuffer

\startdocument
  [title={Meaningfull Math},
   author={Mikael Sundqvist & Hans Hagen}]

\StartExample
    \im {1 + 2 = 3}
\StopExample

\StartExample
    \im {1 - 2 = -1}
\StopExample

\StartExample
    \im {3^2 + 4^2 = 5^2}
\StopExample

\StartExample
    % Use structured input (see next two examples)
    \darkred \im {(1 + 2 + 3 + 4)^2 = 1^3 + 2^3 + 3^3 + 4^3}
\StopExample

\StartExample
    % Better, but next one is even better
    \im {\left(1 + 2 + 3 + 4 \right)^2 = 1^3 + 2^3 + 3^3 + 4^3}
\StopExample

\StartExample
    \im{\fenced[parenthesis]{1 + 2 + 3 + 4 }^2 = 1^3 + 2^3 + 3^3 + 4^3}
\StopExample

\StartExample
    % f and g are in this document registered as functions
    % The text can probably improved
    \darkred \im {abcdefghikl}
\StopExample

\StartExample
    % f and g are in this document registered as functions
    % The text can probably improved
    \darkred \im {xx \sin(x) x \frac{x}{x} x \sqrt{x} x \int x }
\StopExample

\StartExample
    % f is registered as a function
    % h is not
    % Do we need times here?
    \darkred \im {af(x) + bh(x) + f(x + b)}
\StopExample

\StartExample
    % Can we go shorter?
    \im {\binom{3}{2} = \frac{3!}{(3-2)!2!}}
\StopExample

\StartExample
    % Can we go shorter?
    \im {\binom{2n}{n + 1} = \frac{2n!}{(n - 1)!(n + 1)!}}
\StopExample

\StartExample
    \im {x + x^2 + x^3 + \ldots = x/(1 - x)}
\StopExample

\StartExample
    \im {\ee^{\pi \ii } = -1}
\StopExample

\StartExample
    % Do we need "times" before the \ee?
    \im {a + b \ii = \sqrt{a^2 + b^2}\ee^{\ii\arg(a + \ii b)}}
\StopExample

\StartExample
    \im {\conjugate{a + b \ii} = a - b \ii}
\StopExample

\StartExample
    % Maybe implies that
    \im {x^2 = -1 \implies x = \pm \ii}
\StopExample

\StartExample
    \im {x \neq x + -1}
\StopExample

\StartExample
    \dm {\frac{1}{2} = \frac{1}{3} + \frac{1}{6}}
\StopExample

\StartExample
    \dm {\frac{1}{x} + \frac{1}{y} = \frac{x + y}{xy}}
\StopExample

\StartExample
    \im {\sqrt{x} = x^{1/2} \neq x^{1/3} = \root[3]{x}}
\StopExample

\StartExample
    \dm {\sum_{n = 1}^{+\infty} \frac{1}{n^2} = \frac{\pi^2}{6}}
\StopExample

\StartExample
    \dm {\sum_{n \in \naturalnumbers} \frac{1}{n^2} = \frac{\pi^2}{6}}
\StopExample

\StartExample
    \dm {\sin x = \prod_{n = 1}^{+\infty} \left(1 - \frac{x^2}{\pi^2n^2}\right)}
\StopExample

\StartExample
    \dm {\int_{a}^{b} f'(x) \dd x = f(b) - f(a)}
\StopExample

\StartExample
    \dm {\int_{x=a}^{b} f'(x) \dd x = f(b) - f(a)}
\StopExample

\StartExample
    % Why no pause in this one (as in the \sum_{n \in \naturalnumbers} ...)?
    \darkred \dm {\int_{\Omega} f \dd \mu = 0}
\StopExample

\StartExample
    \im {\naturalnumbers \subset \integers \subset \rationals \subset \reals \subset \complexes}
\StopExample

\StartExample
    % Here we do not want of
    \darkred \im {f \maps \reals \to \reals}
\StopExample

\StartExample
    \im {\sin \maps \reals \to \reals}
\StopExample

\StartExample
    \im {\sin \mapsas x \mapsto \sin(x)}
\StopExample

\StartExample
    % Also here, we want no "of".
    \im {f \mapsas x \mapsto x + \exp(x)}
\StopExample

\StartExample
    \im {x \mapsto \ln(x)}
\StopExample

\StartExample
    \im {\sin x = \sin(x) \neq \sin(x) + 1 \neq \sin(x + 1)}
\StopExample

\StartExample
    \im {\lim_{k \tendsto +\infty} a_{k}}
\StopExample

\StartExample
    \im {\lim a_{k} = -\infty}
\StopExample

\StartExample
    % Should be two formulas, but in this document we only show the last one
    \im {f(x) \tendsto A \mtext{ as } x \tendsto a}
\StopExample

\StartExample
    \dm {\lim_{x \tendsto 0} \frac{\sin x}{x}}
\StopExample

\StartExample
    \dm {\lim_{f(x) \tendsto 0} g(x)}
\StopExample

\StartExample
    % Do we want "The function" here?
    \im {f'(x) + f''(x) + f'''(x) + f''''(x)}
\StopExample

\StartExample
    \im {h' + h'' + h''' + h''''}
\StopExample

\StartExample
\im { \secondderivative{f} = f'' }
\StopExample

\StartExample
    % These need to be checked
    \darkred \im {f_1'(x) + f_1^2'(x) + (f_1^2)'(x) }
\StopExample

\StartExample
    \darkred \im {(f_1)^2 \neq f_1^2 }
\StopExample

\StartExample
    \darkred \im {(x_1)^2 \neq x_1^2 }
\StopExample

\StartExample
    % Order issue
    \darkred \im {\Gamma_1^2_3^4}
\StopExample

\StartExample
    % Order issue
    \darkred \im {\tuple{x^1,x^2,x^3}}
\StopExample

\StartExample
    % Maybe times inbetween?
    \darkred \im {\adjoint{T}T = T\adjoint{T}}
\StopExample

\StartExample
    % Same comment as above
    \im {A \adj(A) = \det(A) I}
\StopExample

\StartExample
    \im {(u \convolve v) (x) \colonequals \int_{\reals} u(\xi) v(x - \xi) \dd \xi}
\StopExample

\StartExample
    \im {(f \convolve g) (x) \colonequals \int_{\reals} f(\xi) g(x - \xi) \dd \xi}
\StopExample

\StartExample
    \im {f = \sin}
\StopExample

\StartExample
    \im {\transpose{A} + \transpose{(A + B^2)} + \transpose{\left(A^2 + B\right)}}
\StopExample


% \StartExample
% \im { \left(f + g\right)\derivative (x) = f\derivative(x) + g\derivative(x) }
% \StopExample

% \StartExample
%     % "inverse image under f"
%     % f\preimage(A)
%     % f\inverse
%     \def\preimage{\primescript{-1}}
%     \im { f^{-1}(A) \neq  f\primescript{-1}(A) \neq f\preimage (A)}
% \StopExample

\StartExample
    \im {\sin''(x) = -\sin(x) = \sin(x + \pi)}
\StopExample

% f'(x), f\prime(x)  -> f prime of x
% f\derivative(x) -> derivative of f of x

\StartExample
    \dm {\frac{\dd u}{\dd t} = u'}
\StopExample

\StartExample
    \dm {\frac{\partial u}{\partial t} = c^2 \laplace u}
\StopExample

\StartExample
    \dm {\frac{\partial u}{\partial t} = c^2 \frac{\partial^2u}{\partial x^2}}
\StopExample

\StartExample
    \dm {\frac{\partial^3 u}{\partial x^2 \partial y}}
\StopExample

\StartExample
    % To be thought of
    \im {\bar{\partial} u = f}
\StopExample

\StartExample
    % Experimented with partial derivative d d x group u plus v end group...
    % but for accessibility reasons it is better to keep the partial
    \dm {\frac{\partial}{\partial x}(u + v) = \frac{\partial u}{\partial x} + \frac{\partial v}{\partial x}}
\StopExample

\StartExample
    \im {(1 - \laplace)u = f}
\StopExample

\StartExample\showmakeup[mathglue]
    \im {\laplace = \gradient \scalarproduct \gradient = \gradient^2 = \nabla \scalarproduct \nabla}
\StopExample

\StartExample
    \im {\gradient \crossproduct \gradient}
\StopExample

\StartExample
    \im {\floor{3.6} = \ceiling{2.7} = \integerpart{3.2}}
\StopExample

\StartExample
    \im {A = \set[size=1]{1, 2, 3}}
\StopExample

\StartExample
    \im {A = \tuple{1, 2, 3}}
\StopExample

\StartExample
    \im {\innerproduct{u \fence v} = \conjugate{\innerproduct{v \fence u}}}
\StopExample

\StartExample
    \im {\reals_{+} \colonequals \set{x \fence x \in \reals \land x > 0}}
\StopExample

\StartExample
    % Do not use unstructured input
    % Possible: (), (], [), [], ][, ]], [[, -> structure via lua?
    % To Mikael: Fix the \times
    \im {(a,b]^2 + \neq \leftopeninterval{a,b} \times \leftopeninterval{a,b} \neq ]a,(b + 1)]}
\StopExample

\StartExample
    % Check leading space (book page 57)
    \im {\abs{a_{n_k} - A} < \epsilon }
\StopExample

\definemathfence[parentes][parenthesis][define=yes]
% 2347 in the book
\StartExample
    \startformula
        u(b)-u(a)=\lim_{n\to+\infty} \parentes{f(x_1)\Delta x_1+f(x_2)\Delta x_2+\cdots+f(x_n)\Delta x_n}.
    \stopformula
\StopExample

\StartExample
    \im {f(x) = x^2 \mtp{,} x \in \reals}
\StopExample

\StartExample
    \im {\closure{\openinterval{a,b}} = \closedinterval{a,b}}
\StopExample

\StartExample
    \im {\closure{\varopeninterval{0,1}} = \closedinterval{0,1}}
\StopExample

\StartExample
    \im {\abs{x + y} \leq \abs{x} + \abs{y}}
\StopExample

\StartExample
    \im {\norm{x + y} \leq \norm{x} + \norm{y}}
\StopExample

\StartExample
    \im {\norm{\alpha x} = \abs{\alpha} \norm{x}}
\StopExample

\StartExample
    \darkred
    \im {\lnot(P \lor Q) = (\lnot P) \land (\lnot Q)}
\StopExample

\StartExample
    \im {\neg(P \land Q) \iff (\lnot P) \lor (\lnot Q)}
\StopExample

\StartExample
    % times between forall and x
    \darkred \im {(\forall x \in \reals)(x > 0 \lor x = 0 \lor x < 0)}
\StopExample

\StartExample
    \dm {f(x) =
        \startcases
            \NC x  \NC x > 0 \NR
            \NC -x \NC x < 0 \NR
        \stopcases}
\StopExample


\StartExample
    \dm{f(x) =
        \startcases[lefttext=\mtp{,}]
            \NC x  \NC x > 0 \NR
            \NC -x \NC x < 0 \NR
        \stopcases}
\StopExample

\StartExample
    \dm{f(x) =
        \startcases[righttext=\mtext{if }]
            \NC x  \NC x > 0 \NR
            \NC -x \NC x < 0 \NR
        \stopcases}
\StopExample

\stopdocument
