#!/usr/bin/env python # coding: utf-8 # # Scratch - LaTeX # # The LaTeX [Scratch](https://ctan.org/pkg/scratch?lang=en) package [[docs](http://anorien.csc.warwick.ac.uk/mirrors/CTAN/macros/latex/contrib/scratch/scratch-fr.pdf)] provides a means for dislaying Scratch style programming blocks in a document. # # *Examples taken, with modification, from from the documentation.* # In[3]: get_ipython().run_line_magic('load_ext', 'tikz_magic') # In[4]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\\begin{scratch}\n\\blockmove{Movement Block}\n\\blocklook{Appearance Block}\n\\blocksound{Sound Block}\n\\blockpen{Pen Block}\n\\blockvariable{Variable Block}\n\\blocklist{List Block}\n\\blockevent{Event Block}\n\\blockcontrol{Control Block}\n\\blocksensing{Sensor Block}\n\\end{scratch}\n') # In[5]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\\begin{scratch}\n\\blockinit{When \\greenflag is clicked}\n\\blockmove{Do something...}\n\\blockstop{Done}\n\\end{scratch}\n') # In[6]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blocklook{Say \\txtbox{Hello} for \\ovalnum{2} seconds}\n\\blocksensing{Ask \\txtbox{What is your name ?} and wait}\n\\end{scratch}\n') # In[7]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blockinit{When I receive \\selectmenu{message 1}}\n\\blockcontrol{Create a copy of \\selectmenu{myself}}\n3\n\\blocklook{Display colour \\selectmenu{colour} \\ovalnum{25}}\n\n\\blockpen{Use colour \\squarecolor{red!75!black}}\n\\end{scratch}\n') # In[8]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blockmove{Move \\ovalnum{10}}\n\\blockmove{Heading \\ovalnum{180\\selectarrownum}}\n\\blockmove{Turn \\turnleft{} for \\ovalnum{45} degrees}\n\\blockcontrol{Wait \\ovalnum{1} second}\n\\end{scratch}\n\n') # In[10]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blockmove{Turn in \\ovalmove{direction}}\n\\blocksound{Add \\ovaloperator{\\ovalnum{-0.5} * \\ovalsound{volume}} to volume}\n\\blockmove{Move over \\ovalsound{time} seconds to x: \\ovalsensing{cursor x}\ny: \\ovaloperator{\\ovalnum{4} * \\ovalsensing{cursor y}}}\n\\end{scratch}\n') # ## Conditional Statements # In[11]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blockif{if \\booloperator{\\ovalmove{x} < \\txtbox{7}} then}\n{\\blockmove{go to \\selectmenu{first position}}\n\\blocklook{Change costume to \\selectmenu{costume 4}}\n}\n\\blockmove{move to \\selectmenu{another position}}\n\\end{scratch}\n') # In[13]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\begin{scratch}\n\\blockifelse{if \\boolsensing{colour \\squarecolor{blue!65} detected ?} then}\n{\\blockmove{forward \\ovalnum{2}}\n\\blocksound{play sound \\selectmenu{meeow}}\n}\n{ \\blockvariable{change \\ovalvariable{total} to \\ovaloperator{\\ovalvariable{total} + \\ovalnum{1}}}\n\\blocksound{stop all sounds}\n}\n\\blockmove{go to \\selectmenu{mouse cursor}}\n\\end{scratch}\n') # ### Loops # In[16]: get_ipython().run_cell_magic('tikz', '-p scratch --no-wrap -s 0.5', '\n\\setscratch{else word=else}\n\\begin{scratch}\n\\blockpen{Pen down}\n\\blockinfloop{Repeat \\ovalnum{4} times}\n{\n\\blockmove{turn \\ovaloperator{random number between \\ovalnum{0} and \\ovalnum{359}}}\n\\blockmove{forward \\ovaloperator{random number between \\ovalnum{1} and \\ovalnum{10}}}\n\\blockif{if \\boolsensing{\\selectmenu{edge} detected?}}\n{\n5\n\\blockmove{move to x: \\ovalnum{0} y: \\ovalnum{0}}\n}\n}\n\\end{scratch}\n') # ## 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 = get_ipython().run_line_magic('tikz', '-p scratch -s 0.5 --no-wrap --variable p') blocks # In[ ]: