[Tex/LaTex] Create a Handwriting environment for both text and Math

environmentsfontsmath-modetext;

I would like to create an environment that would allow me to have parts of a document in "Handwriting mode".

So the objective would be to be able to type the following in the source code :

\begin{Handwriting}

This text should look like it is Hand written


$This \cdot equation = \ as \times \ well$

\end{Handwriting}

from another thread I tried

\usepackage{mathpazo}
\usepackage[version=3]{mhchem}

\usepackage{amsmath}
\usepackage{siunitx}
\newenvironment{Handwriting}{\fontfamily{augie}\selectfont}{\par}

but this does not help for the equation.

I suspect the use of the mathastext package could help but I did not figure out how yet.

Bonus question that makes matters a bit more complicated is : would it be possible to use the "Chalkduster" font instead of "augie" ?

Thank you for you help,
Clément

Best Answer

Example with mathastext:

\documentclass{article}

\usepackage{mathpazo}
\usepackage[version=3]{mhchem}
\usepackage{amsmath}
\usepackage{siunitx}
\usepackage[subdued]{mathastext}% no italic for Augie anyhow
\MTfamily{augie}
\Mathastext[augie]

\newenvironment{Handwriting}{\MTversion{augie}}{\par}

\begin{document}

This test is normal, and math too ($x^n+y^n = z^n$).

\begin{Handwriting}

This text should look like it is Hand written.

This equation as well:
\[x^n + y^n = z^n\]

\end{Handwriting}

\end{document}

enter image description here


Here is with Chalkduster, hence Unicode engines, which is not at all mathastext ballpark.

I added defaultmathsizes option to keep standard sizes for scriptsize (even if mathastext is "subdued", without this option it will make use of larger size in subscripts and superscripts).

Caution: you probably want to use mathspec or unicode-math which should provide the needed things. I have little experience with them (I rarely use unicode engines). Notice that user level interface is very often in LaTeX2e made "preamble-only", for example \DeclareMathSymbol macro, hence one has to use TeX engine primitives.

\documentclass{article}
\usepackage[no-math]{fontspec}
\usepackage{amsmath}
%\usepackage{siunitx}

\newfontfamily\Chalkduster[NFSSFamily=Chalkduster]{Chalkduster}
\usepackage[subdued, defaultmathsizes]{mathastext}
\MTfamily{Chalkduster}
\Mathastext[Chalkduster]

\newenvironment{Handwriting}{\MTversion{Chalkduster}\MTdonotfixfonts
  % adjust some additional glyphs
  \Umathchardef\prod 1 \symmtoperatorfont `∏\relax % mathop
  \Umathchardef\sum  1 \symmtoperatorfont `∑\relax % mathop
%  \Umathchardef\in   3 \symmtoperatorfont `∈\relax % not in CHALKDUSTER?
  \Umathchardef\int  1 \symmtoperatorfont `∫\relax % mathop
  \Umathchardef\neq  3 \symmtoperatorfont `≠\relax % mathrel
%
% This syntax works with luatex not with xelatex
  % \Umathcodenum`∏=\prod
  % \Umathcodenum`∑=\sum
  % \Umathcodenum`∫=\int
  % \Umathcodenum`≠=\neq
% So we repeat
  \Umathcode`∏ = 1 \symmtoperatorfont `∏\relax % mathop
  \Umathcode`∑ = 1 \symmtoperatorfont `∑\relax % mathop
  \Umathcode`∫ = 1 \symmtoperatorfont `∫\relax % mathop
  \Umathcode`≠ = 3 \symmtoperatorfont `≠\relax % mathrel
}{\par}

\begin{document}

This text is normal, and math too
\[x^n + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p\in P} (1 - \frac1p) =
  \int \sqrt{1+x^2} dx\]

\begin{Handwriting}

This text should look like it is Hand written.

These equations as well:
\[x^n + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p\in P} (1 - \frac1p) =
  \int \sqrt{1+x^2} dx\]
\[x^n + y^n = (z^n - w^n) = ∑_{ij} a_{ij} ≠ ∏_{p\in P} (1 - \frac1p) =
  ∫\sqrt{1+x^2} dx\]


\end{Handwriting}

\end{document}
% Local variables:
% TeX-engine: xetex
% End:

The above would produce strange output when compiled with lualatex if the \MTdonotfixfonts were omitted (with xelatex, this macro does nothing). Perhaps the \MTfixmathfonts macro dating back to 2016/05/03 of mathastext is obsoleted due to change with font handling on lua side. (untested, I don't use LuaTeX)

enter image description here

As one can see, the square root sign was left untouched (I guess one needs a genuine OpenType math font for all such extensible symbols). And the ELEMENT OF seems to be missing from Chalkduster. (it seems to have glyphs in a private area I don't know how to access)

Attention to no-math option for fontspec. (I vaguely remember polyglossia loads fontspec so this many need in that case some \PassOptionsToPackage right after \documentclass).