{"route":"/en-US-v0.13.1/reference/introspection/state/","title":"State","description":"Documentation for the State type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"state-and-markup","name":"State And Markup","children":[]},{"id":"state-in-typst","name":"State In Typst","children":[]},{"id":"time-travel","name":"Time Travel","children":[]},{"id":"caution","name":"Caution","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-key","name":"key","children":[]},{"id":"constructor-init","name":"init","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-get","name":"Get","children":[]},{"id":"definitions-at","name":"At","children":[{"id":"definitions-at-selector","name":"selector","children":[]}]},{"id":"definitions-final","name":"Final","children":[]},{"id":"definitions-update","name":"Update","children":[{"id":"definitions-update-update","name":"update","children":[]}]}]}],"body":{"kind":"type","content":{"name":"state","title":"State","keywords":[],"oneliner":"Manages stateful parts of your document.","details":"<p>Manages stateful parts of your document.</p>\n<p>Let's say you have some computations in your document and want to remember\nthe result of your last computation to use it in the next one. You might try\nsomething similar to the code below and expect it to output 10, 13, 26, and\n21. However this <strong>does not work</strong> in Typst. If you test this code, you will\nsee that Typst complains with the following error message: <em>Variables from\noutside the function are read-only and cannot be modified.</em></p>\n<pre><code><span class=\"typ-comment\">// This doesn&#39;t work!</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> x <span class=\"typ-op\">=</span> <span class=\"typ-num\">0</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  x <span class=\"typ-op\">=</span> <span class=\"typ-func\">eval</span><span class=\"typ-punct\">(</span>\n    expr<span class=\"typ-punct\">.</span><span class=\"typ-func\">replace</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-func\">str</span><span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">[</span>New value is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">x</span>. <span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre>\n<h2 id=\"state-and-markup\">State and document markup</h2>\n<p>Why does it do that? Because, in general, this kind of computation with side\neffects is problematic in document markup and Typst is upfront about that.\nFor the results to make sense, the computation must proceed in the same\norder in which the results will be laid out in the document. In our simple\nexample, that's the case, but in general it might not be.</p>\n<p>Let's look at a slightly different, but similar kind of state: The heading\nnumbering. We want to increase the heading counter at each heading. Easy\nenough, right? Just add one. Well, it's not that simple. Consider the\nfollowing example:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">heading</span><span class=\"typ-punct\">(</span>numbering<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;1.&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">template</span><span class=\"typ-punct\">(</span>body<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-heading\">= Outline</span>\n  <span class=\"typ-escape\">...</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">body</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> <span class=\"typ-func\">template</span>\n\n<span class=\"typ-heading\">= Introduction</span>\n<span class=\"typ-escape\">...</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/382f18a61cf8fa6150847e8c9bd970c0.png\" alt=\"Preview\"></div></div>\n<p>Here, Typst first processes the body of the document after the show rule,\nsees the <code>Introduction</code> heading, then passes the resulting content to the\n<code>template</code> function and only then sees the <code>Outline</code>. Just counting up would\nnumber the <code>Introduction</code> with <code>1</code> and the <code>Outline</code> with <code>2</code>.</p>\n<h2 id=\"state-in-typst\">Managing state in Typst</h2>\n<p>So what do we do instead? We use Typst's state management system. Calling\nthe <code>state</code> function with an identifying string key and an optional initial\nvalue gives you a state value which exposes a few functions. The two most\nimportant ones are <code>get</code> and <code>update</code>:</p>\n<ul>\n<li>\n<p>The <a href=\"/en-US-v0.13.1/reference/introspection/state/#definitions-get\"><code>get</code></a> function retrieves the current value of the state.\nBecause the value can vary over the course of the document, it is a\n<em>contextual</em> function that can only be used when <a href=\"/en-US-v0.13.1/reference/context/\">context</a> is\navailable.</p>\n</li>\n<li>\n<p>The <a href=\"/en-US-v0.13.1/reference/introspection/state/#definitions-update\"><code>update</code></a> function modifies the state. You can give it\nany value. If given a non-function value, it sets the state to that value.\nIf given a function, that function receives the previous state and has to\nreturn the new state.</p>\n</li>\n</ul>\n<p>Our initial example would now look like this:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> s <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">0</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">s</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>x <span class=\"typ-op\">=&gt;</span>\n    <span class=\"typ-func\">eval</span><span class=\"typ-punct\">(</span>expr<span class=\"typ-punct\">.</span><span class=\"typ-func\">replace</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-func\">str</span><span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">)</span>\n  New value is <span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> s<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>.\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/4ef077712c72e97c10569d045d9f7e7b.png\" alt=\"Preview\"></div></div>\n<p>State managed by Typst is always updated in layout order, not in evaluation\norder. The <code>update</code> method returns content and its effect occurs at the\nposition where the returned content is inserted into the document.</p>\n<p>As a result, we can now also store some of the computations in variables,\nbut they still show the correct results:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-escape\">...</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> more <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x - 5&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">more</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/95e487c3196497c7c1a2164ab7894ce0.png\" alt=\"Preview\"></div></div>\n<p>This example is of course a bit silly, but in practice this is often exactly\nwhat you want! A good example are heading counters, which is why Typst's\n<a href=\"/en-US-v0.13.1/reference/introspection/counter/\">counting system</a> is very similar to its state system.</p>\n<h2 id=\"time-travel\">Time Travel</h2>\n<p>By using Typst's state management system you also get time travel\ncapabilities! We can find out what the value of the state will be at any\nposition in the document from anywhere else. In particular, the <code>at</code> method\ngives us the value of the state at any particular location and the <code>final</code>\nmethods gives us the value of the state at the end of the document.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-escape\">...</span>\n\nValue at <span class=\"typ-raw\">`&lt;here&gt;`</span> is\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> s<span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-label\">&lt;here&gt;</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;10&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x + 3&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-strong\">*Here.*</span> <span class=\"typ-label\">&lt;here&gt;</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x * 2&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">compute</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x - 5&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/1526d8d8866c90f34a790b4fa9b8eba0.png\" alt=\"Preview\"></div></div>\n<h2 id=\"caution\">A word of caution</h2>\n<p>To resolve the values of all states, Typst evaluates parts of your code\nmultiple times. However, there is no guarantee that your state manipulation\ncan actually be completely resolved.</p>\n<p>For instance, if you generate state updates depending on the final value of\na state, the results might never converge. The example below illustrates\nthis. We initialize our state with <code>1</code> and then update it to its own final\nvalue plus 1. So it should be <code>2</code>, but then its final value is <code>2</code>, so it\nshould be <code>3</code>, and so on. This example displays a finite value because Typst\nsimply gives up after a few attempts.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-comment\">// This is bad!</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> s <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;x&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> s<span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span>s<span class=\"typ-punct\">.</span><span class=\"typ-func\">final</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">+</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> s<span class=\"typ-punct\">.</span><span class=\"typ-func\">get</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/e0006b34068755bbf3085f4912651e6c.png\" alt=\"Preview\"></div></div>\n<p>In general, you should try not to generate state updates from within context\nexpressions. If possible, try to express your updates as non-contextual\nvalues or functions that compute the new value from the previous value.\nSometimes, it cannot be helped, but in those cases it is up to you to ensure\nthat the result converges.</p>","constructor":{"path":[],"name":"state","title":"Construct","keywords":[],"oneliner":"Create a new state identified by a key.","element":false,"contextual":false,"deprecation":null,"details":"<p>Create a new state identified by a key.</p>","example":null,"self":false,"params":[{"name":"key","details":"<p>The key that identifies this state.</p>","example":null,"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"init","details":"<p>The initial value of the state.</p>","example":null,"types":["any"],"strings":[],"default":"<code><span class=\"typ-key\">none</span></code>","positional":true,"named":false,"required":false,"variadic":false,"settable":false}],"returns":["state"],"scope":[]},"scope":[{"path":["state"],"name":"get","title":"Get","keywords":[],"oneliner":"Retrieves the value of the state at the current location.","element":false,"contextual":true,"deprecation":null,"details":"<p>Retrieves the value of the state at the current location.</p>\n<p>This is equivalent to <code>state<span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">here</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span></code>.</p>","example":null,"self":true,"params":[],"returns":["any"],"scope":[]},{"path":["state"],"name":"at","title":"At","keywords":[],"oneliner":"Retrieves the value of the state at the given selector's unique match.","element":false,"contextual":true,"deprecation":null,"details":"<p>Retrieves the value of the state at the given selector's unique match.</p>\n<p>The <code>selector</code> must match exactly one element in the document. The most\nuseful kinds of selectors for this are <a href=\"/en-US-v0.13.1/reference/foundations/label/\">labels</a> and\n<a href=\"/en-US-v0.13.1/reference/introspection/location/\">locations</a>.</p>","example":null,"self":true,"params":[{"name":"selector","details":"<p>The place at which the state's value should be retrieved.</p>","example":null,"types":["label","selector","location","function"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["state"],"name":"final","title":"Final","keywords":[],"oneliner":"Retrieves the value of the state at the end of the document.","element":false,"contextual":true,"deprecation":null,"details":"<p>Retrieves the value of the state at the end of the document.</p>","example":null,"self":true,"params":[],"returns":["any"],"scope":[]},{"path":["state"],"name":"update","title":"Update","keywords":[],"oneliner":"Update the value of the state.","element":false,"contextual":false,"deprecation":null,"details":"<p>Update the value of the state.</p>\n<p>The update will be in effect at the position where the returned content\nis inserted into the document. If you don't put the output into the\ndocument, nothing happens! This would be the case, for example, if you\nwrite <code><span class=\"typ-key\">let</span> _ <span class=\"typ-op\">=</span> <span class=\"typ-func\">state</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;key&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">update</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">7</span><span class=\"typ-punct\">)</span></code>. State updates are always\napplied in layout order and in that case, Typst wouldn't know when to\nupdate the state.</p>","example":null,"self":true,"params":[{"name":"update","details":"<p>If given a non function-value, sets the state to that value. If\ngiven a function, that function receives the previous state and has\nto return the new state.</p>","example":null,"types":["any","function"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["content"],"scope":[]}]}}}