Scratch - LaTeX

The LaTeX Scratch package [docs] provides a means for dislaying Scratch style programming blocks in a document.

Examples taken, with modification, from from the documentation.

In [3]:
%load_ext tikz_magic
In [4]:
%%tikz -p scratch --no-wrap -s 0.5
\begin{scratch}
\blockmove{Movement Block}
\blocklook{Appearance Block}
\blocksound{Sound Block}
\blockpen{Pen Block}
\blockvariable{Variable Block}
\blocklist{List Block}
\blockevent{Event Block}
\blockcontrol{Control Block}
\blocksensing{Sensor Block}
\end{scratch}
Out[4]:
In [5]:
%%tikz -p scratch --no-wrap -s 0.5
\begin{scratch}
\blockinit{When \greenflag is clicked}
\blockmove{Do something...}
\blockstop{Done}
\end{scratch}
Out[5]:
In [6]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blocklook{Say \txtbox{Hello} for \ovalnum{2} seconds}
\blocksensing{Ask \txtbox{What is your name ?} and wait}
\end{scratch}
Out[6]:
In [7]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blockinit{When I receive \selectmenu{message 1}}
\blockcontrol{Create a copy of \selectmenu{myself}}
3
\blocklook{Display colour \selectmenu{colour} \ovalnum{25}}

\blockpen{Use colour \squarecolor{red!75!black}}
\end{scratch}
Out[7]:
In [8]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blockmove{Move \ovalnum{10}}
\blockmove{Heading \ovalnum{180\selectarrownum}}
\blockmove{Turn \turnleft{} for \ovalnum{45} degrees}
\blockcontrol{Wait \ovalnum{1} second}
\end{scratch}
Out[8]:
In [10]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blockmove{Turn in \ovalmove{direction}}
\blocksound{Add \ovaloperator{\ovalnum{-0.5} * \ovalsound{volume}} to volume}
\blockmove{Move over \ovalsound{time} seconds to x: \ovalsensing{cursor x}
y: \ovaloperator{\ovalnum{4} * \ovalsensing{cursor y}}}
\end{scratch}
Out[10]:

Conditional Statements

In [11]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blockif{if \booloperator{\ovalmove{x} < \txtbox{7}} then}
{\blockmove{go to \selectmenu{first position}}
\blocklook{Change costume to \selectmenu{costume 4}}
}
\blockmove{move to \selectmenu{another position}}
\end{scratch}
Out[11]:
In [13]:
%%tikz -p scratch --no-wrap -s 0.5

\begin{scratch}
\blockifelse{if \boolsensing{colour \squarecolor{blue!65} detected ?} then}
{\blockmove{forward \ovalnum{2}}
\blocksound{play sound \selectmenu{meeow}}
}
{ \blockvariable{change \ovalvariable{total} to \ovaloperator{\ovalvariable{total} + \ovalnum{1}}}
\blocksound{stop all sounds}
}
\blockmove{go to \selectmenu{mouse cursor}}
\end{scratch}
Out[13]:

Loops

In [16]:
%%tikz -p scratch --no-wrap -s 0.5

\setscratch{else word=else}
\begin{scratch}
\blockpen{Pen down}
\blockinfloop{Repeat \ovalnum{4} times}
{
\blockmove{turn \ovaloperator{random number between \ovalnum{0} and \ovalnum{359}}}
\blockmove{forward \ovaloperator{random number between \ovalnum{1} and \ovalnum{10}}}
\blockif{if \boolsensing{\selectmenu{edge} detected?}}
{
5
\blockmove{move to x: \ovalnum{0} y: \ovalnum{0}}
}
}
\end{scratch}
Out[16]:

Rendering Blocks Using Line Magic

If we pass the LaTeX code to a string, we can render it using line magic.

In [17]:
p=r'''
\setscratch{else word=else}
\begin{scratch}
\blockpen{Pen down}
\blockinfloop{Repeat \ovalnum{4} times}
{
\blockmove{turn \ovaloperator{random number between \ovalnum{0} and \ovalnum{359}}}
\blockmove{forward \ovaloperator{random number between \ovalnum{1} and \ovalnum{10}}}
\blockif{if \boolsensing{\selectmenu{edge} detected?}}
{
5
\blockmove{move to x: \ovalnum{0} y: \ovalnum{0}}
}
}
\end{scratch}
'''

blocks = %tikz -p scratch -s 0.5 --no-wrap --variable p
blocks
Out[17]:
In [ ]: