{"route":"/en-US-v0.13.1/reference/model/ref/","title":"Reference","description":"Documentation for the `ref` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"syntax","name":"Syntax","children":[]},{"id":"customization","name":"Customization","children":[]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-target","name":"target","children":[]},{"id":"parameters-supplement","name":"supplement","children":[]},{"id":"parameters-form","name":"form","children":[]}]}],"body":{"kind":"func","content":{"path":[],"name":"ref","title":"Reference","keywords":[],"oneliner":"A reference to a label or bibliography.","element":true,"contextual":false,"deprecation":null,"details":"<p>A reference to a label or bibliography.</p>\n<p>Takes a label and cross-references it. There are two kind of references,\ndetermined by its <a href=\"/en-US-v0.13.1/reference/model/ref/#parameters-form\"><code>form</code></a>: <code><span class=\"typ-str\">&quot;normal&quot;</span></code> and <code><span class=\"typ-str\">&quot;page&quot;</span></code>.</p>\n<p>The default, a <code><span class=\"typ-str\">&quot;normal&quot;</span></code> reference, produces a textual reference to a\nlabel. For example, a reference to a heading will yield an appropriate\nstring such as &quot;Section 1&quot; for a reference to the first heading. The\nreferences are also links to the respective element. Reference syntax can\nalso be used to <a href=\"/en-US-v0.13.1/reference/model/cite/\" title=\"cite\">cite</a> from a bibliography.</p>\n<p>As the default form requires a supplement and numbering, the label must be\nattached to a <em>referenceable element</em>. Referenceable elements include\n<a href=\"/en-US-v0.13.1/reference/model/heading/\">headings</a>, <a href=\"/en-US-v0.13.1/reference/model/figure/\">figures</a>, <a href=\"/en-US-v0.13.1/reference/math/equation/\">equations</a>, and\n<a href=\"/en-US-v0.13.1/reference/model/footnote/\">footnotes</a>. To create a custom referenceable element like a\ntheorem, you can create a figure of a custom <a href=\"/en-US-v0.13.1/reference/model/figure/#parameters-kind\"><code>kind</code></a> and\nwrite a show rule for it. In the future, there might be a more direct way\nto define a custom referenceable element.</p>\n<p>If you just want to link to a labelled element and not get an automatic\ntextual reference, consider using the <a href=\"/en-US-v0.13.1/reference/model/link/\" title=\"`link`\"><code>link</code></a> function instead.</p>\n<p>A <code><span class=\"typ-str\">&quot;page&quot;</span></code> reference produces a page reference to a label, displaying the\npage number at its location. You can use the\n<a href=\"/en-US-v0.13.1/reference/layout/page/#parameters-supplement\">page's supplement</a> to modify the text before the page\nnumber. Unlike a <code><span class=\"typ-str\">&quot;normal&quot;</span></code> reference, the label can be attached to any\nelement.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">page</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\">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\">set</span> math<span class=\"typ-punct\">.</span><span class=\"typ-func\">equation</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\n<span class=\"typ-heading\">= Introduction</span> <span class=\"typ-label\">&lt;intro&gt;</span>\nRecent developments in\ntypesetting software have\nrekindled hope in previously\nfrustrated researchers. <span class=\"typ-ref\">@distress</span>\nAs shown in <span class=\"typ-ref\">@results</span> (see\n<span class=\"typ-func\">#</span><span class=\"typ-func\">ref</span><span class=\"typ-punct\">(</span><span class=\"typ-label\">&lt;results&gt;</span><span class=\"typ-punct\">,</span> form<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;page&quot;</span><span class=\"typ-punct\">)</span>),\nwe <span class=\"typ-escape\">...</span>\n\n<span class=\"typ-heading\">= Results</span> <span class=\"typ-label\">&lt;results&gt;</span>\nWe discuss our approach in\ncomparison with others.\n\n<span class=\"typ-heading\">== Performance</span> <span class=\"typ-label\">&lt;perf&gt;</span>\n<span class=\"typ-ref\">@slow</span> demonstrates what slow\nsoftware looks like.\n<span class=\"typ-math-delim\">$</span> T(n) = O(2<span class=\"typ-math-op\">^</span>n) <span class=\"typ-math-delim\">$</span> <span class=\"typ-label\">&lt;slow&gt;</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">bibliography</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;works.bib&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/a792745ccec945a90763138d4e45762f.png\" alt=\"Preview\"></div></div>\n<h2 id=\"syntax\">Syntax</h2>\n<p>This function also has dedicated syntax: A <code><span class=\"typ-str\">&quot;normal&quot;</span></code> reference to a\nlabel can be created by typing an <code>@</code> followed by the name of the label\n(e.g. <code><span class=\"typ-heading\">= Introduction</span> <span class=\"typ-label\">&lt;intro&gt;</span></code> can be referenced by typing <code><span class=\"typ-ref\">@intro</span></code>).</p>\n<p>To customize the supplement, add content in square brackets after the\nreference: <code><span class=\"typ-ref\">@intro<span class=\"typ-punct\">[</span>Chapter<span class=\"typ-punct\">]</span></span></code>.</p>\n<h2 id=\"customization\">Customization</h2>\n<p>If you write a show rule for references, you can access the referenced\nelement through the <code>element</code> field of the reference. The <code>element</code> may\nbe <code><span class=\"typ-key\">none</span></code> even if it exists if Typst hasn't discovered it yet, so you\nalways need to handle that case in your code.</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\">set</span> math<span class=\"typ-punct\">.</span><span class=\"typ-func\">equation</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\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-func\">ref</span><span class=\"typ-punct\">:</span> it <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">let</span> eq <span class=\"typ-op\">=</span> math<span class=\"typ-punct\">.</span>equation\n  <span class=\"typ-key\">let</span> el <span class=\"typ-op\">=</span> it<span class=\"typ-punct\">.</span>element\n  <span class=\"typ-key\">if</span> el <span class=\"typ-op\">!=</span> <span class=\"typ-key\">none</span> <span class=\"typ-key\">and</span> el<span class=\"typ-punct\">.</span><span class=\"typ-func\">func</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">==</span> eq <span class=\"typ-punct\">{</span>\n    <span class=\"typ-comment\">// Override equation references.</span>\n    <span class=\"typ-func\">link</span><span class=\"typ-punct\">(</span>el<span class=\"typ-punct\">.</span><span class=\"typ-func\">location</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span><span class=\"typ-func\">numbering</span><span class=\"typ-punct\">(</span>\n      el<span class=\"typ-punct\">.</span>numbering<span class=\"typ-punct\">,</span>\n      <span class=\"typ-op\">..</span><span class=\"typ-func\">counter</span><span class=\"typ-punct\">(</span>eq<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span>el<span class=\"typ-punct\">.</span><span class=\"typ-func\">location</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n    <span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">}</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">{</span>\n    <span class=\"typ-comment\">// Other references as usual.</span>\n    it\n  <span class=\"typ-punct\">}</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-heading\">= Beginnings</span> <span class=\"typ-label\">&lt;beginning&gt;</span>\nIn <span class=\"typ-ref\">@beginning</span> we prove <span class=\"typ-ref\">@pythagoras</span>.\n<span class=\"typ-math-delim\">$</span> a<span class=\"typ-math-op\">^</span>2 + b<span class=\"typ-math-op\">^</span>2 = c<span class=\"typ-math-op\">^</span>2 <span class=\"typ-math-delim\">$</span> <span class=\"typ-label\">&lt;pythagoras&gt;</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/ff69119c08e1a5967e92f26ccad7e5ca.png\" alt=\"Preview\"></div></div>","example":null,"self":false,"params":[{"name":"target","details":"<p>The target label that should be referenced.</p>\n<p>Can be a label that is defined in the document or, if the\n<a href=\"/en-US-v0.13.1/reference/model/ref/#parameters-form\"><code>form</code></a> is set to <code>&quot;normal&quot;</code>, an entry from the\n<a href=\"/en-US-v0.13.1/reference/model/bibliography/\" title=\"`bibliography`\"><code>bibliography</code></a>.</p>","example":null,"types":["label"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"supplement","details":"<p>A supplement for the reference.</p>\n<p>If the <a href=\"/en-US-v0.13.1/reference/model/ref/#parameters-form\"><code>form</code></a> is set to <code><span class=\"typ-str\">&quot;normal&quot;</span></code>:</p>\n<ul>\n<li>For references to headings or figures, this is added before the\nreferenced number.</li>\n<li>For citations, this can be used to add a page number.</li>\n</ul>\n<p>If the <a href=\"/en-US-v0.13.1/reference/model/ref/#parameters-form\"><code>form</code></a> is set to <code><span class=\"typ-str\">&quot;page&quot;</span></code>, then this is added\nbefore the page number of the label referenced.</p>\n<p>If a function is specified, it is passed the referenced element and\nshould return content.</p>","example":"<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\">show</span> ref<span class=\"typ-punct\">.</span><span class=\"typ-func\">where</span><span class=\"typ-punct\">(</span>\n  form<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;normal&quot;</span>\n<span class=\"typ-punct\">)</span><span class=\"typ-punct\">:</span> <span class=\"typ-key\">set</span> <span class=\"typ-func\">ref</span><span class=\"typ-punct\">(</span>supplement<span class=\"typ-punct\">:</span> it <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">if</span> it<span class=\"typ-punct\">.</span><span class=\"typ-func\">func</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">==</span> heading <span class=\"typ-punct\">{</span>\n    <span class=\"typ-str\">&quot;Chapter&quot;</span>\n  <span class=\"typ-punct\">}</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">{</span>\n    <span class=\"typ-str\">&quot;Thing&quot;</span>\n  <span class=\"typ-punct\">}</span>\n<span class=\"typ-punct\">}</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= Introduction</span> <span class=\"typ-label\">&lt;intro&gt;</span>\nIn <span class=\"typ-ref\">@intro</span>, we see how to turn\nSections into Chapters. And\nin <span class=\"typ-ref\">@intro<span class=\"typ-punct\">[</span>Part<span class=\"typ-punct\">]</span></span>, it is done\nmanually.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/aa86f60f6632f6ef7a93b80a5a97c7d1.png\" alt=\"Preview\"></div></div>","types":["none","auto","content","function"],"strings":[],"default":"<code><span class=\"typ-key\">auto</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true},{"name":"form","details":"<p>The kind of reference to produce.</p>","example":"<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">page</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\nHere <span class=\"typ-label\">&lt;here&gt;</span> we are on\n<span class=\"typ-func\">#</span><span class=\"typ-func\">ref</span><span class=\"typ-punct\">(</span><span class=\"typ-label\">&lt;here&gt;</span><span class=\"typ-punct\">,</span> form<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;page&quot;</span><span class=\"typ-punct\">)</span>.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/7c0b8deae09773dbb50f5ed89759c240.png\" alt=\"Preview\"></div></div>","types":["str"],"strings":[{"string":"normal","details":"<p>Produces a textual reference to a label.</p>"},{"string":"page","details":"<p>Produces a page reference to a label.</p>"}],"default":"<code><span class=\"typ-str\">&quot;normal&quot;</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":true}],"returns":["content"],"scope":[]}}}