% !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,
  linespread=1.4,
  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>The inline mini-language
% =====================================================================

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

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

Fractions use / and chain left to right, powers use ^,
indices use _:

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

Powers and indices bind tighter than division, as in ordinary reading: a
script attaches to the atom right next to it (its base), never to the whole
fraction. So the same four letters give four different readings, no
parentheses needed except to lift a script onto the fraction itself:

<box line:Navy fill:AliceBlue radius:3>{
	$a^2/b$ — the power stays on $a$ (numerator)
	$a/b^2$ — the power stays on $b$ (denominator), so this is $a/(b^2)$
	$a^2/b^2$ — both in place
	$(a/b)^2$ — parentheses lift the power onto the whole fraction
}

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>Interpolation inside maths
% =====================================================================

A value computed with ##{{...}} or ##name drops straight into
a formula, and the boundary is exact: a letter touching the value is never
swallowed into it, so a coefficient glued to a variable reads just as written.
The value also follows the 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}$
}

<box line:Teal fill:MintCream radius:3 title:{Values touching symbols}>{
	$#{n}x$ is seven times $x$, and $#{n}x^2$ keeps its power.
	Two values at once: $#{n} + #{n*n} = #{n + n*n}$.
	A half two ways: $1/2 = #{1/2}$, alongside the literal $3.5$.
}

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

In the spirit of sqrt(), three helpers wrap their argument:
abs(x) gives $abs(x)$, norm(v) gives $norm(v)$, and
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>Operators with an index
% =====================================================================

A big operator carries its index in (...); 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$ <3tab> $prod(k=1, n) k$
}

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

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

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

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

<box line:DarkGreen fill:Honeydew radius:3 title:{Primitive and definite integral}>{
	$int(x) f(x)$ <3tab> $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$ <3tab> $int(x=a, b) (f(x) + 1)$
}

% =====================================================================
<h1>Multiple integrals
% =====================================================================

Separate several domains with ; 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>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)$ <3tab> $pvint(x=a, b) f(x)$ <3tab> $meanint(x=a, b) f(x)$
}

% =====================================================================
<h1>Trigonometry
% =====================================================================

Function names — sin, cos, tan, ln, exp and
the rest — are set upright automatically, and a name glued to (...)
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)$ <3tab> $lim(x->0) sin(x)/x = 1$
}

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

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

<box line:DarkOrange fill:OldLace radius:3 title:{Leibniz notation}>{
	$dy/dx$ <3tab> $(d^2 y)/(dx^2)$ <3tab> $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 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>Matrix blocks
% =====================================================================

A matrix is a block: one line is one row, ; 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 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>Augmented matrix
% =====================================================================

A single | 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 <<matrix>> and <<bmatrix>>, never on
<<det>>. A bar at a row edge, or misaligned across rows, raises a
clear scholatex: error naming the line.

% =====================================================================
<nextpage h1>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}
