{"route":"/en-US-v0.14.1/tutorial/formatting/","title":"Formatting","description":"Typst's tutorial.","part":null,"outline":[{"id":"set-rules","name":"Set rules","children":[]},{"id":"autocomplete","name":"Autocomplete","children":[]},{"id":"page-setup","name":"Page Setup","children":[]},{"id":"sophistication","name":"Sophistication","children":[]},{"id":"show-rules","name":"Show rules","children":[]},{"id":"review","name":"Review","children":[]}],"body":{"kind":"html","content":"<h1>Formatting</h1>\n<p>So far, you have written a report with some text, a few equations and images.\nHowever, it still looks very plain. Your teaching assistant does not yet know\nthat you are using a new typesetting system, and you want your report to fit in\nwith the other student's submissions. In this chapter, we will see how to format\nyour report using Typst's styling system.</p>\n<h2 id=\"set-rules\">Set rules</h2>\n<p>As we have seen in the previous chapter, Typst has functions that <em>insert</em>\ncontent (e.g. the <a href=\"/en-US-v0.14.1/reference/visualize/image/\" title=\"`image`\"><code>image</code></a> function) and others that <em>manipulate</em> content that\nthey received as arguments (e.g. the <a href=\"/en-US-v0.14.1/reference/layout/align/\" title=\"`align`\"><code>align</code></a> function). The first impulse you\nmight have when you want, for example, to change the font, could be to look\nfor a function that does that and wrap the complete document in it.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>font<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;New Computer Modern&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>\n  <span class=\"typ-heading\">= Background</span>\n  In the case of glaciers, fluid\n  dynamics principles can be used\n  to understand how the movement\n  and behaviour of the ice is\n  influenced by factors such as\n  temperature, pressure, and the\n  presence of other fluids (such as\n  water).\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/331b3b0f46e67088a6a43b6f56bef8ae.png\" alt=\"Preview\"></div></div>\n<p>Wait, shouldn't all arguments of a function be specified within parentheses? Why\nis there a second set of square brackets with content <em>after</em> the parentheses?\nThe answer is that, as passing content to a function is such a common thing to\ndo in Typst, there is special syntax for it: Instead of putting the content\ninside of the argument list, you can write it in square brackets directly after\nthe normal arguments, saving on punctuation.</p>\n<p>As seen above, that works. With the <a href=\"/en-US-v0.14.1/reference/text/text/\" title=\"`text`\"><code>text</code></a> function, we can adjust the font\nfor all text within it. However, wrapping the document in countless functions\nand applying styles selectively and in-situ can quickly become cumbersome.</p>\n<p>Fortunately, Typst has a more elegant solution. With <em>set rules,</em> you can apply\nstyle properties to all occurrences of some kind of content. You write a set\nrule by entering the <code><span class=\"typ-key\">set</span></code> keyword, followed by the name of the function whose\nproperties you want to set, and a list of arguments in parentheses.</p>\n<div class=\"previewed-code\"><pre><code><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\">= Background</span>\nIn the case of glaciers, fluid\ndynamics principles can be used\nto understand how the movement\nand behaviour of the ice is\ninfluenced by factors such as\ntemperature, pressure, and the\npresence of other fluids (such as\nwater).\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/c8049d728c921fe8eac88c71c130e525.png\" alt=\"Preview\"></div></div><div class=\"info-box\">\n<p>Want to know in more technical terms what is happening here?</p>\n<p>Set rules can be conceptualized as setting default values\nfor some of the parameters of a function for all future\nuses of that function.</p>\n</div>\n<h2 id=\"autocomplete\">The autocomplete panel</h2>\n<p>If you followed along and tried a few things in the app, you might have noticed\nthat always after you enter a <code>#</code> character, a panel pops up to show you the\navailable functions, and, within an argument list, the available parameters.\nThat's the autocomplete panel. It can be very useful while you are writing your\ndocument: You can apply its suggestions by hitting the Return key or navigate to\nthe desired completion with the arrow keys. The panel can be dismissed by\nhitting the Escape key and opened again by typing <code>#</code> or hitting\n<kbd>Ctrl</kbd> + <kbd>Space</kbd>. Use the autocomplete panel to discover the\nright arguments for functions. Most suggestions come with a small description of\nwhat they do.</p>\n<p><img src=\"/en-US-v0.14.1/assets/2-formatting-autocomplete.png\" alt=\"Autocomplete panel\" /></p>\n<h2 id=\"page-setup\">Set up the page</h2>\n<p>Back to set rules: When writing a rule, you choose the function depending on\nwhat type of element you want to style. Here is a list of some functions that\nare commonly used in set rules:</p>\n<ul>\n<li><a href=\"/en-US-v0.14.1/reference/text/text/\" title=\"`text`\"><code>text</code></a> to set font family, size, color, and other properties of text</li>\n<li><a href=\"/en-US-v0.14.1/reference/layout/page/\" title=\"`page`\"><code>page</code></a> to set the page size, margins, headers, enable columns, and footers</li>\n<li><a href=\"/en-US-v0.14.1/reference/model/par/\" title=\"`par`\"><code>par</code></a> to justify paragraphs, set line spacing, and more</li>\n<li><a href=\"/en-US-v0.14.1/reference/model/heading/\" title=\"`heading`\"><code>heading</code></a> to set the appearance of headings and enable numbering</li>\n<li><a href=\"/en-US-v0.14.1/reference/model/document/\" title=\"`document`\"><code>document</code></a> to set the metadata contained in the PDF output, such as title\nand author</li>\n</ul>\n<p>Not all function parameters can be set. In general, only parameters that tell\na function <em>how</em> to do something can be set, not those that tell it <em>what</em> to\ndo it with. The function reference pages indicate which parameters are settable.</p>\n<p>Let's add a few more styles to our document. We want larger margins and a serif\nfont. For the purposes of the example, we'll also set another page size.</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>\n  paper<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;a6&quot;</span><span class=\"typ-punct\">,</span>\n  margin<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span>x<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1.8cm</span><span class=\"typ-punct\">,</span> y<span class=\"typ-punct\">:</span> <span class=\"typ-num\">1.5cm</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n<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><span class=\"typ-punct\">,</span>\n  size<span class=\"typ-punct\">:</span> <span class=\"typ-num\">10pt</span>\n<span class=\"typ-punct\">)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">par</span><span class=\"typ-punct\">(</span>\n  justify<span class=\"typ-punct\">:</span> <span class=\"typ-key\">true</span><span class=\"typ-punct\">,</span>\n  leading<span class=\"typ-punct\">:</span> <span class=\"typ-num\">0.52em</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= Introduction</span>\nIn this report, we will explore the\nvarious factors that influence fluid\ndynamics in glaciers and how they\ncontribute to the formation and\nbehaviour of these natural structures.\n\n<span class=\"typ-escape\">...</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">align</span><span class=\"typ-punct\">(</span>center <span class=\"typ-op\">+</span> bottom<span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;glacier.jpg&quot;</span><span class=\"typ-punct\">,</span> width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">70%</span><span class=\"typ-punct\">)</span>\n\n  <span class=\"typ-strong\">*Glaciers form an important\n  part of the earth&#39;s climate\n  system.*</span>\n<span class=\"typ-punct\">]</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/bd7be31b07c68299397a8ed4e0255631.png\" alt=\"Preview\"></div></div>\n<p>There are a few things of note here.</p>\n<p>First is the <a href=\"/en-US-v0.14.1/reference/layout/page/\" title=\"`page`\"><code>page</code></a> set rule. It receives two arguments: the page size and\nmargins for the page. The page size is a string. Typst accepts <a href=\"/en-US-v0.14.1/reference/layout/page/#parameters-paper\">many standard\npage sizes,</a> but you can also specify a custom page size. The\nmargins are specified as a <a href=\"/en-US-v0.14.1/reference/foundations/dictionary/\">dictionary.</a> Dictionaries are a\ncollection of key-value pairs. In this case, the keys are <code>x</code> and <code>y</code>, and the\nvalues are the horizontal and vertical margins, respectively. We could also have\nspecified separate margins for each side by passing a dictionary with the keys\n<code>left</code>, <code>right</code>, <code>top</code>, and <code>bottom</code>.</p>\n<p>Next is the set <a href=\"/en-US-v0.14.1/reference/text/text/\" title=\"`text`\"><code>text</code></a> set rule. Here, we set the font size to <code><span class=\"typ-num\">10pt</span></code> and\nfont family to <code><span class=\"typ-str\">&quot;New Computer Modern&quot;</span></code>. The Typst app comes with many fonts\nthat you can try for your document. When you are in the text function's argument\nlist, you can discover the available fonts in the autocomplete panel.</p>\n<p>We have also set the spacing between lines (a.k.a. leading): It is specified as\na <a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"length\">length</a> value, and we used the <code>em</code> unit to specify the leading relative to\nthe size of the font: <code><span class=\"typ-num\">1em</span></code> is equivalent to the current font size (which\ndefaults to <code><span class=\"typ-num\">11pt</span></code>).</p>\n<p>Finally, we have bottom aligned our image by adding a vertical alignment to our\ncenter alignment. Vertical and horizontal alignments can be combined with the\n<code><span class=\"typ-op\">+</span></code> operator to yield a 2D alignment.</p>\n<h2 id=\"sophistication\">A hint of sophistication</h2>\n<p>To structure our document more clearly, we now want to number our headings. We\ncan do this by setting the <code>numbering</code> parameter of the <a href=\"/en-US-v0.14.1/reference/model/heading/\" title=\"`heading`\"><code>heading</code></a> function.</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\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\n<span class=\"typ-heading\">== Background</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">12</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">== Methods</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">15</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/e16b45d2ef3501cceeac862bc2945d73.png\" alt=\"Preview\"></div></div>\n<p>We specified the string <code><span class=\"typ-str\">&quot;1.&quot;</span></code> as the numbering parameter. This tells Typst to\nnumber the headings with arabic numerals and to put a dot between the number of\neach level. We can also use <a href=\"/en-US-v0.14.1/reference/model/numbering/\">letters, roman numerals, and symbols</a>\nfor our headings:</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.a&quot;</span><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\n<span class=\"typ-heading\">== Background</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">12</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">== Methods</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">15</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/2e5bf40eb67a53e40a7f5be3bb6c0fe1.png\" alt=\"Preview\"></div></div>\n<p>This example also uses the <a href=\"/en-US-v0.14.1/reference/text/lorem/\" title=\"`lorem`\"><code>lorem</code></a> function to generate some placeholder text.\nThis function takes a number as an argument and generates that many words of\n<em>Lorem Ipsum</em> text.</p>\n<div class=\"info-box\">\n<p>Did you wonder why the headings and text set rules apply to all text and headings,\neven if they are not produced with the respective functions?</p>\n<p>Typst internally calls the <code>heading</code> function every time you write\n<code><span class=\"typ-heading\">= Conclusion</span></code>. In fact, the function call <code><span class=\"typ-func\">#</span><span class=\"typ-func\">heading</span><span class=\"typ-punct\">[</span>Conclusion<span class=\"typ-punct\">]</span></code> is\nequivalent to the heading markup above. Other markup elements work similarly,\nthey are only <em>syntax sugar</em> for the corresponding function calls.</p>\n</div>\n<h2 id=\"show-rules\">Show rules</h2>\n<p>You are already pretty happy with how this turned out. But one last thing needs\nto be fixed: The report you are writing is intended for a larger project and\nthat project's name should always be accompanied by a logo, even in prose.</p>\n<p>You consider your options. You could add an <code><span class=\"typ-func\">#</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;logo.svg&quot;</span><span class=\"typ-punct\">)</span></code> call before\nevery instance of the logo using search and replace. That sounds very tedious.\nInstead, you could maybe\n<a href=\"/en-US-v0.14.1/reference/foundations/function/#defining-functions\">define a custom function</a> that always yields the\nlogo with its image. However, there is an even easier way:</p>\n<p>With show rules, you can redefine how Typst displays certain elements. You\nspecify which elements Typst should show differently and how they should look.\nShow rules can be applied to instances of text, many functions, and even the\nwhole document.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;ArtosFlow&quot;</span><span class=\"typ-punct\">:</span> name <span class=\"typ-op\">=&gt;</span> <span class=\"typ-func\">box</span><span class=\"typ-punct\">[</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">box</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">image</span><span class=\"typ-punct\">(</span>\n    <span class=\"typ-str\">&quot;logo.svg&quot;</span><span class=\"typ-punct\">,</span>\n    height<span class=\"typ-punct\">:</span> <span class=\"typ-num\">0.7em</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">name</span>\n<span class=\"typ-punct\">]</span>\n\nThis report is embedded in the\nArtosFlow project. ArtosFlow is a\nproject of the Artos Institute.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/df8f7f22dc78fab4de371cc998da1fbe.png\" alt=\"Preview\"></div></div>\n<p>There is a lot of new syntax in this example: We write the <code><span class=\"typ-key\">show</span></code> keyword,\nfollowed by a string of text we want to show differently and a colon. Then, we\nwrite a function that takes the content that shall be shown as an argument.\nHere, we called that argument <code>name</code>. We can now use the <code>name</code> variable in the\nfunction's body to print the ArtosFlow name. Our show rule adds the logo image\nin front of the name and puts the result into a box to prevent linebreaks from\noccurring between logo and name. The image is also put inside of a box, so that\nit does not appear in its own paragraph.</p>\n<p>The calls to the first box function and the image function did not require a\nleading <code>#</code> because they were not embedded directly in markup. When Typst\nexpects code instead of markup, the leading <code>#</code> is not needed to access\nfunctions, keywords, and variables. This can be observed in parameter lists,\nfunction definitions, and <a href=\"/en-US-v0.14.1/reference/scripting/\">code blocks</a>.</p>\n<h2 id=\"review\">Review</h2>\n<p>You now know how to apply basic formatting to your Typst documents. You learned\nhow to set the font, justify your paragraphs, change the page dimensions, and\nadd numbering to your headings with set rules. You also learned how to use a\nbasic show rule to change how text appears throughout your document.</p>\n<p>You have handed in your report. Your supervisor was so happy with it that they\nwant to adapt it into a conference paper! In the next section, we will learn how\nto format your document as a paper using more advanced show rules and functions.</p>"}}