\documentclass{article}

\usepackage{luacas}
\usepackage{amsmath}
\usepackage{amssymb}

\usepackage[margin=1in]{geometry}
\usepackage[shortlabels]{enumitem}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{positioning,calc}
\usepackage{forest}
\usepackage{minted}
\usemintedstyle{pastie}
\usepackage[hidelinks]{hyperref}
\usepackage{parskip}
\usepackage{multicol}
\usepackage[most]{tcolorbox}
    \tcbuselibrary{xparse}
\usepackage{microtype}

\definecolor{rose}{RGB}{128,0,0}
\definecolor{roseyellow}{RGB}{222,205,99}
\definecolor{roseblue}{RGB}{167,188,214}
\definecolor{rosenavy}{RGB}{79,117,139}
\definecolor{roseorange}{RGB}{232,119,34}
\definecolor{rosegreen}{RGB}{61,68,30}
\definecolor{rosewhite}{RGB}{223,209,167}
\definecolor{rosebrown}{RGB}{108,87,27}
\definecolor{rosegray}{RGB}{84,88,90}

\usepackage[
backend=biber,
style=numeric,
]{biblatex}
\addbibresource{sources.bib}

\newtcolorbox{codebox}[1][sidebyside]{
    enhanced,skin=bicolor,
    #1,
    arc=1pt,
    colframe=brown,
    colback=brown!15,colbacklower=white,
    boxrule=1pt,
    notitle
}

\begin{document}

\subsection{Tutorial 1: Limit Definition of the Derivative}

Alice is teaching calculus, and she wants to give her students many examples of the dreaded \emph{limit definition of the derivative}. On the other hand, she'd like to avoid working out many examples by-hand. She decides to give \texttt{luacas} a try.

Alice can access the \texttt{luacas} program using a custom environment: \mintinline{latex}{\begin{CAS}..\end{CAS}}. The first thing Alice must do is declare variables that will be used going forward:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
\end{CAS}
\end{minted}
Alice decides that $f$, the function to be differentiated, should be $x^2$. So Alice makes this assignment with:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
\end{CAS}
\end{minted}
Now, Alice wants to use the variable $q$ to store the appropriate \emph{difference quotient} of $f$. Alice could hardcode this into $q$, but that seems to defeat the oft sought after goal of reusable code. So Alice decides to use the \texttt{substitute} command of \texttt{luacas}:
\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f) - f)/h
\end{CAS}
\end{minted}
Alice is curious to know if $q$ is what she thinks it is. So Alice decides to have \LaTeX{} print out the contents of $q$ within her document. For this, she uses the \mintinline{latex}{\print} command. 
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)- f)/h
\end{CAS}
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \[ \print{q} \] 
\end{codebox}
So far so good! Alice wants to expand the numerator of $q$; she finds the aptly named \texttt{expand} method helpful in this regard. Alice redefines \mintinline{lua}{q} to be \mintinline{lua}{q=expand(q)}, and prints the result to see if things worked as expected:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        q = expand(q)
    \end{CAS}
    \[ \print*{q} \] 
\end{codebox}
Alice is pleasantly surprised that the result of the expansion has been \emph{simplified}, i.e., the factors of $x^2$ and $-x^2$ cancelled each other out, and the resulting extra factor of $h$ has been cancelled out of the numerator and denominator.

Finally, Alice wants to take the limit as $h\to 0$. Now that our difference quotient has been expanded and simplified, this amounts to another substitution:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
    subs = {[h] = 0}
    q = substitute(subs,q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        subs = {[h]=0}
        q = q:substitute(subs)
    \end{CAS}
    \[ \print{q} \] 
\end{codebox}
Alice is slightly disappointed that $0+2x$ is returned and not $2x$. Alice takes a guess that there's a \mintinline{lua}{simplify} command. This does the trick: adding the line \mintinline{lua}{q = simplify(q)} before leaving the \texttt{CAS} environment returns the expected $2x$:
\begin{codebox}
    \begin{minted}[fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
    subs = {[x]=x+h}
    q = (substitute(subs,f)-f)/h
    q = expand(q)
    subs = {[h] = 0}
    q = substitute(subs,q)
    q = simplify(q)
\end{CAS}
\[ \print{q} \] 
    \end{minted}
    \tcblower
    \begin{CAS}
        q=simplify(q)
    \end{CAS}
    \[ \print{q} \] 
\end{codebox}

Alternatively, Alice could have used the \mintinline{latex}{\print*} command instead of \mintinline{latex}{\print} -- the essential difference is that \mintinline{latex}{\print*}, unlike \mintinline{latex}{\print}, automatically simplifies the content of the argument. 

Alice is pretty happy with how everything is working, but she wants to be able to typeset the individual steps of this process. Alice is therefore thrilled to learn that the \mintinline{latex}{\begin{CAS}..\end{CAS}} environment is very robust -- it can:
\begin{itemize}
    \item Be entered into and exited out of essentially anywhere within her \LaTeX{} document, for example, within \mintinline{latex}{\begin{aligned}..\end{aligned}}; and 
    \item CAS variables persist -- if Alice assigns \mintinline{lua}{f = x^2} within \mintinline{latex}{\begin{CAS}..\end{CAS}}, then the CAS remembers that \mintinline{lua}{f = x^2} the next time Alice enters the CAS environment. 
\end{itemize}
Here's Alice's completed code:
\begin{codebox}[frame hidden,breakable]
\begin{minted}[breaklines,fontsize=\small]{latex}
\begin{CAS}
    vars('x','h')
    f = x^2
\end{CAS}
Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using thelimit definition of the derivative. Toward that end, we start with the appopriatedifference quotient:
\begin{CAS}
    subs = {[x]=x+h}
    q = (substitute(subs,f) - f)/h
\end{CAS}
\[ \begin{aligned}
    \print{q} &= 
    \begin{CAS} 
        q = expand(q) 
    \end{CAS}
    \print{q}& &\text{expand/simplify} \\
    \begin{CAS}
        subs = {[h]=0}
        q = substitute(subs,q)
    \end{CAS}
    &\xrightarrow{h\to 0} \print{q}& &\text{take limit}\\ 
    &= 
    \begin{CAS}
        q = simplify(q)
    \end{CAS}
    \print{q} & &\text{simplify.}
\end{aligned} \] 
So $\print{diff(f,x)} = \print*{diff(f,x)}$. 
\end{minted}
\end{codebox}
    
Alice can produce another example merely by changing the definition of $f$ on the third line to another polynomial:

\begin{minted}{latex}
\begin{CAS}
    vars('x','h')
    f = 2*x^3-x
\end{CAS}
\end{minted}
And here is Alice's completed project:
\begin{tcolorbox}[colback=rose!10,
    colframe=rose,
    arc=1pt,
    frame hidden]
{\bf Tutorial 1:} {\itshape A limit definition of the derivative for Alice.}\vskip0.2cm

\begin{CAS}
vars('x','h')
f = 2*x^3-x
\end{CAS}
Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using the limit definition of the derivative. Toward that end, we start with the appropriate difference quotient:
\begin{CAS}
subs = {[x] = x+h}
q = (f:substitute(subs) - f)/h
\end{CAS}
\[ \begin{aligned}
\print{q} &= 
\begin{CAS} 
    q = expand(q)
\end{CAS}
\print{q}& &\text{expand/simplify} \\
\begin{CAS}
    subs = {[h]=0}
    q = q:substitute(subs)
\end{CAS}
&\xrightarrow{h\to 0} \print{q}& &\text{take limit} \\ 
&= 
\begin{CAS}
    q = simplify(q)
\end{CAS}
\print{q}& &\text{simplify.}
\end{aligned} \] 
%So $\print{diff(f,x)} = \print*{diff(f,x)}$.
\end{tcolorbox}

\end{document}