{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook has examples of using magics." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the first line of a cell starts with %% or #%, the kernel treats it as a \"magic\".\n", "`javascript` will just send the contents as javascript to the browser. In this context, you can use `element` to manipulate the output area." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "data": { "application/javascript": [ "element.append('foo')" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%% javascript\n", "element.append('foo')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/javascript": [ "for (var i=0;i<4;i++) {\n", " element.append(i)\n", "}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#% javascript\n", "for (var i=0;i<4;i++) {\n", " element.append(i)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To generate output of type latex, use the `#% latex` magic." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation*} \\frac{1}{2} \\end{equation*}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#% latex\n", "my $x = '\\begin{equation*} \\frac{1}{2} \\end{equation*}';\n", "$x" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation}\n", "\\frac{1}{2}\n", "\\end{equation}\n" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%% latex(equation)\n", "'\\frac{1}{2}'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "½\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "\\begin{equation}\n", "\\frac{1}{2}\n", "\\end{equation}\n" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#% latex(equation) > html\n", "say '½';\n", "'\\frac{1}{2}';" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(Routine::WrapHandle.new Routine::WrapHandle.new)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Rat.^find_method($_)\n", " .wrap( sub ($x) {\n", " '\\frac{' ~ $x.numerator ~ '}{' ~ $x.denominator ~ '}'\n", " } ) for ;" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation}\n", "\\frac{1}{5} + \\frac{9}{10} - \\frac{1}{10}\n", "\\end{equation}\n" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%% latex(equation)\n", "\"{0.2} + {0.9} - {0.1}\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "\\begin{equation}\n", "\\frac{2}{3} + \\frac{1}{8} == \\frac{19}{24}\n", "\\end{equation}\n" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%% latex(equation)\n", "my $x = 2/3;\n", "my $y = 1/8;\n", "\"$x + $y == { $x + $y }\"" ] } ], "metadata": { "kernelspec": { "display_name": "Raku", "language": "raku", "name": "raku" }, "language_info": { "file_extension": ".raku", "mimetype": "text/x-raku", "name": "raku", "version": "6.d" } }, "nbformat": 4, "nbformat_minor": 2 }