{"route":"/en-US-v0.14.1/tutorial/making-a-template/","title":"Making a Template","description":"Typst's tutorial.","part":null,"outline":[{"id":"variables","name":"Variables","children":[]},{"id":"toy-template","name":"Toy Template","children":[]},{"id":"set-and-show-rules","name":"Set And Show Rules","children":[]},{"id":"named-arguments","name":"Named Arguments","children":[]},{"id":"separate-file","name":"Separate File","children":[]},{"id":"review","name":"Review","children":[]}],"body":{"kind":"html","content":"<h1>Making a Template</h1>\n<p>In the previous three chapters of this tutorial, you have learned how to write a\ndocument in Typst, apply basic styles, and customize its appearance in-depth to\ncomply with a publisher's style guide. Because the paper you wrote in the\nprevious chapter was a tremendous success, you have been asked to write a\nfollow-up article for the same conference. This time, you want to take the style\nyou created in the previous chapter and turn it into a reusable template. In\nthis chapter you will learn how to create a template that you and your team can\nuse with just one show rule. Let's get started!</p>\n<h2 id=\"variables\">Reusing data with variables</h2>\n<p>In the past chapters, most of the content of the document was entered by hand.\nIn the third chapter, we used the <code>document</code> element and context to cut down on\nrepetition and only enter the title once. But in practice, there may be many\nmore things that occur multiple times in your document. There are multiple good\nreasons to just define these repeated values once:</p>\n<ol>\n<li>It makes changing them later easier</li>\n<li>It allows you to quickly find all instances where you used something</li>\n<li>It makes it easy to be consistent throughout</li>\n<li>For long or hard-to-enter repeated segments, a shorter variable name is often\nmore convenient to type</li>\n</ol>\n<p>If you were using a conventional word processor, you might resort to using a\nplaceholder value that you can later search for. In Typst, however, you can\ninstead use variables to safely store content and reuse it across your whole\ndocument through a variable name.</p>\n<p>The technique of using context to reproduce an element's property we have\nlearned earlier is not always the most appropriate for this: Typst's built-in\nelements focus on semantic properties like the title and description of a\ndocument, or things that directly relate to typesetting, like the text size.</p>\n<p>For our example, we want to take a look at Typst's pronunciation. One of the\nbest ways to transcribe pronunciation is the International Phonetic Alphabet\n(IPA). But because it uses characters not found on common keyboards, typing IPA\nrepeatedly can become cumbersome. So let's instead define a variable that we can\nreference multiple times.</p>\n<pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> ipa <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>taɪpst<span class=\"typ-punct\">]</span>\n</code></pre>\n<p>Here, we use a new keyword, <code><span class=\"typ-key\">let</span></code>, to indicate a variable definition. Then,\nwe put the name of our variable, in this case, <code>ipa</code>. Finally, we type an equals\nsign and the value of our variable. It is enclosed in square brackets because\nit is content, mirroring how you would call a function accepting content. In\nother words, this syntax mirrors the phrase <em>&quot;Let the variable <code>ipa</code> have the\nvalue <code><span class=\"typ-punct\">[</span>taɪpst<span class=\"typ-punct\">]</span></code>.&quot;</em></p>\n<p>Now, we can use the variable in our document:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> ipa <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span>taɪpst<span class=\"typ-punct\">]</span>\n\nThe canonical way to\npronounce Typst is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">ipa</span>.\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">table</span><span class=\"typ-punct\">(</span>\n  columns<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1fr</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">1fr</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">[</span>Name<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> <span class=\"typ-punct\">[</span>Typst<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">[</span>Pronunciation<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> ipa<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/1a33d218e9ec4ec2b683baca85e512e7.png\" alt=\"Preview\"></div></div>\n<p>In the example, you can see that the variable can be used both in markup\n(prefixed with a <code>#</code>) and in a function call (by just typing its name). Of\ncourse, we can change the value of the variable and all its occurrences will\nautomatically change with it. Let's make it a bit clearer what is IPA and what\nis normal prose by rendering IPA in italics. We are also using slashes which, by\nconvention, often enclose IPA.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> ipa <span class=\"typ-op\">=</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>\n  style<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;italic&quot;</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>/taɪpst/<span class=\"typ-punct\">]</span>\n\nThe canonical way to\npronounce Typst is <span class=\"typ-pol\">#</span><span class=\"typ-pol\">ipa</span>.\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">table</span><span class=\"typ-punct\">(</span>\n  columns<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1fr</span><span class=\"typ-punct\">,</span> <span class=\"typ-num\">1fr</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">[</span>Name<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> <span class=\"typ-punct\">[</span>Typst<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">[</span>Pronunciation<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span> ipa<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/ef87ca8429247c40169a6c3d07584d47.png\" alt=\"Preview\"></div></div>\n<p>Here, we called the text function and assigned its <em>return value</em> to the\nvariable. When you call a function, it processes its arguments and then yields\nanother value (often content). So far in this tutorial, we called most\nfunctions directly in markup, like this: <code><span class=\"typ-func\">#</span><span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>fill<span class=\"typ-punct\">:</span> red<span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>CRIMSON!<span class=\"typ-punct\">]</span></code>. This\ncall to the text function returns the red text as a return value. Because we\nplaced it in markup, its return value just immediately got inserted into the\ncontent we wrote. With variables, we can instead store it to use it later or\ncompose it with other values.</p>\n<p>Variables are not limited to storing content: they can store any data type Typst\nknows about. Throughout this tutorial, you made use of many data types when you\npassed them to Typst's built-in functions. Here is an example assigning each of\nthem to a variable:</p>\n<pre><code><span class=\"typ-comment\">// Content with markup inside</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> blind-text <span class=\"typ-op\">=</span> <span class=\"typ-punct\">[</span><span class=\"typ-emph\">_Lorem ipsum_</span> dolor sit amet<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-comment\">// Unformatted strings</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> funny-font <span class=\"typ-op\">=</span> <span class=\"typ-str\">&quot;MS Comic Sans&quot;</span>\n\n<span class=\"typ-comment\">// Absolute lengths (see also pt, in, ...)</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> mile <span class=\"typ-op\">=</span> <span class=\"typ-num\">160934cm</span>\n\n<span class=\"typ-comment\">// Lengths relative to the font size</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> double-space <span class=\"typ-op\">=</span> <span class=\"typ-num\">2em</span>\n\n<span class=\"typ-comment\">// Ratios</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> progress <span class=\"typ-op\">=</span> <span class=\"typ-num\">80%</span>\n\n<span class=\"typ-comment\">// Integer numbers</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> answer <span class=\"typ-op\">=</span> <span class=\"typ-num\">42</span>\n\n<span class=\"typ-comment\">// Booleans</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> truth <span class=\"typ-op\">=</span> <span class=\"typ-key\">false</span>\n\n<span class=\"typ-comment\">// Horizontal and vertical alignment</span>\n<span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> focus <span class=\"typ-op\">=</span> center\n</code></pre>\n<p>In this chapter of the tutorial, you will leverage variables and your own\nfunctions to build templates that can be reused across multiple documents.</p>\n<h2 id=\"toy-template\">A toy template</h2>\n<p>In Typst, templates are functions in which you can wrap your whole document. To\nlearn how to do that, let's first review how to write your very own functions.\nThey can do anything you want them to, so why not go a bit crazy?</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">amazed</span><span class=\"typ-punct\">(</span>term<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-func\">box</span><span class=\"typ-punct\">[</span>✨ <span class=\"typ-pol\">#</span><span class=\"typ-pol\">term</span> ✨<span class=\"typ-punct\">]</span>\n\nYou are <span class=\"typ-func\">#</span><span class=\"typ-func\">amazed</span><span class=\"typ-punct\">[</span>beautiful<span class=\"typ-punct\">]</span>!\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/85ffb432ec9336d10dbea32ea93e4896.png\" alt=\"Preview\"></div></div>\n<p>Comparing this against the previous section, you may have noticed that this\nlooks a lot like a variable definition using <code><span class=\"typ-key\">let</span></code>. This instinct is correct:\nFunctions are just another data type. Here, we are defining the variable\n<code>amazed</code>, assigning it a function that takes a single argument, <code>term</code>, and\nreturns content with the <code>term</code> surrounded by sparkles. We also put the whole\nthing in a <a href=\"/en-US-v0.14.1/reference/layout/box/\" title=\"`box`\"><code>box</code></a> so that the term we are amazed by cannot be separated from\nits sparkles by a line break. The special function definition syntax makes the\ndefinition shorter and more readable, but you can also use the regular variable\ndefinition syntax (see <a href=\"/en-US-v0.14.1/reference/scripting/#bindings\">the scripting reference</a> for\ndetails). After its definition, we are able to call the function just like all\nbuilt-in functions.</p>\n<p>Many functions that come with Typst have optional named parameters. Our\nfunctions can also have them. Let's add a parameter to our function that lets us\nchoose the color of the text. We need to provide a default color in case the\nparameter isn't given.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">amazed</span><span class=\"typ-punct\">(</span>term<span class=\"typ-punct\">,</span> color<span class=\"typ-punct\">:</span> blue<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span>color<span class=\"typ-punct\">,</span> <span class=\"typ-func\">box</span><span class=\"typ-punct\">[</span>✨ <span class=\"typ-pol\">#</span><span class=\"typ-pol\">term</span> ✨<span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">}</span>\n\nYou are <span class=\"typ-func\">#</span><span class=\"typ-func\">amazed</span><span class=\"typ-punct\">[</span>beautiful<span class=\"typ-punct\">]</span>!\nI am <span class=\"typ-func\">#</span><span class=\"typ-func\">amazed</span><span class=\"typ-punct\">(</span>color<span class=\"typ-punct\">:</span> purple<span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>amazed<span class=\"typ-punct\">]</span>!\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/de3b1f5b9b2c4f6a9672c242956d317.png\" alt=\"Preview\"></div></div>\n<p>Templates now work by wrapping our whole document in a custom function like\n<code>amazed</code>. But wrapping a whole document in a giant function call would be\ncumbersome! Instead, we can use an &quot;everything&quot; show rule to achieve the same\nwith cleaner code. To write such a show rule, put a colon directly after the\nshow keyword and then provide a function. This function is given the rest of the\ndocument as a parameter. The function can then do anything with this content.\nSince the <code>amazed</code> function can be called with a single content argument, we can\njust pass it by name to the show rule. Let's try it:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> <span class=\"typ-func\">amazed</span>\nI choose to focus on the good\nin my life and let go of any\nnegative thoughts or beliefs.\nIn fact, I am amazing!\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/808bff8bf2db750d153f0acbf0b0fbf1.png\" alt=\"Preview\"></div></div>\n<p>Our whole document will now be passed to the <code>amazed</code> function, as if we wrapped\nit around it. Of course, this is not especially useful with this particular\nfunction, but when combined with set rules and named arguments, it can be very\npowerful.</p>\n<h2 id=\"set-and-show-rules\">Embedding set and show rules</h2>\n<p>To apply some set and show rules to our template, we can use <code>set</code> and <code>show</code>\nwithin a content block in our function and then insert the document into\nthat content block.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">template</span><span class=\"typ-punct\">(</span>doc<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</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>font<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Inria Serif&quot;</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">#</span><span class=\"typ-key\">show</span> <span class=\"typ-str\">&quot;something cool&quot;</span><span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>Typst<span class=\"typ-punct\">]</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">doc</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>\nI am learning something cool today.\nIt&#39;s going great so far!\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/3e1c39dbdd957964b7524749bf0cfd5.png\" alt=\"Preview\"></div></div>\n<p>Just like we already discovered in the previous chapter, set rules will apply to\neverything within their content block. Since the everything show rule passes our\nwhole document to the <code>template</code> function, the text set rule and string show\nrule in our template will apply to the whole document. Let's use this knowledge\nto create a template that reproduces the body style of the paper we wrote in the\nprevious chapter.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">conf</span><span class=\"typ-punct\">(</span>title<span class=\"typ-punct\">,</span> doc<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\">page</span><span class=\"typ-punct\">(</span>\n    paper<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;us-letter&quot;</span><span class=\"typ-punct\">,</span>\n    header<span class=\"typ-punct\">:</span> <span class=\"typ-func\">align</span><span class=\"typ-punct\">(</span>\n      right <span class=\"typ-op\">+</span> horizon<span class=\"typ-punct\">,</span>\n      title\n    <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n    columns<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-op\">..</span><span class=\"typ-punct\">.</span>\n  <span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">set</span> <span class=\"typ-func\">par</span><span class=\"typ-punct\">(</span>justify<span class=\"typ-punct\">:</span> <span class=\"typ-key\">true</span><span class=\"typ-punct\">)</span>\n  <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;Libertinus Serif&quot;</span><span class=\"typ-punct\">,</span>\n    size<span class=\"typ-punct\">:</span> <span class=\"typ-num\">11pt</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span>\n\n  <span class=\"typ-comment\">// Heading show rules.</span>\n  ...\n\n  doc\n<span class=\"typ-punct\">}</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> doc <span class=\"typ-op\">=&gt;</span> <span class=\"typ-func\">conf</span><span class=\"typ-punct\">(</span>\n  <span class=\"typ-punct\">[</span>Paper title<span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  doc<span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</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.14.1/assets/400d52d31bee3faf506dfff0afe445fc.png\" alt=\"Preview\"></div></div>\n<p>We copy-pasted most of that code from the previous chapter. The two differences\nare this:</p>\n<ol>\n<li>\n<p>We wrapped everything in the function <code>conf</code> using an everything show rule.\nThe function applies a few set and show rules and echoes the content it has\nbeen passed at the end.</p>\n</li>\n<li>\n<p>Moreover, we used a curly-braced code block instead of a content block. This\nway, we don't need to prefix all set rules and function calls with a <code>#</code>. In\nexchange, we cannot write markup directly in the code block anymore.</p>\n</li>\n</ol>\n<p>Also note where the title comes from: We previously had it inside of a variable.\nNow, we are receiving it as the first parameter of the template function. To do\nso, we passed a closure (that's a function without a name that is used right\naway) to the everything show rule. We did that because the <code>conf</code> function\nexpects two positional arguments, the title and the body, but the show rule will\nonly pass the body. Therefore, we add a new function definition that allows us\nto set a paper title and use the single parameter from the show rule.</p>\n<h2 id=\"named-arguments\">Templates with named arguments</h2>\n<p>Our paper in the previous chapter had a title and an author list. Let's add\nthese things to our template. In addition to the title, we want our template to\naccept a list of authors with their affiliations and the paper's abstract. To\nkeep things readable, we'll add those as named arguments. In the end, we want it\nto work like this:</p>\n<pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> doc <span class=\"typ-op\">=&gt;</span> <span class=\"typ-func\">conf</span><span class=\"typ-punct\">(</span>\n  title<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>\n    A Fluid Dynamic Model for\n    Glacier Flow\n  <span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  authors<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span>\n    <span class=\"typ-punct\">(</span>\n      name<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Theresa Tungsten&quot;</span><span class=\"typ-punct\">,</span>\n      affiliation<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Artos Institute&quot;</span><span class=\"typ-punct\">,</span>\n      email<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;tung@artos.edu&quot;</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">(</span>\n      name<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Eugene Deklan&quot;</span><span class=\"typ-punct\">,</span>\n      affiliation<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Honduras State&quot;</span><span class=\"typ-punct\">,</span>\n      email<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;e.deklan@hstate.hn&quot;</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-punct\">,</span>\n  abstract<span class=\"typ-punct\">:</span> <span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">80</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  doc<span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-escape\">...</span>\n</code></pre>\n<p>Let's build this new template function. First, we add a default value to the\n<code>title</code> argument. This way, we can call the template without specifying a title.\nWe also add the named <code>authors</code> and <code>abstract</code> parameters with empty defaults.\nNext, we copy the code that generates title, abstract and authors from the\nprevious chapter into the template, replacing the fixed details with the\nparameters.</p>\n<p>The new <code>authors</code> parameter expects an <a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"array\">array</a> of <a href=\"/en-US-v0.14.1/reference/foundations/dictionary/\">dictionaries</a>\nwith the keys <code>name</code>, <code>affiliation</code> and <code>email</code>. Because we can have an\narbitrary number of authors, we dynamically determine if we need one, two or\nthree columns for the author list. First, we determine the number of authors\nusing the <a href=\"/en-US-v0.14.1/reference/foundations/array/#definitions-len\"><code>.len()</code></a> method on the <code>authors</code> array. Then, we set the\nnumber of columns as the minimum of this count and three, so that we never\ncreate more than three columns. If there are more than three authors, a new row\nwill be inserted instead. For this purpose, we have also added a <code>row-gutter</code>\nparameter to the <code>grid</code> function. Otherwise, the rows would be too close\ntogether. To extract the details about the authors from the dictionary, we use\nthe <a href=\"/en-US-v0.14.1/reference/scripting/#fields\">field access syntax</a>.</p>\n<p>We still have to provide an argument to the grid for each author: Here is where\nthe array's <a href=\"/en-US-v0.14.1/reference/foundations/array/#definitions-map\"><code>map</code> method</a> comes in handy. It takes a function as an\nargument that gets called with each item of the array. We pass it a function\nthat formats the details for each author and returns a new array containing\ncontent values. We've now got one array of values that we'd like to use as\nmultiple arguments for the grid. We can do that by using the\n<a href=\"/en-US-v0.14.1/reference/foundations/arguments/\"><code>spread</code> operator</a>. It takes an array and applies each of its items\nas a separate argument to the function.</p>\n<p>The resulting template function looks like this:</p>\n<pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">conf</span><span class=\"typ-punct\">(</span>\n  authors<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  abstract<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span><span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n  doc<span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-punct\">{</span>\n  <span class=\"typ-comment\">// Set and show rules from before.</span>\n  ...\n\n  <span class=\"typ-func\">place</span><span class=\"typ-punct\">(</span>\n    top <span class=\"typ-op\">+</span> center<span class=\"typ-punct\">,</span>\n    float<span class=\"typ-punct\">:</span> <span class=\"typ-key\">true</span><span class=\"typ-punct\">,</span>\n    scope<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;parent&quot;</span><span class=\"typ-punct\">,</span>\n    clearance<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2em</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">{</span>\n      <span class=\"typ-func\">title</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n\n      <span class=\"typ-key\">let</span> count <span class=\"typ-op\">=</span> authors<span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span>\n      <span class=\"typ-key\">let</span> ncols <span class=\"typ-op\">=</span> calc<span class=\"typ-punct\">.</span><span class=\"typ-func\">min</span><span class=\"typ-punct\">(</span>count<span class=\"typ-punct\">,</span> <span class=\"typ-num\">3</span><span class=\"typ-punct\">)</span>\n      <span class=\"typ-func\">grid</span><span class=\"typ-punct\">(</span>\n        columns<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span><span class=\"typ-num\">1fr</span><span class=\"typ-punct\">,</span><span class=\"typ-punct\">)</span> <span class=\"typ-op\">*</span> ncols<span class=\"typ-punct\">,</span>\n        row-gutter<span class=\"typ-punct\">:</span> <span class=\"typ-num\">24pt</span><span class=\"typ-punct\">,</span>\n        <span class=\"typ-op\">..</span>authors<span class=\"typ-punct\">.</span><span class=\"typ-func\">map</span><span class=\"typ-punct\">(</span>author <span class=\"typ-op\">=&gt;</span> <span class=\"typ-punct\">[</span>\n          <span class=\"typ-pol\">#</span><span class=\"typ-pol\">author</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">name</span> <span class=\"typ-escape\">\\</span>\n          <span class=\"typ-pol\">#</span><span class=\"typ-pol\">author</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">affiliation</span> <span class=\"typ-escape\">\\</span>\n          <span class=\"typ-func\">#</span><span class=\"typ-func\">link</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;mailto:&quot;</span> <span class=\"typ-op\">+</span> author<span class=\"typ-punct\">.</span>email<span class=\"typ-punct\">)</span>\n        <span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n      <span class=\"typ-punct\">)</span>\n\n      <span class=\"typ-func\">par</span><span class=\"typ-punct\">(</span>justify<span class=\"typ-punct\">:</span> <span class=\"typ-key\">false</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">[</span>\n        <span class=\"typ-strong\">*Abstract*</span> <span class=\"typ-escape\">\\</span>\n        <span class=\"typ-pol\">#</span><span class=\"typ-pol\">abstract</span>\n      <span class=\"typ-punct\">]</span>\n\n    <span class=\"typ-punct\">}</span>\n  <span class=\"typ-punct\">)</span>\n\n  doc\n<span class=\"typ-punct\">}</span>\n</code></pre>\n<h2 id=\"separate-file\">A separate file</h2>\n<p>Most of the time, a template is specified in a different file and then imported\ninto the document. This way, the main file you write in is kept clutter free and\nyour template is easily reused. Create a new text file in the file panel by\nclicking the plus button and name it <code>conf.typ</code>. Move the <code>conf</code> function\ndefinition inside of that new file. Now you can access it from your main file by\nadding an import before the show rule. Specify the path of the file between the\n<code><span class=\"typ-key\">import</span></code> keyword and a colon, then name the function that you want to import.</p>\n<p>Another thing that you can do to make applying templates just a bit more elegant\nis to use the <a href=\"/en-US-v0.14.1/reference/foundations/function/#definitions-with\"><code>.with</code></a> method on functions to pre-populate all\nthe named arguments. This way, you can avoid spelling out a closure and\nappending the content argument at the bottom of your template list. Templates on\n<a href=\"https://typst.app/universe/\">Typst Universe</a> are designed to work with this style of function\ncall.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">import</span> <span class=\"typ-str\">&quot;conf.typ&quot;</span><span class=\"typ-punct\">:</span> conf\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">document</span><span class=\"typ-punct\">(</span>title<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>\n  A Fluid Dynamic Model for\n  Glacier Flow\n<span class=\"typ-punct\">]</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-key\">#</span><span class=\"typ-key\">show</span><span class=\"typ-punct\">:</span> conf<span class=\"typ-punct\">.</span><span class=\"typ-func\">with</span><span class=\"typ-punct\">(</span>\n  authors<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">(</span>\n    <span class=\"typ-punct\">(</span>\n      name<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Theresa Tungsten&quot;</span><span class=\"typ-punct\">,</span>\n      affiliation<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Artos Institute&quot;</span><span class=\"typ-punct\">,</span>\n      email<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;tung@artos.edu&quot;</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">(</span>\n      name<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Eugene Deklan&quot;</span><span class=\"typ-punct\">,</span>\n      affiliation<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Honduras State&quot;</span><span class=\"typ-punct\">,</span>\n      email<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;e.deklan@hstate.hn&quot;</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-punct\">,</span>\n  abstract<span class=\"typ-punct\">:</span> <span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">80</span><span class=\"typ-punct\">)</span><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\">90</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">== Motivation</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">140</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">== Problem Statement</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">50</span><span class=\"typ-punct\">)</span>\n\n<span class=\"typ-heading\">= Related Work</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">lorem</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">200</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/ce3f3a18593a1c2ac64ee6bee62da151.png\" alt=\"Preview\"></div></div>\n<p>We have now converted the conference paper into a reusable template for that\nconference! Why not share it in the <a href=\"https://forum.typst.app/\">Forum</a> or on\n<a href=\"https://discord.gg/2uDybryKPe\">Typst's Discord server</a> so that others can use\nit too?</p>\n<h2 id=\"review\">Review</h2>\n<p>Congratulations, you have completed Typst's Tutorial! In this section, you have\nlearned how to define your own functions and how to create and apply templates\nthat define reusable document styles. You've made it far and learned a lot. You\ncan now use Typst to write your own documents and share them with others.</p>\n<p>We are still a super young project and are looking for feedback. If you have any\nquestions, suggestions or you found a bug, please let us know\nin the <a href=\"https://forum.typst.app/\">Forum</a>,\non our <a href=\"https://discord.gg/2uDybryKPe\">Discord server</a>,\non <a href=\"https://github.com/typst/typst/\">GitHub</a>,\nor via the web app's feedback form (always available in the Help menu).</p>\n<p>So what are you waiting for? <a href=\"https://typst.app\">Sign up</a> and write something!</p>"}}