{"route":"/en-US-v0.14.1/reference/introspection/query/","title":"Query","description":"Documentation for the `query` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"finding-elements","name":"Finding elements","children":[]},{"id":"caution","name":"Caution","children":[]},{"id":"command-line-queries","name":"Command line queries","children":[{"id":"retrieving-a-specific-field","name":"Retrieving a specific field","children":[]},{"id":"querying-for-a-specific-export-target","name":"Querying for a specific export target","children":[]}]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-target","name":"target","children":[]}]}],"body":{"kind":"func","content":{"path":[],"name":"query","title":"Query","keywords":[],"oneliner":"Finds elements in the document.","element":false,"contextual":true,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Finds elements in the document.</p>\n<p>The <code>query</code> function lets you search your document for elements of a\nparticular type or with a particular label. To use it, you first need to\nensure that <a href=\"/en-US-v0.14.1/reference/context/\" title=\"context\">context</a> is available.</p>\n<h2 id=\"finding-elements\">Finding elements</h2>\n<p>In the example below, we manually create a table of contents instead of\nusing the <a href=\"/en-US-v0.14.1/reference/model/outline/\" title=\"`outline`\"><code>outline</code></a> function.</p>\n<p>To do this, we first query for all headings in the document at level 1 and\nwhere <code>outlined</code> is true. Querying only for headings at level 1 ensures\nthat, for the purpose of this example, sub-headings are not included in the\ntable of contents. The <code>outlined</code> field is used to exclude the &quot;Table of\nContents&quot; heading itself.</p>\n<p>Note that we open a <code>context</code> to be able to use the <code>query</code> function.</p>\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\n<span class=\"typ-func\">#</span><span class=\"typ-func\">heading</span><span class=\"typ-punct\">(</span>outlined<span class=\"typ-punct\">:</span> <span class=\"typ-key\">false</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>\n  Table of Contents\n<span class=\"typ-punct\">]</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">let</span> chapters <span class=\"typ-op\">=</span> <span class=\"typ-func\">query</span><span class=\"typ-punct\">(</span>\n    heading<span class=\"typ-punct\">.</span><span class=\"typ-func\">where</span><span class=\"typ-punct\">(</span>\n      level<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">,</span>\n      outlined<span class=\"typ-punct\">:</span> <span class=\"typ-key\">true</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">)</span>\n  <span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">for</span> chapter <span class=\"typ-key\">in</span> chapters <span class=\"typ-punct\">{</span>\n    <span class=\"typ-key\">let</span> loc <span class=\"typ-op\">=</span> chapter<span class=\"typ-punct\">.</span><span class=\"typ-func\">location</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n    <span class=\"typ-key\">let</span> nr <span class=\"typ-op\">=</span> <span class=\"typ-func\">numbering</span><span class=\"typ-punct\">(</span>\n      loc<span class=\"typ-punct\">.</span><span class=\"typ-func\">page-numbering</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n      <span class=\"typ-op\">..</span><span class=\"typ-func\">counter</span><span class=\"typ-punct\">(</span>page<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span>loc<span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">)</span>\n    <span class=\"typ-punct\">[</span><span class=\"typ-pol\">#</span><span class=\"typ-pol\">chapter</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">body</span> <span class=\"typ-func\">#</span><span class=\"typ-func\">h</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">1fr</span><span class=\"typ-punct\">)</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">nr</span> <span class=\"typ-escape\">\\</span> <span class=\"typ-punct\">]</span>\n  <span class=\"typ-punct\">}</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-heading\">= Introduction</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">10</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">pagebreak</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">== Sub-Heading</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">8</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= Discussion</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">18</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/8e8f9e9bb6b78c544e7cd2dd55edf70b-0.png\" alt=\"Preview page 1\"><img src=\"/en-US-v0.14.1/assets/8e8f9e9bb6b78c544e7cd2dd55edf70b-1.png\" alt=\"Preview page 2\"></div></div>\n<p>To get the page numbers, we first get the location of the elements returned\nby <code>query</code> with <a href=\"/en-US-v0.14.1/reference/foundations/content/#definitions-location\"><code>location</code></a>. We then also retrieve the\n<a href=\"/en-US-v0.14.1/reference/introspection/location/#definitions-page-numbering\">page numbering</a> and <a href=\"/en-US-v0.14.1/reference/introspection/counter/#page-counter\">page\ncounter</a> at that location and apply the numbering to\nthe counter.</p>\n<h2 id=\"caution\">A word of caution</h2>\n<p>To resolve all your queries, Typst evaluates and layouts parts of the\ndocument multiple times. However, there is no guarantee that your queries\ncan actually be completely resolved. If you aren't careful a query can\naffect itself—leading to a result that never stabilizes.</p>\n<p>In the example below, we query for all headings in the document. We then\ngenerate as many headings. In the beginning, there's just one heading,\ntitled <code>Real</code>. Thus, <code>count</code> is <code>1</code> and one <code>Fake</code> heading is generated.\nTypst sees that the query's result has changed and processes it again. This\ntime, <code>count</code> is <code>2</code> and two <code>Fake</code> headings are generated. This goes on and\non. As we can see, the output has a finite amount of headings. This is\nbecause Typst simply gives up after a few attempts.</p>\n<p>In general, you should try not to write queries that affect themselves. The\nsame words of caution also apply to other introspection features like\n<a href=\"/en-US-v0.14.1/reference/introspection/counter/\">counters</a> and <a href=\"/en-US-v0.14.1/reference/introspection/state/\" title=\"state\">state</a>.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-heading\">= Real</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">context</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">let</span> elems <span class=\"typ-op\">=</span> <span class=\"typ-func\">query</span><span class=\"typ-punct\">(</span>heading<span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">let</span> count <span class=\"typ-op\">=</span> elems<span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n  count <span class=\"typ-op\">*</span> <span class=\"typ-punct\">[</span><span class=\"typ-heading\">= Fake</span><span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/b66e3cb3bae911d3a0525883200bcf7.png\" alt=\"Preview\"></div></div>\n<h2 id=\"command-line-queries\">Command line queries</h2>\n<p>You can also perform queries from the command line with the <code>typst query</code>\ncommand. This command executes an arbitrary query on the document and\nreturns the resulting elements in serialized form. Consider the following\n<code>example.typ</code> file which contains some invisible <a href=\"/en-US-v0.14.1/reference/introspection/metadata/\" title=\"metadata\">metadata</a>:</p>\n<pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">metadata</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;This is a note&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-label\">&lt;note&gt;</span>\n</code></pre>\n<p>You can execute a query on it as follows using Typst's CLI:</p>\n<pre style=\"background-color:#ffffff;\">\n<span style=\"color:#4b69c6;\">$</span><span style=\"color:#000000;\"> typst query example.typ </span><span style=\"color:#198810;\">&quot;&lt;note&gt;&quot;\n</span><span style=\"color:#4b69c6;\">[\n</span><span style=\"color:#000000;\">  {\n</span><span style=\"color:#000000;\">    </span><span style=\"color:#198810;\">&quot;func&quot;</span><span style=\"color:#4b69c6;\">: </span><span style=\"color:#198810;\">&quot;metadata&quot;</span><span style=\"color:#000000;\">,\n</span><span style=\"color:#000000;\">    </span><span style=\"color:#198810;\">&quot;value&quot;</span><span style=\"color:#4b69c6;\">: </span><span style=\"color:#198810;\">&quot;This is a note&quot;</span><span style=\"color:#000000;\">,\n</span><span style=\"color:#000000;\">    </span><span style=\"color:#198810;\">&quot;label&quot;</span><span style=\"color:#4b69c6;\">: </span><span style=\"color:#198810;\">&quot;&lt;note&gt;&quot;\n</span><span style=\"color:#000000;\">  }\n</span><span style=\"color:#4b69c6;\">]\n</span></pre>\n<h3 id=\"retrieving-a-specific-field\">Retrieving a specific field</h3>\n<p>Frequently, you're interested in only one specific field of the resulting\nelements. In the case of the <code>metadata</code> element, the <code>value</code> field is the\ninteresting one. You can extract just this field with the <code>--field</code>\nargument.</p>\n<pre style=\"background-color:#ffffff;\">\n<span style=\"color:#4b69c6;\">$</span><span style=\"color:#000000;\"> typst query example.typ </span><span style=\"color:#198810;\">&quot;&lt;note&gt;&quot;</span><span style=\"color:#000000;\"> --field value\n</span><span style=\"color:#4b69c6;\">[</span><span style=\"color:#198810;\">&quot;This is a note&quot;</span><span style=\"color:#4b69c6;\">]\n</span></pre>\n<p>If you are interested in just a single element, you can use the <code>--one</code>\nflag to extract just it.</p>\n<pre style=\"background-color:#ffffff;\">\n<span style=\"color:#4b69c6;\">$</span><span style=\"color:#000000;\"> typst query example.typ </span><span style=\"color:#198810;\">&quot;&lt;note&gt;&quot;</span><span style=\"color:#000000;\"> --field value --one\n</span><span style=\"color:#198810;\">&quot;This is a note&quot;\n</span></pre>\n<h3 id=\"querying-for-a-specific-export-target\">Querying for a specific export target</h3>\n<p>In case you need to query a document when exporting for a specific target,\nyou can use the <code>--target</code> argument. Valid values are <code>paged</code>, and <code>html</code>\n(if the <a href=\"/en-US-v0.14.1/reference/html/\" title=\"`html`\"><code>html</code></a> feature is enabled).</p>"}],"self":false,"params":[{"name":"target","details":[{"kind":"html","content":"<p>Can be</p>\n<ul>\n<li>an element function like a <code>heading</code> or <code>figure</code>,</li>\n<li>a <code><span class=\"typ-label\">&lt;label&gt;</span></code>,</li>\n<li>a more complex selector like <code>heading<span class=\"typ-punct\">.</span><span class=\"typ-func\">where</span><span class=\"typ-punct\">(</span>level<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1</span><span class=\"typ-punct\">)</span></code>,</li>\n<li>or <code><span class=\"typ-func\">selector</span><span class=\"typ-punct\">(</span>heading<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">before</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>.</li>\n</ul>\n<p>Only <a href=\"/en-US-v0.14.1/reference/introspection/location/#locatable\">locatable</a> element functions are supported.</p>"}],"types":["label","selector","location","function"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["array"],"scope":[]}}}