{"route":"/en-US-v0.13.1/reference/styling/","title":"Styling","description":"All concepts needed to style your document with Typst.","part":null,"outline":[{"id":"set-rules","name":"Set rules","children":[]},{"id":"show-rules","name":"Show rules","children":[]}],"body":{"kind":"html","content":"<h1>Styling</h1>\n<p>Typst includes a flexible styling system that automatically applies styling of\nyour choice to your document. With <em>set rules,</em> you can configure basic\nproperties of elements. This way, you create most common styles. However, there\nmight not be a built-in property for everything you wish to do. For this reason,\nTypst further supports <em>show rules</em> that can completely redefine the appearance\nof elements.</p>\n<h2 id=\"set-rules\">Set rules</h2>\n<p>With set rules, you can customize the appearance of elements. They are written\nas a <a href=\"/en-US-v0.13.1/reference/foundations/function/\">function call</a> to an <a href=\"/en-US-v0.13.1/reference/foundations/function/#element-functions\">element\nfunction</a> preceded by the <code><span class=\"typ-key\">set</span></code> keyword (or\n<code><span class=\"typ-key\">#</span><span class=\"typ-key\">set</span></code> in markup). Only optional parameters of that function can be provided\nto the set rule. Refer to each function's documentation to see which parameters\nare optional. In the example below, we use two set rules to change the\n<a href=\"/en-US-v0.13.1/reference/text/text/#parameters-font\">font family</a> and <a href=\"/en-US-v0.13.1/reference/model/heading/#parameters-numbering\">heading numbering</a>.</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;I.&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>\n  font<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;New Computer Modern&quot;</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= Introduction</span>\nWith set rules, you can style\nyour document.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/9d6d1565eca1266b69c1e10e8c947f7e.png\" alt=\"Preview\"></div></div>\n<p>A top level set rule stays in effect until the end of the file. When nested\ninside of a block, it is only in effect until the end of that block. With a\nblock, you can thus restrict the effect of a rule to a particular segment of\nyour document. Below, we use a content block to scope the list styling to one\nparticular list.</p>\n<div class=\"previewed-code\"><pre><code>This list is affected: <span class=\"typ-punct\">#</span><span class=\"typ-punct\">[</span>\n  <span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">list</span><span class=\"typ-punct\">(</span>marker<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span><span class=\"typ-escape\">--</span><span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-marker\">-</span> Dash\n<span class=\"typ-punct\">]</span>\n\nThis one is not:\n<span class=\"typ-marker\">-</span> Bullet\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/e9c9106d715f7f5cc305c7567b35f1a6.png\" alt=\"Preview\"></div></div>\n<p>Sometimes, you'll want to apply a set rule conditionally. For this, you can use\na <em>set-if</em> rule.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">task</span><span class=\"typ-punct\">(</span>body<span class=\"typ-punct\">,</span> critical<span class=\"typ-punct\">:</span> <span class=\"typ-key\">false</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>red<span class=\"typ-punct\">)</span> <span class=\"typ-key\">if</span> critical\n  <span class=\"typ-punct\">[</span><span class=\"typ-marker\">-</span> <span class=\"typ-pol\">#</span><span class=\"typ-pol\">body</span><span class=\"typ-punct\">]</span>\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">task</span><span class=\"typ-punct\">(</span>critical<span class=\"typ-punct\">:</span> <span class=\"typ-key\">true</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>Food today?<span class=\"typ-punct\">]</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">task</span><span class=\"typ-punct\">(</span>critical<span class=\"typ-punct\">:</span> <span class=\"typ-key\">false</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>Work deadline<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/fd4966a8439dae633a77e390f53b005f.png\" alt=\"Preview\"></div></div>\n<h2 id=\"show-rules\">Show rules</h2>\n<p>With show rules, you can deeply customize the look of a type of element. The\nmost basic form of show rule is a <em>show-set rule.</em> Such a rule is written as the\n<code><span class=\"typ-key\">show</span></code> keyword followed by a <a href=\"/en-US-v0.13.1/reference/foundations/selector/\" title=\"selector\">selector</a>, a colon and then a set rule. The most\nbasic form of selector is an <a href=\"/en-US-v0.13.1/reference/foundations/function/#element-functions\">element function</a>.\nThis lets the set rule only apply to the selected element. In the example below,\nheadings become dark blue while all other text stays black.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-func\">heading</span><span class=\"typ-punct\">:</span> <span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>navy<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= This is navy-blue</span>\nBut this stays black.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/d2d8f7b75d586130c5564fd7947e349.png\" alt=\"Preview\"></div></div>\n<p>With show-set rules you can mix and match properties from different functions to\nachieve many different effects. But they still limit you to what is predefined\nin Typst. For maximum flexibility, you can instead write a show rule that\ndefines how to format an element from scratch. To write such a show rule,\nreplace the set rule after the colon with an arbitrary <a href=\"/en-US-v0.13.1/reference/foundations/function/\" title=\"function\">function</a>. This function\nreceives the element in question and can return arbitrary content. The available\n<a href=\"/en-US-v0.13.1/reference/scripting/#fields\">fields</a> on the element passed to the function again match\nthe parameters of the respective element function. Below, we define a show rule\nthat formats headings for a fantasy encyclopedia.</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;(I)&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-func\">heading</span><span class=\"typ-punct\">:</span> it <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">[</span>\n  <span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">align</span><span class=\"typ-punct\">(</span>center<span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>font<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Inria Serif&quot;</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-escape\">\\~</span> <span class=\"typ-func\">#</span><span class=\"typ-func\">emph</span><span class=\"typ-punct\">(</span>it<span class=\"typ-punct\">.</span>body<span class=\"typ-punct\">)</span>\n     <span class=\"typ-func\">#</span><span class=\"typ-func\">counter</span><span class=\"typ-punct\">(</span>heading<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">display</span><span class=\"typ-punct\">(</span>\n       it<span class=\"typ-punct\">.</span>numbering\n     <span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\~</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-heading\">= Dragon</span>\nWith a base health of 15, the\ndragon is the most powerful\ncreature.\n\n<span class=\"typ-heading\">= Manticore</span>\nWhile less powerful than the\ndragon, the manticore gets\nextra style points.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/62bbe4aa94b0a082e3baa01eaf3c3d08.png\" alt=\"Preview\"></div></div>\n<p>Like set rules, show rules are in effect until the end of the current block or\nfile.</p>\n<p>Instead of a function, the right-hand side of a show rule can also take a\nliteral string or content block that should be directly substituted for the\nelement. And apart from a function, the left-hand side of a show rule can also\ntake a number of other <em>selectors</em> that define what to apply the transformation\nto:</p>\n<ul>\n<li>\n<p><strong>Everything:</strong> <code><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> rest <span class=\"typ-op\">=&gt;</span> ..</code> <br />\nTransform everything after the show rule. This is useful to apply a more\ncomplex layout to your whole document without wrapping everything in a giant\nfunction call.</p>\n</li>\n<li>\n<p><strong>Text:</strong> <code><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;Text&quot;</span><span class=\"typ-punct\">:</span> ..</code> <br />\nStyle, transform or replace text.</p>\n</li>\n<li>\n<p><strong>Regex:</strong> <code><span class=\"typ-key\">show</span> <span class=\"typ-func\">regex</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;\\w+&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">:</span> ..</code> <br />\nSelect and transform text with a regular expression for even more flexibility.\nSee the documentation of the <a href=\"/en-US-v0.13.1/reference/foundations/regex/\"><code>regex</code> type</a> for details.</p>\n</li>\n<li>\n<p><strong>Function with fields:</strong> <code><span class=\"typ-key\">show</span> 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><span class=\"typ-punct\">:</span> ..</code> <br />\nTransform only elements that have the specified fields. For example, you might\nwant to only change the style of level-1 headings.</p>\n</li>\n<li>\n<p><strong>Label:</strong> <code><span class=\"typ-key\">show</span> <span class=\"typ-label\">&lt;intro&gt;</span><span class=\"typ-punct\">:</span> ..</code> <br />\nSelect and transform elements that have the specified label. See the\ndocumentation of the <a href=\"/en-US-v0.13.1/reference/foundations/label/\"><code>label</code> type</a> for more details.</p>\n</li>\n</ul>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;Project&quot;</span><span class=\"typ-punct\">:</span> <span class=\"typ-func\">smallcaps</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;badly&quot;</span><span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;great&quot;</span>\n\nWe started Project in 2019\nand are still working on it.\nProject is progressing badly.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/341cc85624eca5d9cf86c6e8dd61832c.png\" alt=\"Preview\"></div></div>"}}