% !TeX program = lualatex
% =====================================================================
%  03-math.tex
%  A short maths course in scholatex: the inline mini-language, the
%  named helpers, operators with an index, integrals, trigonometry,
%  and the matrix / system blocks. Key formulas are framed in boxes.
%  lang=en here keeps the decimal point; set lang=fr for a comma.
% =====================================================================
\documentclass[
  margins=18,
  font=Latin Modern Roman,
  size=12,
  lang=en
]{scholatex}
\begin{document}

let title = <red b 18pt c>
let h1    = <navy b section>
let raw   = <gray i>

<title>scholatex — a little maths course

% =====================================================================
<h1>1 · The inline mini-language
% =====================================================================

Wrap maths in <tt>{$...$}. A few rewrites keep it light: <tt>{*} is a
product, <tt>{+-} a plus-or-minus, and <tt>{\<=} <tt>{\>=} <tt>{!=} the
three comparisons.

<box line:Navy fill:AliceBlue radius:3>{
	$a * b$, $x +- y$, $a <= b$, $c >= d$, $e != f$
}

Fractions use <tt>{/} and chain left to right, powers use <tt>{^},
indices use <tt>{_}:

<box line:Navy fill:AliceBlue radius:3>{
	$1/2$, $a/b/c$, $x^2 + y^2 = r^2$, $x_1 + x_2$
}

Roots and Greek names need no backslashes:

<box line:Navy fill:AliceBlue radius:3>{
	$sqrt(2)$, $sqrt(x^2 + y^2)$, $alpha + beta != gamma$, $pi >= 3$
}

% =====================================================================
<h1>2 · Interpolation inside maths
% =====================================================================

A value computed with <tt>{\#\{...\}} or <tt>{\#name} drops straight into
a formula, and follows the <tt>{lang} option so a typed and a computed
number always match.

let n = 7

<box line:Teal fill:MintCream radius:3>{
	$n = #n$, so $n^2 = #{n*n}$ and $n/2 = #{n/2}$
}

% =====================================================================
<h1>3 · Helpers: abs, norm, vec
% =====================================================================

In the spirit of <tt>{sqrt()}, three helpers wrap their argument:
<tt>{abs(x)} gives $abs(x)$, <tt>{norm(v)} gives $norm(v)$, and
<tt>{vec(AB)} draws a vector $vec(AB)$.

<box line:Teal fill:MintCream radius:3 title:{Vectors fall out for free}>{
	Chasles' relation $vec(AB) + vec(BC) = vec(AC)$, and the norm of a
	vector $norm(vec(AB))$, need nothing extra.
}

% =====================================================================
<h1>4 · Operators with an index
% =====================================================================

A big operator carries its index in <tt>{(...)}; its body follows freely
and is set in display style so fractions stay full size.

<box line:Indigo fill:Lavender radius:3 title:{Sum and product}>{
	$sum(i=1, n) i = n(n+1)/2$ \qquad $prod(k=1, n) k$
}

A limit's <tt>{(...)} holds the approach, written with <tt>{->}; the
target sits under the word, as it should:

<box line:Indigo fill:Lavender radius:3>{
	$lim(x->0) f(x)$ \qquad $lim(x->+inf) 1/x$
}

% =====================================================================
<h1>5 · Integrals: body and differential
% =====================================================================

An integral closes on a differential. Its head <tt>{(...)} names the
variable; everything up to the end of the formula is the integrand, and
<tt>{\,dx} is appended automatically.

<box line:DarkGreen fill:Honeydew radius:3 title:{Primitive and definite integral}>{
	$int(x) f(x)$ \qquad $int(x=a, b) f(x)$
}

The head's variable is the differential, so a change of letter is just a
change in the head: $int(t=0, 1) t^2$. To keep a term outside the
integral, close the integrand in parentheses — these differ:

<box line:DarkGreen fill:Honeydew radius:3>{
	$(int(x=a, b) f(x)) + 1$ \qquad $int(x=a, b) (f(x) + 1)$
}

% =====================================================================
<h1>6 · Multiple integrals
% =====================================================================

Separate several domains with <tt>{;} inside the head. The count of
domains chooses $\int$, $\iint$ or $\iiint$; the
differentials come out in reverse order, the Fubini convention.

<box line:DarkGreen fill:Honeydew radius:3>{
	$int(x=a, b ; y=c, d) f(x,y)$

	$int(x=a, b ; y=c, d ; z=e, g) f(x,y,z)$
}

A single named domain is a region integral over that set, with the area
element giving the surface form: $int(D) f$.

% =====================================================================
<h1>7 · Contour, principal value, average
% =====================================================================

Three named integral operators round out the family: a contour integral
$contourint(C) f(z)$, a Cauchy principal value $pvint(x=a, b) f(x)$, and
the average (normalised) integral $meanint(x=a, b) f(x)$.

<box line:DarkGreen fill:Honeydew radius:3 title:{The integral family}>{
	$contourint(C) f(z)$ \qquad $pvint(x=a, b) f(x)$ \qquad $meanint(x=a, b) f(x)$
}

% =====================================================================
<h1>8 · Trigonometry
% =====================================================================

Function names — <tt>{sin}, <tt>{cos}, <tt>{tan}, <tt>{ln}, <tt>{exp} and
the rest — are set upright automatically, and a name glued to <tt>{(...)}
takes its argument as one atom, so fractions behave.

<box line:Crimson fill:MistyRose radius:3 title:{The fundamental identity}>{
	$sin(x)^2 + cos(x)^2 = 1$
}

<box line:Crimson fill:MistyRose radius:3 title:{Addition formula}>{
	$cos(a+b) = cos(a)cos(b) - sin(a)sin(b)$
}

<box line:Crimson fill:MistyRose radius:3>{
	$tan(x) = sin(x)/cos(x)$ \qquad $lim(x->0) sin(x)/x = 1$
}

% =====================================================================
<h1>9 · Derivatives and differential equations
% =====================================================================

A derivative is written as the fraction it is. The differential <tt>{d} is
set upright (ISO 80000-2), matching the <tt>{d} of the integrals — but only
when both sides of the fraction carry it, so a variable named <tt>{d} is left
alone (<tt>{d/2} stays a plain fraction).

<box line:DarkOrange fill:OldLace radius:3 title:{Leibniz notation}>{
	$dy/dx$ \qquad $(d^2 y)/(dx^2)$ \qquad $d/dx (x^2) = 2x$
}

A first-order differential equation reads in one line:

<box line:DarkOrange fill:OldLace radius:3>{
	$dy/dx + y = 0$
}

Partial derivatives use <tt>{partial} ($\partial$); parenthesise each side so
the fraction groups correctly. The heat equation, for instance:

<box line:DarkOrange fill:OldLace radius:3 title:{The heat equation}>{
	$(partial u)/(partial t) = (partial^2 u)/(partial x^2)$
}

% =====================================================================
<h1>10 · Matrix blocks
% =====================================================================

A matrix is a block: one line is one row, <tt>{;} separates the entries.
Every cell goes through the mini-language, so $2x + 1$ or $1/2$ work
inside a cell.

<matrix>{
	1 ; 2 ; 3
	4 ; 5 ; 6
}

<raw>The raw form needs a <tt>{pmatrix} environment, an ampersand between
every entry and a double backslash at every row end.

The same block under two other names draws a determinant or square
brackets:

<det>{
	a ; b
	c ; d
}

<bmatrix>{
	2x + 1 ; 0
	0 ; 1/2
}

% =====================================================================
<h1>11 · Augmented matrix
% =====================================================================

A single <tt>{|} inside a row draws the augmentation bar at that column on
every row — how a linear system is set up for elimination:

<bmatrix>{
	2 ; 1 | 7
	1 ; -1 | 1
}

The bar is allowed on <tt>{\<matrix\>} and <tt>{\<bmatrix\>}, never on
<tt>{\<det\>}. A bar at a row edge, or misaligned across rows, raises a
clear <tt>{scholatex:} error naming the line.

% =====================================================================
<h1>12 · Systems of equations
% =====================================================================

A system aligns automatically on the first relational operator, so
equalities and inequalities mix freely. One equation per line, no
separator:

<system>{
	2x + 3y = 7
	x - y = 1
}

<system>{
	2x + 3 <= 7
	x >= 0
}

<violet b c>End of the demonstration.

\end{document}
