{"route":"/en-US-v0.13.1/reference/scripting/","title":"Scripting","description":"Automate your document with Typst's scripting capabilities.","part":null,"outline":[{"id":"expressions","name":"Expressions","children":[]},{"id":"blocks","name":"Blocks","children":[]},{"id":"bindings","name":"Bindings","children":[]},{"id":"conditionals","name":"Conditionals","children":[]},{"id":"loops","name":"Loops","children":[]},{"id":"fields","name":"Fields","children":[]},{"id":"methods","name":"Methods","children":[]},{"id":"modules","name":"Modules","children":[]},{"id":"packages","name":"Packages","children":[]},{"id":"operators","name":"Operators","children":[]}],"body":{"kind":"html","content":"<h1>Scripting</h1>\n<p>Typst embeds a powerful scripting language. You can automate your documents and\ncreate more sophisticated styles with code. Below is an overview over the\nscripting concepts.</p>\n<h2 id=\"expressions\">Expressions</h2>\n<p>In Typst, markup and code are fused into one. All but the most common elements\nare created with <em>functions.</em> To make this as convenient as possible, Typst\nprovides compact syntax to embed a code expression into markup: An expression is\nintroduced with a hash (<code>#</code>) and normal markup parsing resumes after the\nexpression is finished. If a character would continue the expression but should\nbe interpreted as text, the expression can forcibly be ended with a semicolon\n(<code>;</code>).</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">emph</span><span class=\"typ-punct\">[</span>Hello<span class=\"typ-punct\">]</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">emoji</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">face</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-str\">#</span><span class=\"typ-str\">&quot;hello&quot;</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/56fcebfd54e87e06f0936777ca6c4f31.png\" alt=\"Preview\"></div></div>\n<p>The example above shows a few of the available expressions, including\n<a href=\"/en-US-v0.13.1/reference/foundations/function/\">function calls</a>, <a href=\"/en-US-v0.13.1/reference/scripting/#fields\">field accesses</a>, and\n<a href=\"/en-US-v0.13.1/reference/scripting/#methods\">method calls</a>. More kinds of expressions are\ndiscussed in the remainder of this chapter. A few kinds of expressions are not\ncompatible with the hash syntax (e.g. binary operator expressions). To embed\nthese into markup, you can use parentheses, as in <code><span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span> <span class=\"typ-op\">+</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span></code>.</p>\n<h2 id=\"blocks\">Blocks</h2>\n<p>To structure your code and embed markup into it, Typst provides two kinds of\n<em>blocks:</em></p>\n<ul>\n<li>\n<p><strong>Code block:</strong> <code><span class=\"typ-punct\">{</span> <span class=\"typ-key\">let</span> x <span class=\"typ-op\">=</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">;</span> x <span class=\"typ-op\">+</span> <span class=\"typ-num\">2</span> <span class=\"typ-punct\">}</span></code> <br />\nWhen writing code, you'll probably want to split up your computation into\nmultiple statements, create some intermediate variables and so on. Code blocks\nlet you write multiple expressions where one is expected. The individual\nexpressions in a code block should be separated by line breaks or semicolons.\nThe output values of the individual expressions in a code block are joined to\ndetermine the block's value. Expressions without useful output, like <code><span class=\"typ-key\">let</span></code>\nbindings yield <code><span class=\"typ-key\">none</span></code>, which can be joined with any value without effect.</p>\n</li>\n<li>\n<p><strong>Content block:</strong> <code><span class=\"typ-punct\">[</span><span class=\"typ-strong\">*Hey*</span> there!<span class=\"typ-punct\">]</span></code> <br />\nWith content blocks, you can handle markup/content as a programmatic value,\nstore it in variables and pass it to <a href=\"/en-US-v0.13.1/reference/foundations/function/\">functions</a>. Content\nblocks are delimited by square brackets and can contain arbitrary markup. A\ncontent block results in a value of type <a href=\"/en-US-v0.13.1/reference/foundations/content/\" title=\"content\">content</a>. An arbitrary number of\ncontent blocks can be passed as trailing arguments to functions. That is,\n<code><span class=\"typ-func\">list</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">[</span>A<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> <span class=\"typ-punct\">[</span>B<span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span></code> is equivalent to <code><span class=\"typ-func\">list</span><span class=\"typ-punct\">[</span>A<span class=\"typ-punct\">]</span><span class=\"typ-punct\">[</span>B<span class=\"typ-punct\">]</span></code>.</p>\n</li>\n</ul>\n<p>Content and code blocks can be nested arbitrarily. In the example below,\n<code><span class=\"typ-punct\">[</span>hello <span class=\"typ-punct\">]</span></code> is joined with the output of  <code>a <span class=\"typ-op\">+</span> <span class=\"typ-punct\">[</span> the <span class=\"typ-punct\">]</span> <span class=\"typ-op\">+</span> b</code> yielding\n<code><span class=\"typ-punct\">[</span>hello from the <span class=\"typ-strong\">*world*</span><span class=\"typ-punct\">]</span></code>.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-punct\">#</span><span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">let</span> a <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>from<span class=\"typ-punct\">]</span>\n  <span class=\"typ-key\">let</span> b <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span><span class=\"typ-strong\">*world*</span><span class=\"typ-punct\">]</span>\n  <span class=\"typ-punct\">[</span>hello <span class=\"typ-punct\">]</span>\n  a <span class=\"typ-op\">+</span> <span class=\"typ-punct\">[</span> the <span class=\"typ-punct\">]</span> <span class=\"typ-op\">+</span> b\n<span class=\"typ-punct\">}</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/f5f1a59a923ddd7899e3844457f683a1.png\" alt=\"Preview\"></div></div>\n<h2 id=\"bindings\">Bindings and Destructuring</h2>\n<p>As already demonstrated above, variables can be defined with <code><span class=\"typ-key\">let</span></code> bindings.\nThe variable is assigned the value of the expression that follows the <code>=</code> sign.\nThe assignment of a value is optional, if no value is assigned, the variable\nwill be initialized as <code><span class=\"typ-key\">none</span></code>. The <code><span class=\"typ-key\">let</span></code> keyword can also be used to create\na <a href=\"/en-US-v0.13.1/reference/foundations/function/#defining-functions\">custom named function</a>. Variables can be\naccessed for the rest of the containing block (or the rest of the file if there\nis no containing block).</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> name <span class=\"typ-op\">=</span> <span class=\"typ-str\">&quot;Typst&quot;</span>\nThis is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">name</span>&#39;s documentation.\nIt explains <span class=\"typ-pol\">#</span><span class=\"typ-pol\">name</span>.\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">add</span><span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">,</span> y<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> x <span class=\"typ-op\">+</span> y\nSum is <span class=\"typ-func\">#</span><span class=\"typ-func\">add</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">)</span>.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/cab2fd22fe9abd4d4b827c1bc30aee2f.png\" alt=\"Preview\"></div></div>\n<p>Let bindings can also be used to destructure <a href=\"/en-US-v0.13.1/reference/foundations/array/\">arrays</a> and\n<a href=\"/en-US-v0.13.1/reference/foundations/dictionary/\">dictionaries</a>. In this case, the left-hand side of the\nassignment should mirror an array or dictionary. The <code>..</code> operator can be used\nonce in the pattern to collect the remainder of the array's or dictionary's\nitems.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">,</span> y<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span>\nThe coordinates are <span class=\"typ-pol\">#</span><span class=\"typ-pol\">x</span>, <span class=\"typ-pol\">#</span><span class=\"typ-pol\">y</span>.\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>a<span class=\"typ-punct\">,</span> <span class=\"typ-op\">..</span><span class=\"typ-punct\">,</span> b<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">)</span>\nThe first element is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">a</span>.\nThe last element is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">b</span>.\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> books <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span>\n  Shakespeare<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Hamlet&quot;</span><span class=\"typ-punct\">,</span>\n  Homer<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;The Odyssey&quot;</span><span class=\"typ-punct\">,</span>\n  Austen<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Persuasion&quot;</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>Austen<span class=\"typ-punct\">,</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> books\nAusten wrote <span class=\"typ-pol\">#</span><span class=\"typ-pol\">Austen</span>.\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>Homer<span class=\"typ-punct\">:</span> h<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> books\nHomer wrote <span class=\"typ-pol\">#</span><span class=\"typ-pol\">h</span>.\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>Homer<span class=\"typ-punct\">,</span> <span class=\"typ-op\">..</span>other<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> books\n<span class=\"typ-key\">#</span><span class=\"typ-key\">for</span> <span class=\"typ-punct\">(</span>author<span class=\"typ-punct\">,</span> title<span class=\"typ-punct\">)</span> <span class=\"typ-key\">in</span> other <span class=\"typ-punct\">[</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">author</span> wrote <span class=\"typ-pol\">#</span><span class=\"typ-pol\">title</span>.\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/574a8a54d94246e011156a58bb988f10.png\" alt=\"Preview\"></div></div>\n<p>You can use the underscore to discard elements in a destructuring pattern:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-punct\">(</span>_<span class=\"typ-punct\">,</span> y<span class=\"typ-punct\">,</span> _<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">)</span>\nThe y coordinate is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">y</span>.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/2d320f557a31c13c47827649626f615e.png\" alt=\"Preview\"></div></div>\n<p>Destructuring also works in argument lists of functions ...</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> left <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">5</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> right <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">6</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">left</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">zip</span><span class=\"typ-punct\">(</span>right<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">map</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-punct\">(</span><span class=\"typ-punct\">(</span>a<span class=\"typ-punct\">,</span>b<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">=&gt;</span> a <span class=\"typ-op\">+</span> b\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/eb451d0a1cf36461e8a56ce203acf067.png\" alt=\"Preview\"></div></div>\n<p>... and on the left-hand side of normal assignments. This can be useful to\nswap variables among other things.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-punct\">#</span><span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">let</span> a <span class=\"typ-op\">=</span> <span class=\"typ-num\">1</span>\n  <span class=\"typ-key\">let</span> b <span class=\"typ-op\">=</span> <span class=\"typ-num\">2</span>\n  <span class=\"typ-punct\">(</span>a<span class=\"typ-punct\">,</span> b<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span>b<span class=\"typ-punct\">,</span> a<span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">[</span>a = <span class=\"typ-pol\">#</span><span class=\"typ-pol\">a</span>, b = <span class=\"typ-pol\">#</span><span class=\"typ-pol\">b</span><span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/2c2bcc6a2509a95daa0bc4e986ee7e6d.png\" alt=\"Preview\"></div></div>\n<h2 id=\"conditionals\">Conditionals</h2>\n<p>With a conditional, you can display or compute different things depending on\nwhether some condition is fulfilled. Typst supports <code><span class=\"typ-key\">if</span></code>, <code>else <span class=\"typ-key\">if</span></code> and\n<code>else</code> expressions. When the condition evaluates to <code><span class=\"typ-key\">true</span></code>, the conditional\nyields the value resulting from the if's body, otherwise yields the value\nresulting from the else's body.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">if</span> <span class=\"typ-num\">1</span> <span class=\"typ-op\">&lt;</span> <span class=\"typ-num\">2</span> <span class=\"typ-punct\">[</span>\n  This is shown\n<span class=\"typ-punct\">]</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">[</span>\n  This is not.\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/9f03dfe97f385ab1a6d01127a9152c4d.png\" alt=\"Preview\"></div></div>\n<p>Each branch can have a code or content block as its body.</p>\n<ul>\n<li><code><span class=\"typ-key\">if</span> condition <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code></li>\n<li><code><span class=\"typ-key\">if</span> condition <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span></code></li>\n<li><code><span class=\"typ-key\">if</span> condition <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code></li>\n<li><code><span class=\"typ-key\">if</span> condition <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span> <span class=\"typ-key\">else</span> <span class=\"typ-key\">if</span> condition <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span></code></li>\n</ul>\n<h2 id=\"loops\">Loops</h2>\n<p>With loops, you can repeat content or compute something iteratively. Typst\nsupports two types of loops: <code><span class=\"typ-key\">for</span></code> and <code><span class=\"typ-key\">while</span></code> loops. The former iterate\nover a specified collection whereas the latter iterate as long as a condition\nstays fulfilled. Just like blocks, loops <em>join</em> the results from each iteration\ninto one value.</p>\n<p>In the example below, the three sentences created by the for loop join together\ninto a single content value and the length-1 arrays in the while loop join\ntogether into one larger array.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">for</span> c <span class=\"typ-key\">in</span> <span class=\"typ-str\">&quot;ABC&quot;</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">c</span> is a letter.\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> n <span class=\"typ-op\">=</span> <span class=\"typ-num\">2</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">while</span> n <span class=\"typ-op\">&lt;</span> <span class=\"typ-num\">10</span> <span class=\"typ-punct\">{</span>\n  n <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span>n <span class=\"typ-op\">*</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">-</span> <span class=\"typ-num\">1</span>\n  <span class=\"typ-punct\">(</span>n<span class=\"typ-punct\">,</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">}</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/ef87ec09b6f156465a2deb8f2ca4620b.png\" alt=\"Preview\"></div></div>\n<p>For loops can iterate over a variety of collections:</p>\n<ul>\n<li>\n<p><code><span class=\"typ-key\">for</span> value <span class=\"typ-key\">in</span> array <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code> <br />\nIterates over the items in the <a href=\"/en-US-v0.13.1/reference/foundations/array/\" title=\"array\">array</a>. The destructuring syntax described in\n<a href=\"/en-US-v0.13.1/reference/scripting/#bindings\">Let binding</a> can also be used here.</p>\n</li>\n<li>\n<p><code><span class=\"typ-key\">for</span> pair <span class=\"typ-key\">in</span> dict <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code> <br />\nIterates over the key-value pairs of the <a href=\"/en-US-v0.13.1/reference/foundations/dictionary/\" title=\"dictionary\">dictionary</a>. The pairs can also be\ndestructured by using <code><span class=\"typ-key\">for</span> <span class=\"typ-punct\">(</span>key<span class=\"typ-punct\">,</span> value<span class=\"typ-punct\">)</span> <span class=\"typ-key\">in</span> dict <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code>. It is more efficient\nthan <code><span class=\"typ-key\">for</span> pair <span class=\"typ-key\">in</span> dict<span class=\"typ-punct\">.</span><span class=\"typ-func\">pairs</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code> because it doesn't create a temporary\narray of all key-value pairs.</p>\n</li>\n<li>\n<p><code><span class=\"typ-key\">for</span> letter <span class=\"typ-key\">in</span> <span class=\"typ-str\">&quot;abc&quot;</span> <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code> <br />\nIterates over the characters of the <a href=\"/en-US-v0.13.1/reference/foundations/str/\">string</a>. Technically, it iterates\nover the grapheme clusters of the string. Most of the time, a grapheme cluster\nis just a single codepoint. However, a grapheme cluster could contain multiple\ncodepoints, like a flag emoji.</p>\n</li>\n<li>\n<p><code><span class=\"typ-key\">for</span> byte <span class=\"typ-key\">in</span> <span class=\"typ-func\">bytes</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;😀&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code> <br />\nIterates over the <a href=\"/en-US-v0.13.1/reference/foundations/bytes/\" title=\"bytes\">bytes</a>, which can be converted from a <a href=\"/en-US-v0.13.1/reference/foundations/str/\">string</a> or\n<a href=\"/en-US-v0.13.1/reference/data-loading/read/\" title=\"read\">read</a> from a file without encoding. Each byte value is an <a href=\"/en-US-v0.13.1/reference/foundations/int/\">integer</a>\nbetween <code><span class=\"typ-num\">0</span></code> and <code><span class=\"typ-num\">255</span></code>.</p>\n</li>\n</ul>\n<p>To control the execution of the loop, Typst provides the <code><span class=\"typ-key\">break</span></code> and\n<code><span class=\"typ-key\">continue</span></code> statements. The former performs an early exit from the loop while\nthe latter skips ahead to the next iteration of the loop.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">for</span> letter <span class=\"typ-key\">in</span> <span class=\"typ-str\">&quot;abc nope&quot;</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">if</span> letter <span class=\"typ-op\">==</span> <span class=\"typ-str\">&quot; &quot;</span> <span class=\"typ-punct\">{</span>\n    <span class=\"typ-key\">break</span>\n  <span class=\"typ-punct\">}</span>\n\n  letter\n<span class=\"typ-punct\">}</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/8ba145cb687a39c8f9146abd55f943f9.png\" alt=\"Preview\"></div></div>\n<p>The body of a loop can be a code or content block:</p>\n<ul>\n<li><code><span class=\"typ-key\">for</span> .. in collection <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code></li>\n<li><code><span class=\"typ-key\">for</span> .. in collection <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span></code></li>\n<li><code><span class=\"typ-key\">while</span> condition <span class=\"typ-punct\">{</span>..<span class=\"typ-punct\">}</span></code></li>\n<li><code><span class=\"typ-key\">while</span> condition <span class=\"typ-punct\">[</span>..<span class=\"typ-punct\">]</span></code></li>\n</ul>\n<h2 id=\"fields\">Fields</h2>\n<p>You can use <em>dot notation</em> to access fields on a value. For values of type\n<a href=\"/en-US-v0.13.1/reference/foundations/content/\" title=\"`content`\"><code>content</code></a>, you can also use the <a href=\"/en-US-v0.13.1/reference/foundations/content/#definitions-fields\"><code>fields</code></a> function to list\nthe fields.</p>\n<p>The value in question can be either:</p>\n<ul>\n<li>a <a href=\"/en-US-v0.13.1/reference/foundations/dictionary/\" title=\"dictionary\">dictionary</a> that has the specified key,</li>\n<li>a <a href=\"/en-US-v0.13.1/reference/foundations/symbol/\" title=\"symbol\">symbol</a> that has the specified modifier,</li>\n<li>a <a href=\"/en-US-v0.13.1/reference/foundations/module/\" title=\"module\">module</a> containing the specified definition,</li>\n<li><a href=\"/en-US-v0.13.1/reference/foundations/content/\" title=\"content\">content</a> consisting of an element that has the specified field. The\navailable fields match the arguments of the\n<a href=\"/en-US-v0.13.1/reference/foundations/function/#element-functions\">element function</a> that were given when the\nelement was constructed.</li>\n</ul>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> it <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span><span class=\"typ-heading\">= Heading</span><span class=\"typ-punct\">]</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">it</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">body</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">it</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">depth</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">it</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">fields</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> dict <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span>greet<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Hello&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">greet</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">emoji</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">face</span>\n\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/e560cf75c003b55ee615a91fba54908a.png\" alt=\"Preview\"></div></div>\n<h2 id=\"methods\">Methods</h2>\n<p>A <em>method call</em> is a convenient way to call a function that is scoped to a\nvalue's <a href=\"/en-US-v0.13.1/reference/foundations/type/\" title=\"type\">type</a>. For example, we can call the <a href=\"/en-US-v0.13.1/reference/foundations/str/#definitions-len\"><code>str.len</code></a> function in\nthe following two equivalent ways:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-pol\">#</span><span class=\"typ-pol\">str</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;abc&quot;</span><span class=\"typ-punct\">)</span> is the same as\n<span class=\"typ-str\">#</span><span class=\"typ-str\">&quot;abc&quot;</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/8fc9134962ea2ca6f8f3beaa1a77c940.png\" alt=\"Preview\"></div></div>\n<p>The structure of a method call is <code>value<span class=\"typ-punct\">.</span><span class=\"typ-func\">method</span><span class=\"typ-punct\">(</span><span class=\"typ-op\">..</span>args<span class=\"typ-punct\">)</span></code> and its equivalent\nfull function call is <code><span class=\"typ-func\">type</span><span class=\"typ-punct\">(</span>value<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">method</span><span class=\"typ-punct\">(</span>value<span class=\"typ-punct\">,</span> <span class=\"typ-op\">..</span>args<span class=\"typ-punct\">)</span></code>. The documentation\nof each type lists its scoped functions. You cannot currently define your own\nmethods.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> values <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">4</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">values</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">pop</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">values</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n\n<span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;a, b, c&quot;</span>\n    <span class=\"typ-punct\">.</span><span class=\"typ-func\">split</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;, &quot;</span><span class=\"typ-punct\">)</span>\n    <span class=\"typ-punct\">.</span><span class=\"typ-func\">join</span><span class=\"typ-punct\">[</span> <span class=\"typ-escape\">---</span> <span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-str\">#</span><span class=\"typ-str\">&quot;abc&quot;</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> is the same as\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">str</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;abc&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/69c81d74d21d4e212b8bdde97b02c949.png\" alt=\"Preview\"></div></div>\n<p>There are a few special functions that modify the value they are called on (e.g.\n<a href=\"/en-US-v0.13.1/reference/foundations/array/#definitions-push\"><code>array.push</code></a>). These functions <em>must</em> be called in method form.\nIn some cases, when the method is only called for its side effect, its return\nvalue should be ignored (and not participate in joining). The canonical way to\ndiscard a value is with a let binding: <code><span class=\"typ-key\">let</span> _ <span class=\"typ-op\">=</span> array<span class=\"typ-punct\">.</span><span class=\"typ-func\">remove</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span></code>.</p>\n<h2 id=\"modules\">Modules</h2>\n<p>You can split up your Typst projects into multiple files called <em>modules.</em> A\nmodule can refer to the content and definitions of another module in multiple\nways:</p>\n<ul>\n<li>\n<p><strong>Including:</strong> <code><span class=\"typ-key\">include</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span></code> <br />\nEvaluates the file at the path <code>bar.typ</code> and returns the resulting <a href=\"/en-US-v0.13.1/reference/foundations/content/\" title=\"content\">content</a>.</p>\n</li>\n<li>\n<p><strong>Import:</strong> <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span></code> <br />\nEvaluates the file at the path <code>bar.typ</code> and inserts the resulting <a href=\"/en-US-v0.13.1/reference/foundations/module/\" title=\"module\">module</a>\ninto the current scope as <code>bar</code> (filename without extension). You can use the\n<code>as</code> keyword to rename the imported module: <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span> <span class=\"typ-key\">as</span> baz</code>. You\ncan import nested items using dot notation: <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span><span class=\"typ-punct\">:</span> baz<span class=\"typ-punct\">.</span>a</code>.</p>\n</li>\n<li>\n<p><strong>Import items:</strong> <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span><span class=\"typ-punct\">:</span> a<span class=\"typ-punct\">,</span> b</code> <br />\nEvaluates the file at the path <code>bar.typ</code>, extracts the values of the variables\n<code>a</code> and <code>b</code> (that need to be defined in <code>bar.typ</code>, e.g. through <code><span class=\"typ-key\">let</span></code>\nbindings) and defines them in the current file. Replacing <code>a, b</code> with <code>*</code>\nloads all variables defined in a module. You can use the <code>as</code> keyword to\nrename the individual items: <code><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;bar.typ&quot;</span><span class=\"typ-punct\">:</span> a <span class=\"typ-key\">as</span> one<span class=\"typ-punct\">,</span> b <span class=\"typ-key\">as</span> two</code></p>\n</li>\n</ul>\n<p>Instead of a path, you can also use a <a href=\"/en-US-v0.13.1/reference/foundations/module/\">module value</a>, as shown in the\nfollowing example:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">import</span> emoji<span class=\"typ-punct\">:</span> face\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">face</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">grin</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/9c38d95483bccbf7479a82427087ab2a.png\" alt=\"Preview\"></div></div>\n<h2 id=\"packages\">Packages</h2>\n<p>To reuse building blocks across projects, you can also create and import Typst\n<em>packages.</em> A package import is specified as a triple of a namespace, a name,\nand a version.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;@preview/example:0.1.0&quot;</span><span class=\"typ-punct\">:</span> add\n<span class=\"typ-func\">#</span><span class=\"typ-func\">add</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">7</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/3d994560e3517e38057aec147c441de6.png\" alt=\"Preview\"></div></div>\n<p>The <code>preview</code> namespace contains packages shared by the community. You can find\nall available community packages on <a href=\"https://typst.app/universe/\">Typst Universe</a>.</p>\n<p>If you are using Typst locally, you can also create your own system-local\npackages. For more details on this, see the\n<a href=\"https://github.com/typst/packages\">package repository</a>.</p>\n<h2 id=\"operators\">Operators</h2>\n<p>The following table lists all available unary and binary operators with effect,\narity (unary, binary) and precedence level (higher binds stronger). Some\noperations, such as <a href=\"/en-US-v0.13.1/reference/foundations/calc/#functions-rem-euclid\">modulus</a>, do not have a special syntax\nand can be achieved using functions from the\n<a href=\"/en-US-v0.13.1/reference/foundations/calc/\"><code>calc</code></a> module.</p>\n<table><thead><tr><th style=\"text-align: center\">Operator</th><th>Effect</th><th style=\"text-align: center\">Arity</th><th style=\"text-align: center\">Precedence</th></tr></thead><tbody>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-op\">-</span></code></td><td>Negation</td><td style=\"text-align: center\">Unary</td><td style=\"text-align: center\">7</td></tr>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-op\">+</span></code></td><td>No effect (exists for symmetry)</td><td style=\"text-align: center\">Unary</td><td style=\"text-align: center\">7</td></tr>\n<tr><td style=\"text-align: center\"><code>*</code></td><td>Multiplication</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">6</td></tr>\n<tr><td style=\"text-align: center\"><code>/</code></td><td>Division</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">6</td></tr>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-op\">+</span></code></td><td>Addition</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">5</td></tr>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-op\">-</span></code></td><td>Subtraction</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">5</td></tr>\n<tr><td style=\"text-align: center\"><code>==</code></td><td>Check equality</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>!=</code></td><td>Check inequality</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>&lt;</code></td><td>Check less-than</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>&lt;=</code></td><td>Check less-than or equal</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>&gt;</code></td><td>Check greater-than</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>&gt;=</code></td><td>Check greater-than or equal</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code>in</code></td><td>Check if in collection</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-key\">not</span> <span class=\"typ-key\">in</span></code></td><td>Check if not in collection</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">4</td></tr>\n<tr><td style=\"text-align: center\"><code><span class=\"typ-key\">not</span></code></td><td>Logical &quot;not&quot;</td><td style=\"text-align: center\">Unary</td><td style=\"text-align: center\">3</td></tr>\n<tr><td style=\"text-align: center\"><code>and</code></td><td>Short-circuiting logical &quot;and&quot;</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">3</td></tr>\n<tr><td style=\"text-align: center\"><code>or</code></td><td>Short-circuiting logical &quot;or&quot;</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">2</td></tr>\n<tr><td style=\"text-align: center\"><code>=</code></td><td>Assignment</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">1</td></tr>\n<tr><td style=\"text-align: center\"><code>+=</code></td><td>Add-Assignment</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">1</td></tr>\n<tr><td style=\"text-align: center\"><code>-=</code></td><td>Subtraction-Assignment</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">1</td></tr>\n<tr><td style=\"text-align: center\"><code>*=</code></td><td>Multiplication-Assignment</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">1</td></tr>\n<tr><td style=\"text-align: center\"><code>/=</code></td><td>Division-Assignment</td><td style=\"text-align: center\">Binary</td><td style=\"text-align: center\">1</td></tr>\n</tbody></table>"}}