share
TeX - LaTeXCreate a Handwriting environment for both text and Math
[+21] [3] Clément Moissard
[2018-10-02 10:20:44]
[ math-mode fonts environments text ]
[ https://tex.stackexchange.com/questions/453429/create-a-handwriting-environment-for-both-text-and-math ]

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

Answer to the bonus question: Yes. If you use LuaLaTeX or XeLaTeX you can use any font installed on your computer. - Skillmon
(2) try out this and that examples. But changing the font to augie. Ok, I will make an answer. - user4686
jfbu, thank you for your answer. Though, I don't think it answers my question, since it changes the font for the entire document, which is not what I want. Skillmon, thank you, I just found it a bit difficult to use the \newfontfamily command right. I did the following : \newfontfamily{\Chalk}{[Chalkduster.ttf]} and then it was easy enough to use, somewhere in the document : {\Chalk This text is an imitation of a Chalkdust-written text } But as I tried to use the font in a new environment (my "Handwriting" environment for example), the compiling time started to take ages. - Clément Moissard
@ClémentMoissard I provided an answer with local changes only. You refer to my comment which indeed was for entire document. I had not really read your post then... - user4686
B and b almost look the same. It is very ugly font. - Display Name
Related, only insofar as look/placement of handwriting: tex.stackexchange.com/questions/317190/… - Steven B. Segletes
Thanks Steven, I was also looking for something like that =D - Clément Moissard
If you're fine with using unicode-math, then I would like to suggest Nagwa TK and Pennstander fonts: fonts.nagwa.com/tk, github.com/juliusross1/Pennstander. - Apoorv Potnis
[+27] [2018-10-02 10:51:30] user4686 [ACCEPTED]

Example with mathastext [1]:

\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).

[1] http://www.ctan.org/pkg/mathastext

Oh cool ! This is brilliant ! Thank you a lot jfbu =) - Clément Moissard
@ClémentMoissard glad it helps. I realize that compared to French Cursive the Augie font has glyphs for +, -, =, which is satisfying here. As explained by mathastext doc, big symbols however (sums, products, integrals) of course remain the ones specified by other packages. - user4686
Yep, that is why I am trying to get the Chalkduster to work (which does have cool big symbols), but still without success. Quite frustratingly, when I add \usepackage{fontspec} \newfontfamily{\Chalk}{[Chalkduster.ttf]} just at the beginning of your code, even the augie formatting disappears - Clément Moissard
Just to really push the idea, I found that it is quite easy to add a background color to the environment, for example : \definecolor{floralwhite}{rgb}{1.0, 0.98, 0.94} \newenvironment{Handwriting} {\MTversion{augie} \begin{mdframed}[backgroundcolor=floralwhite]} {\par \end{mdframed}} But would it be possible to add lines ? The idea would be for the text to look as if it was handwritten on a school-type sheet of paper, with lines. tex.stackexchange.com/questions/188164/… answers for the full document - Clément Moissard
grid typesetting is a challenge in LaTeX. Perhaps look at page 131 of tcolorbox manual, but the examples given clearly show no attempt is made at grid typesetting, only help lines. If you are sure your environment will contain only regular text and math displays, then surely one can do something, although breaking over page will be a problem I expect, perhaps ask the question to tcolorbox author. - user4686
Your answer keeps getting better ! Thank you so much for your help. I can't make it work though. The compilation takes ages and then freezes, would you happen to have any idea why ? Also, do you need the Chalkduster.ttf file in the compilation directory ? I don't understand where your code refers to it - Clément Moissard
Chalkduster is a system font on my macos x, fontspec instructs xelatex (which I use for compiling second example) to find it. I tell fontspec to use the "Chalkduster" name for the LaTeX NFSS font interface (as used in pdflatex) which is expected by mathastext (else it would I think use something like "Chalkduster(0)".) Anyway, if compilation hangs try to reduce to minimal example, perhaps not using mathastext exhibiting problem and then ask another question... - user4686
Coming back a year later... Your augie example works perfectly well, and I've been using it since then. Now I would like to try and change the font, (for "all pony", or anything that looks reasonably close to it), but I'm finding myself unable to do so. Help ? =) - Clément Moissard
Hi guys, would it be possible to define all greek and Greek at once? instead of doing one letter at a time with: \Umathchardef\mu 1 \symmtoperatorfont μ\relax % greek` \Umathchardef\sigma 1 \symmtoperatorfont σ\relax % greek` \Umathchardef\gamma 1 \symmtoperatorfont γ\relax % greek` etc... - JPMD
1
[+7] [2018-10-02 16:29:04] Davislor

Here is a sample with unicode-math. This would work with any TrueType or OpenType font. [1]

\documentclass[varwidth, preview]{standalone}
\usepackage[math-style=upright]{unicode-math}

\defaultfontfeatures{Scale=MatchUppercase}
% Kalam is a free font by the Indian Type Foundry, available at:
% https://github.com/itfoundry/kalam
\setmainfont{Kalam}[
  Scale = 1.0 ,
  UprightFont = *-Regular ,
  BoldFont = *-Bold ,
  Extension = .ttf ]
\setmathfont{GFS Neohellenic Math}
\setmathfont[range={"00-"FF,
                    "03C0, "2013-"2014, "2018-"201A, "201C-"201E, "2021-"2022,
                    "2026, "2030, "2039-"203A, "2044, "20AC, "20BA, "20BD,
                    "2113, "2122, "2126, "212E, "2202, "2206, "220F, "2211,
                    "2212, "2215, "221E, "222B, "2246, "2260, "2264, "2265,
                    "25CA, up/{Latin,latin,num}}]{Kalam-Regular.ttf}
\setmathfont[range=bfup/{Latin,latin,num}]{Kalam-Bold.ttf}

\begin{document}

Kalam is a free font from the Indian Type Foundry.

\[ \lim_{t \to \infty} \frac{\partial}{\partial t}
   \int_0^{2 \muppi} \frac{t^2}{2} \mathop{\symup{d}t} \leq
   \sum_{i=1}^N  \frac{\muppi i}{\sqrt 2} \approx \increment \symbfup{v}t \]

\end{document}

Kalam Sample

Unfortunately, that shows off almost all the font’s mathematical repertoire. Another you might try, from the same source, is Tillana.

ETA

Apologies; you requested an environment, and this doesn’t set up an environment. Sorry about that.

In a future version of unicode-math, you might be able to write:

\newfontfamily\handwritingfamily{Kalam}[
  Scale = MatchUppercase ,
  UprightFont = *-Regular ,
  BoldFont = *-Bold ,
  Extension = .ttf ]
\setmathfont[version=handwriting,
             Scale=MatchUppercase]{GFS Neohellenic Math}
\setmathfont[version=handwriting,
             Scale=MatchUppercase,
             range={"00-"FF,
                    "03C0, "2013-"2014, "2018-"201A, "201C-"201E, "2021-"2022,
                    "2026, "2030, "2039-"203A, "2044, "20AC, "20BA, "20BD,
                    "2113, "2122, "2126, "212E, "2202, "2206, "220F, "2211,
                    "2212, "2215, "221E, "222B, "2246, "2260, "2264, "2265,
                    "25CA, up/{Latin,latin,num}}
            ]{Kalam-Regular.ttf}
\setmathfont[version=handwriting,
             Scale=MatchUppercase,
             range=bfup/{Latin,latin,num}
            ]{Kalam-Bold.ttf}

\newenvironment{handwriting}{\handwritingfamily%
\mathversion{handwriting%
\setoperatorfont{\handwritingfamily}}}

However, as of October 2018, the manual warns, “Note there are currently open issues regarding the interaction between the version and the range features, so please proceed with caution.”

At present, \setmathfont[ version=handwriting, Scale=MatchUppercase ]{Kalam} compiles, but the output will lack any math symbols missing from the font (and I am not aware of any handwriting fonts with an OpenType MATH table). GFS Neohellenic Math is the most handwriting-like OpenType math font I know of.

[1] https://www.fontsquirrel.com/fonts/list/classification/handdrawn

back into 2021, any news of the version and range features compatibility? Thanks! - JPMD
@JPMD The 2021 user manual, I’m afraid, still warns, “Note there are currently open issues regarding the interaction between the version and the range features, so please proceed with caution.” - Davislor
Yes, you are correct. I was hoping the developer of unicode-math could meanwhile find a solution... any other ideas? - JPMD
According to @Ulrike Fischer this is unlikely. See tex.stackexchange.com/a/327944/74131 - Wamseln
2
[+2] [2025-03-05 14:54:32] Wamseln

Since @Davislor's solution for the in-document command is not functioning and is unlikely to work in the future (see Ulrike Fischers answer to another question [1]), I attempted to adapt @Davislor's answer to work with \immediate\write, \write18 and --shell-escape.

This approach produces the desired result: Screenshot showing the normal font togther with the handwritten font in one document

To achieve this, we define a new counter and a new environment in the main document:

% Counter for unique filenames
\newcounter{handwritingfontenv}
\setcounter{handwritingfontenv}{0}

% Environment gets written to a file and then included as graphic
\NewDocumentEnvironment{handwriting}{o +b}{%
    % #1: additional preambel settings for the standalone file
    \stepcounter{handwritingfontenv}%
    \newwrite\tempfile
    \immediate\openout\tempfile=handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex
    \immediate\write\tempfile{
        \noexpand\input{handwritingfonts-preamble}
        \IfNoValueF{#1}{
            \unexpanded{#1}
        }
        \noexpand\begin{document}
        \unexpanded{#2}
        \noexpand\end{document}
    }
    \immediate\closeout\tempfile
    \immediate\write18{lualatex --interaction=batchmode handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex}
    \includegraphics{handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.pdf}
}

This allows us to use @Davislor's working answer while incorporating handwriting within a document that uses different text and math fonts. The font (and other) settings of the handwriting environment should be stored inside the file handwritingfonts-preamble.

Here's what mine looks like:

% handwritingfonts-preamble.tex
\documentclass[varwidth, preview]{standalone}
\usepackage{amsmath}
\usepackage{siunitx}
\sisetup{
    mode=text,
    unit-mode=text,
    reset-text-family=false
}

\usepackage{fontspec}
\usepackage[math-style=upright]{unicode-math}

\usepackage[h]{esvect}

\defaultfontfeatures{Scale=1.1}
% Kalam is a free font by the Indian Type Foundry, available at:
% https://github.com/itfoundry/kalam
\usepackage{mathfont}
\mathfont{Kalam}
\setmainfont{Kalam}[
  Scale = MatchUppercase ,
  UprightFont = *-Regular ,
  BoldFont = *-Bold,
  LetterSpace=2,
  Scale=1.1]
\setmathfont{GFS Neohellenic Math}
\setmathfont[range={"00-"FF,
                    "03C0, "2013-"2014, "2018-"201A, "201C-"201E, "2021-"2022,
                    "2026, "2030, "2039-"203A, "2044, "20AC, "20BA, "20BD,
                    "2113, "2122, "2126, "212E, "2202, "2206, "220F, "2211,
                    "2212, "2215, "221E, "222B, "2246, "2260, "2264, "2265,
                    "25CA, up/{Latin,latin,num}}
            ]{Kalam-Regular}
\setmathfont[range=bfup/{Latin,latin,num}
            ]{Kalam-Bold}
\setmathrm{Kalam-Regular}

Putting it all together:

\documentclass{article}
\usepackage{standalone}
\usepackage{graphicx}
\usepackage{parskip}

\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{siunitx}

% For correct vector length
\usepackage[h]{esvect}

% font family definitions
\setmainfont{Lato}[
    Extension = .ttf,
    UprightFont = *-Regular,
    BoldFont = *-Black,
    ItalicFont = *-Italic,
    BoldItalicFont = *-BlackItalic,
    LetterSpace=2,
    Scale=1.1
]

\RequirePackage{unicode-math}
\setmathfont{LeteSansMath}
\RequirePackage{realscripts}

% Counter for unique filenames
\newcounter{handwritingfontenv}
\setcounter{handwritingfontenv}{0}

% Environment gets written to a file and then included as graphic
\NewDocumentEnvironment{handwriting}{o +b}{%
    % #1: additional preambel settings for the standalone file
    \stepcounter{handwritingfontenv}%
    \newwrite\tempfile
    \immediate\openout\tempfile=handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex
    \immediate\write\tempfile{
        \noexpand\input{handwritingfonts-preamble}
        \IfNoValueF{#1}{
            \unexpanded{#1}
        }
        \noexpand\begin{document}
        \unexpanded{#2}
        \noexpand\end{document}
    }
    \immediate\closeout\tempfile
    \immediate\write18{lualatex --interaction=batchmode handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex}
    \includegraphics{handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.pdf}
}


\begin{document}
    
    This text is normal, and math too. It has german Ümläütß. This is a greek math test: \(\alpha β \gamma δ\)
    
    These equations as are normal too:
    \[
        x + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p \epsilon P} \left(1 - \frac1p \right) = \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
    \]
    
    \[
        \lim_{t \to \infty} \frac{\partial}{\partial t}
        \int_0^{2 \muppi} \frac{t^2}{2} \mathop{\symup{d}t} \leq
        \sum_{i=1}^N  \frac{\muppi i}{\sqrt 2} \approx \increment \symbfup{v}t
    \]
    
    This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.

    \vspace{1em}
    
    \begin{handwriting}
        This text is Hand written, and math too. It has german Ümläütß. This is a greek math test: \(\alpha β \gamma δ\)
        
        These equations as well:
        \[
            x + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p \epsilon P} \left(1 - \frac1p \right) = \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
        \]
        
        \[
            \lim_{t \to \infty} \frac{\partial}{\partial t}
            \int_0^{2 \muppi} \frac{t^2}{2} \mathop{\symup{d}t} \leq
            \sum_{i=1}^N  \frac{\muppi i}{\sqrt 2} \approx \increment \symbfup{v}t
        \]
        
        \[
            \sin(\alpha_k) = \frac{\lambda}{b}
        \]
        
        This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.
    \end{handwriting}
    
    \vspace{1em}
    
    This text and math should be normal again: 
    
    \[
        x^n + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p\in P} \left(1 - \frac1p \right) = \int \sqrt{1+x^2} dx
    \]
    
     This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.

\end{document}

This setup yields the output shown above.

Another example with the duckuments package for 1:1 comparison:

\documentclass{article}
\usepackage{standalone}
\usepackage{graphicx}
\usepackage{parskip}

\usepackage{fontspec}
\usepackage{amsmath}

% font family definitions
\setmainfont{Lato}[
    Extension = .ttf,
    UprightFont = *-Regular,
    BoldFont = *-Black,
    ItalicFont = *-Italic,
    BoldItalicFont = *-BlackItalic,
    LetterSpace=2,
    Scale=1.1
]

\RequirePackage{unicode-math}
\setmathfont{LeteSansMath}
\RequirePackage{realscripts}

% Counter for unique filenames
\newcounter{handwritingfontenv}
\setcounter{handwritingfontenv}{0}

% Environment gets written to a file and then included as graphic
\NewDocumentEnvironment{handwriting}{o +b}{%
    % #1: additional preambel settings for the standalone file
    \stepcounter{handwritingfontenv}%
    \newwrite\tempfile
    \immediate\openout\tempfile=handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex
    \immediate\write\tempfile{
        \noexpand\input{handwritingfonts-preamble}
        \IfNoValueF{#1}{
            \unexpanded{#1}
        }
        \noexpand\begin{document}
        \unexpanded{#2}
        \noexpand\end{document}
    }
    \immediate\closeout\tempfile
    \immediate\write18{lualatex --interaction=batchmode handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.tex}
    \includegraphics{handwritingfontenv-\detokenize\expandafter{\jobname}-\thehandwritingfontenv.pdf}
}

\usepackage{duckuments}


\begin{document}
    
    \blindduck[maths=both]
    
    \begin{handwriting}[\usepackage{duckuments}]
        \blindduck[maths=both]
    \end{handwriting}

\end{document}

This produces: enter image description here

Sadly the equation numbering has the wrong font, but I am at loss here for the moment.

EDIT

I made some stupid mistakes in the last version of my answer. Most importantly i didn't use \setmainfont in the standalone preamble, which was causing the number of the equation numbering to be off and made it necessary to redefine all math operators.

Also I found out that it works loading the mathfont package together with unicode-math. mathfont makes it possible for some math characters like ( and ) but also \sum, \prod and \int to be resized. That has the side effect of them becoming way to bold. But I like the outcome better this way.

EDIT 2

The environment above still needs the separate preamble and most importantly: The standalone documents are not aware of the \linewidth and font size. So I edited the environment to pass the fontsize and linewidth to the standalone. Also I added some lua code to have all the standalones in a separate subfolder and to check if the pdfs are present so they only get compiled, if they are not already there. This saves a lot of time, but also means that the standalone pdf files have to be deleted by hand, when changes are made to the content of the handwriting environment. This can be done by deleting the subfolder.

\documentclass[a4paper,11pt]{article}
\usepackage{standalone}
\usepackage{graphicx}
\usepackage{parskip}

\usepackage{geometry}
\geometry{margin={2.4cm,2.4cm}}

\usepackage{fontspec}
\usepackage{amsmath}
\usepackage{siunitx}

% For correct vector length
\usepackage[h]{esvect}

% font family definitions
\setmainfont{Lato}[
    Extension = .ttf,
    UprightFont = *-Regular,
    BoldFont = *-Black,
    ItalicFont = *-Italic,
    BoldItalicFont = *-BlackItalic,
    LetterSpace=2,
    Scale=1.1
]

\usepackage{unicode-math}
\setmathfont{LeteSansMath}
\usepackage{realscripts}

\newcommand{\handwritingpkgs}{%
    \unexpanded{%
        \usepackage{parskip}
        \usepackage{amsmath}
        \usepackage{siunitx}
        \sisetup{%
            mode=text,
            unit-mode=text,
            reset-text-family=false
        }
        \usepackage{fontspec}
        % Kalam is a free font by the Indian Type Foundry, available at:
        % https://github.com/itfoundry/kalam
        \usepackage{mathfont}
        \mathfont{Kalam}
        \usepackage[math-style=upright]{unicode-math}
        \usepackage[h]{esvect}
        \defaultfontfeatures{Scale=1.1}
        \setmainfont{Kalam}[
          Scale = MatchUppercase ,
          UprightFont = *-Regular ,
          BoldFont = *-Bold,
          LetterSpace=2,
          Scale=1.1]
        \setmathfont{GFS Neohellenic Math}
        \setmathfont[range={"00-"FF,
                            "03C0, "2013-"2014, "2018-"201A, "201C-"201E, "2021-"2022,
                            "2026, "2030, "2039-"203A, "2044, "20AC, "20BA, "20BD,
                            "2113, "2122, "2126, "212E, "2202, "2206, "220F, "2211,
                            "2212, "2215, "221E, "222B, "2246, "2260, "2264, "2265,
                            "25CA, up/{Latin,latin,num}}
                    ]{Kalam-Regular}
        \setmathfont[range=bfup/{Latin,latin,num}
                    ]{Kalam-Bold}
        \setmathrm{Kalam-Regular}
    }
}

% Counter for unique filenames
\newcounter{handwritingfontenv}
\setcounter{handwritingfontenv}{0}

% For checking if the files exist
\directlua{lfs = require "lfs"}
\newif\ifpdffileexists

% Create the output  directory, if needed
\directlua{
    lfs = require "lfs"
    function ensure_dir_exists(dir)
        if not lfs.isdir(dir) then
            lfs.mkdir(dir)
        end
    end
}
\edef\outputdir{handwriting}  % Define output directory

\NewDocumentEnvironment{handwriting}{o +b}{%
    \stepcounter{handwritingfontenv}%
    % Create output directory if needed
    \directlua{ensure_dir_exists("\outputdir")}%
    % Define filenames with path
    \edef\filenamebase{\detokenize\expandafter{\jobname}-handwritingfontenv-\thehandwritingfontenv}%
    \edef\pdffilename{\outputdir/\filenamebase.pdf}%
    % Check if PDF exists
    \directlua{
        if lfs.attributes("\pdffilename") then
            tex.sprint("\\pdffileexiststrue")
        else
            tex.sprint("\\pdffileexistsfalse")
        end
    }%
    \ifpdffileexists%
        % Skip compilation if PDF exists
    \else%
        \newwrite\tempfile%
        \immediate\openout\tempfile=\outputdir/\filenamebase.tex%
        \immediate\write\tempfile{%
            \noexpand\documentclass[varwidth=\the\linewidth, preview, \f@size pt]{standalone}%
            \handwritingpkgs%
            \IfNoValueF{#1}{\unexpanded{#1}}%
            \noexpand\begin{document}%
            \unexpanded{#2}%
            \noexpand\end{document}%
        }%
        \immediate\closeout\tempfile%
        % Compile with output directory
        \immediate\write18{
            cd \outputdir && lualatex --interaction=batchmode \filenamebase.tex && cd ..
        }%
    \fi%
    % Include PDF from subdirectory
    \includegraphics{\pdffilename}%
}{}

\begin{document}
    
    This text is normal, and math too. It has german Ümläütß. This is a greek math test: \(\alpha β \gamma δ\)
    
    These equations as are normal too:
    \[
        x + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p \epsilon P} \left(1 - \frac1p \right) = \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
    \]
    
    \[
        \lim_{t \to \infty} \frac{\partial}{\partial t}
        \int_0^{2 \muppi} \frac{t^2}{2} \mathop{\symup{d}t} \leq
        \sum_{i=1}^N  \frac{\muppi i}{\sqrt 2} \approx \increment \symbfup{v}t
    \]
    
    This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.

    \vspace{1em}
    
    \begin{handwriting}
        This text is Hand written, and math too. It has german Ümläütß. This is a greek math test: \(\alpha β \gamma δ\)
        
        These equations as well:
        \[
            x + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p \epsilon P} \left(1 - \frac1p \right) = \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
        \]
        
        \[
            \lim_{t \to \infty} \frac{\partial}{\partial t}
            \int_0^{2 \muppi} \frac{t^2}{2} \mathop{\symup{d}t} \leq
            \sum_{i=1}^N  \frac{\muppi i}{\sqrt 2} \approx \increment \symbfup{v}t
        \]
        
        \[
            \sin(\alpha_k) = \frac{\lambda}{b}
        \]
        
        This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.
    \end{handwriting}
    
    \vspace{1em}
    
    This text and math should be normal again: 
    
    \[
        x^n + y^n = (z^n - w^n) = \sum_{ij} a_{ij} \neq \prod_{p\in P} \left(1 - \frac1p \right) = \int \sqrt{1+x^2} dx
    \]
    
     This is an inline math example: \(\vv{F} = m \cdot a  = \qty{12}{\newton}\) some text after.

\end{document}

Which now correctly produces: The complete page layout with handwritten and non handwritten text.

One problem still is, that the compilation failes, if compiled for the first time. But subsequend compilations work without errors. (only with --shell-escape though.)

[1] https://tex.stackexchange.com/a/327944/74131

You could replace \NewEnviron{handwriting}{...\BODY...} by \NewDocumentEnvironement{handwriting}{+b}{...#1...}{}. It's included in LaTeX kernel and need no package. See the user guide. - jlab
(1) Thank you @jlab I will change it right away - Wamseln
3