{"route":"/en-US-v0.13.1/tutorial/writing-in-typst/","title":"Writing in Typst","description":"Typst's tutorial.","part":null,"outline":[{"id":"figure","name":"Figure","children":[]},{"id":"bibliography","name":"Bibliography","children":[]},{"id":"maths","name":"Maths","children":[]},{"id":"review","name":"Review","children":[]}],"body":{"kind":"html","content":"<h1>Writing in Typst</h1>\n<p>Let's get started! Suppose you got assigned to write a technical report for\nuniversity. It will contain prose, maths, headings, and figures. To get started,\nyou create a new project on the Typst app. You'll be taken to the editor where\nyou see two panels: A source panel where you compose your document and a\npreview panel where you see the rendered document.</p>\n<p><img src=\"/en-US-v0.13.1/assets/1-writing-app.png\" alt=\"Typst app screenshot\" /></p>\n<p>You already have a good angle for your report in mind. So let's start by writing\nthe introduction. Enter some text in the editor panel. You'll notice that the\ntext immediately appears on the previewed page.</p>\n<div class=\"previewed-code\"><pre><code>In 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</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/78f97553ed9aef0f2a9266f708b97fa0.png\" alt=\"Preview\"></div></div>\n<p><em>Throughout this tutorial, we'll show code examples like this one. Just like in the app, the first panel contains markup and the second panel shows a preview. We shrunk the page to fit the examples so you can see what's going on.</em></p>\n<p>The next step is to add a heading and emphasize some text. Typst uses simple\nmarkup for the most common formatting tasks. To add a heading, enter the <code>=</code>\ncharacter and to emphasize some text with italics, enclose it in\n<code><span class=\"typ-emph\">_underscores_</span></code>.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-heading\">= Introduction</span>\nIn this report, we will explore the\nvarious factors that influence <span class=\"typ-emph\">_fluid\ndynamics_</span> in glaciers and how they\ncontribute to the formation and\nbehaviour of these natural structures.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/a7be6ffb3ed0a950a1a6507637c1d97f.png\" alt=\"Preview\"></div></div>\n<p>That was easy! To add a new paragraph, just add a blank line in between two\nlines of text. If that paragraph needs a subheading, produce it by typing <code>==</code>\ninstead of <code>=</code>. The number of <code>=</code> characters determines the nesting level of the\nheading.</p>\n<p>Now we want to list a few of the circumstances that influence glacier dynamics.\nTo do that, we use a numbered list. For each item of the list, we type a <code>+</code>\ncharacter at the beginning of the line. Typst will automatically number the\nitems.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-marker\">+</span> The climate\n<span class=\"typ-marker\">+</span> The topography\n<span class=\"typ-marker\">+</span> The geology\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/53720741b852350f277645c8bff80fae.png\" alt=\"Preview\"></div></div>\n<p>If we wanted to add a bulleted list, we would use the <code>-</code> character instead of\nthe <code>+</code> character. We can also nest lists: For example, we can add a sub-list to\nthe first item of the list above by indenting it.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-marker\">+</span> The climate\n  <span class=\"typ-marker\">-</span> Temperature\n  <span class=\"typ-marker\">-</span> Precipitation\n<span class=\"typ-marker\">+</span> The topography\n<span class=\"typ-marker\">+</span> The geology\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/c664be04f88cfe00c791693dfee844fe.png\" alt=\"Preview\"></div></div>\n<h2 id=\"figure\">Adding a figure</h2>\n<p>You think that your report would benefit from a figure. Let's add one. Typst\nsupports images in the formats PNG, JPEG, GIF, and SVG. To add an image file to\nyour project, first open the <em>file panel</em> by clicking the box icon in the left\nsidebar. Here, you can see a list of all files in your project. Currently, there\nis only one: The main Typst file you are writing in. To upload another file,\nclick the button with the arrow in the top-right corner. This opens the upload\ndialog, in which you can pick files to upload from your computer. Select an\nimage file for your report.</p>\n<p><img src=\"/en-US-v0.13.1/assets/1-writing-upload.png\" alt=\"Upload dialog\" /></p>\n<p>We have seen before that specific symbols (called <em>markup</em>) have specific\nmeaning in Typst. We can use <code>=</code>, <code>-</code>, <code>+</code>, and <code>_</code> to create headings, lists\nand emphasized text, respectively. However, having a special symbol for\neverything we want to insert into our document would soon become cryptic and\nunwieldy. For this reason, Typst reserves markup symbols only for the most\ncommon things. Everything else is inserted with <em>functions.</em> For our image to\nshow up on the page, we use Typst's <a href=\"/en-US-v0.13.1/reference/visualize/image/\" title=\"`image`\"><code>image</code></a> function.</p>\n<div class=\"previewed-code\"><pre><code><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>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/2b02a560255bdae159a99f1a6eddfe82.png\" alt=\"Preview\"></div></div>\n<p>In general, a function produces some output for a set of <em>arguments</em>. When you\n<em>call</em> a function within markup, you provide the arguments and Typst inserts the\nresult (the function's <em>return value</em>) into the document. In our case, the\n<code>image</code> function takes one argument: The path to the image file. To call a\nfunction in markup, we first need to type the <code>#</code> character, immediately\nfollowed by the name of the function. Then, we enclose the arguments in\nparentheses. Typst recognizes many different data types within argument lists.\nOur file path is a short <a href=\"/en-US-v0.13.1/reference/foundations/str/\">string of text</a>, so we need to enclose it in\ndouble quotes.</p>\n<p>The inserted image uses the whole width of the page. To change that, pass the\n<code>width</code> argument to the <code>image</code> function. This is a <em>named</em> argument and\ntherefore specified as a <code>name: value</code> pair. If there are multiple arguments,\nthey are separated by commas, so we first need to put a comma behind the path.</p>\n<div class=\"previewed-code\"><pre><code><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</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/96969d2883b3704b1ffcc1a849e66084.png\" alt=\"Preview\"></div></div>\n<p>The <code>width</code> argument is a <a href=\"/en-US-v0.13.1/reference/layout/relative/\">relative length</a>. In our case, we\nspecified a percentage, determining that the image shall take up <code><span class=\"typ-num\">70%</span></code> of the\npage's width. We also could have specified an absolute value like <code><span class=\"typ-num\">1cm</span></code> or\n<code><span class=\"typ-num\">0.7in</span></code>.</p>\n<p>Just like text, the image is now aligned at the left side of the page by\ndefault. It's also lacking a caption. Let's fix that by using the <a href=\"/en-US-v0.13.1/reference/model/figure/\" title=\"figure\">figure</a>\nfunction. This function takes the figure's contents as a positional argument and\nan optional caption as a named argument.</p>\n<p>Within the argument list of the <code>figure</code> function, Typst is already in code\nmode. This means, you now have to remove the hash before the image function call.\nThe hash is only needed directly in markup (to disambiguate text from function\ncalls).</p>\n<p>The caption consists of arbitrary markup. To give markup to a function, we\nenclose it in square brackets. This construct is called a <em>content block.</em></p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">figure</span><span class=\"typ-punct\">(</span>\n  <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><span class=\"typ-punct\">,</span>\n  caption<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>\n    <span class=\"typ-emph\">_Glaciers_</span> form an important part\n    of the earth&#39;s climate system.\n  <span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/bf93a745e50ef1f0f945f8f6689655c9.png\" alt=\"Preview\"></div></div>\n<p>You continue to write your report and now want to reference the figure. To do\nthat, first attach a label to figure. A label uniquely identifies an element in\nyour document. Add one after the figure by enclosing some name in angle\nbrackets. You can then reference the figure in your text by writing an <code><span class=\"typ-ref\">@</span></code>\nsymbol followed by that name. Headings and equations can also be labelled to\nmake them referenceable.</p>\n<div class=\"previewed-code\"><pre><code>Glaciers as the one shown in\n<span class=\"typ-ref\">@glaciers</span> will cease to exist if\nwe don&#39;t take action soon!\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">figure</span><span class=\"typ-punct\">(</span>\n  <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><span class=\"typ-punct\">,</span>\n  caption<span class=\"typ-punct\">:</span> <span class=\"typ-punct\">[</span>\n    <span class=\"typ-emph\">_Glaciers_</span> form an important part\n    of the earth&#39;s climate system.\n  <span class=\"typ-punct\">]</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span> <span class=\"typ-label\">&lt;glaciers&gt;</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/730675da2437f41e0bfbfc10c213b64c.png\" alt=\"Preview\"></div></div><div class=\"info-box\">\n<p>So far, we've passed content blocks (markup in square brackets) and strings\n(text in double quotes) to our functions. Both seem to contain text. What's the\ndifference?</p>\n<p>A content block can contain text, but also any other kind of markup, function\ncalls, and more, whereas a string is really just a <em>sequence of characters</em> and\nnothing else.</p>\n<p>For example, the image function expects a path to an image file.\nIt would not make sense to pass, e.g., a paragraph of text or another image as\nthe image's path parameter. That's why only strings are allowed here.\nOn the contrary, strings work wherever content is expected because text is a\nvalid kind of content.</p>\n</div>\n<h2 id=\"bibliography\">Adding a bibliography</h2>\n<p>As you write up your report, you need to back up some of your claims. You can\nadd a bibliography to your document with the <a href=\"/en-US-v0.13.1/reference/model/bibliography/\" title=\"`bibliography`\"><code>bibliography</code></a> function. This\nfunction expects a path to a bibliography file.</p>\n<p>Typst's native bibliography format is\n<a href=\"https://github.com/typst/hayagriva/blob/main/docs/file-format.md\">Hayagriva</a>,\nbut for compatibility you can also use BibLaTeX files. As your classmate has\nalready done a literature survey and sent you a <code>.bib</code> file, you'll use that\none. Upload the file through the file panel to access it in Typst.</p>\n<p>Once the document contains a bibliography, you can start citing from it.\nCitations use the same syntax as references to a label. As soon as you cite a\nsource for the first time, it will appear in the bibliography section of your\ndocument. Typst supports different citation and bibliography styles. Consult the\n<a href=\"/en-US-v0.13.1/reference/model/bibliography/#parameters-style\">reference</a> for more details.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-heading\">= Methods</span>\nWe follow the glacier melting models\nestablished in <span class=\"typ-ref\">@glacier-melt</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/4063c74f5e24b1d79ad2bffcbf2d3558.png\" alt=\"Preview\"></div></div>\n<h2 id=\"maths\">Maths</h2>\n<p>After fleshing out the methods section, you move on to the meat of the document:\nYour equations. Typst has built-in mathematical typesetting and uses its own\nmath notation. Let's start with a simple equation. We wrap it in <code>$</code> signs\nto let Typst know it should expect a mathematical expression:</p>\n<div class=\"previewed-code\"><pre><code>The equation <span class=\"typ-math-delim\">$</span>Q = <span class=\"typ-pol\">rho</span> A v + C<span class=\"typ-math-delim\">$</span>\ndefines the glacial flow rate.\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/feee418cba0c141654db383539650b76.png\" alt=\"Preview\"></div></div>\n<p>The equation is typeset inline, on the same line as the surrounding text. If you\nwant to have it on its own line instead, you should insert a single space at its\nstart and end:</p>\n<div class=\"previewed-code\"><pre><code>The flow rate of a glacier is\ndefined by the following equation:\n\n<span class=\"typ-math-delim\">$</span> Q = <span class=\"typ-pol\">rho</span> A v + C <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/1972349af18eaaa482d7ae624532be43.png\" alt=\"Preview\"></div></div>\n<p>We can see that Typst displayed the single letters <code>Q</code>, <code>A</code>, <code>v</code>, and <code>C</code> as-is,\nwhile it translated <code>rho</code> into a Greek letter. Math mode will always show single\nletters verbatim. Multiple letters, however, are interpreted as symbols,\nvariables, or function names. To imply a multiplication between single letters,\nput spaces between them.</p>\n<p>If you want to have a variable that consists of multiple letters, you can\nenclose it in quotes:</p>\n<div class=\"previewed-code\"><pre><code>The flow rate of a glacier is given\nby the following equation:\n\n<span class=\"typ-math-delim\">$</span> Q = <span class=\"typ-pol\">rho</span> A v + <span class=\"typ-str\">&quot;time offset&quot;</span> <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/2526a88c6062287f852db218a9e59280.png\" alt=\"Preview\"></div></div>\n<p>You'll also need a sum formula in your paper. We can use the <code>sum</code> symbol and\nthen specify the range of the summation in sub- and superscripts:</p>\n<div class=\"previewed-code\"><pre><code>Total displaced soil by glacial flow:\n\n<span class=\"typ-math-delim\">$</span> 7.32 <span class=\"typ-pol\">beta</span> +\n  <span class=\"typ-pol\">sum</span><span class=\"typ-math-op\">_</span><span class=\"typ-punct\">(</span>i=0<span class=\"typ-punct\">)</span><span class=\"typ-math-op\">^</span><span class=\"typ-pol\">nabla</span> Q<span class=\"typ-math-op\">_</span>i <span class=\"typ-math-op\">/</span> 2 <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/ad30f24c6c4995728f44726e6f700b46.png\" alt=\"Preview\"></div></div>\n<p>To add a subscript to a symbol or variable, type a <code>_</code> character and then the\nsubscript. Similarly, use the <code>^</code> character for a superscript. If your\nsub- or superscript consists of multiple things, you must enclose them\nin round parentheses.</p>\n<p>The above example also showed us how to insert fractions: Simply put a <code>/</code>\ncharacter between the numerator and the denominator and Typst will automatically\nturn it into a fraction. Parentheses are smartly resolved, so you can enter your\nexpression as you would into a calculator and Typst will replace parenthesized\nsub-expressions with the appropriate notation.</p>\n<div class=\"previewed-code\"><pre><code>Total displaced soil by glacial flow:\n\n<span class=\"typ-math-delim\">$</span> 7.32 <span class=\"typ-pol\">beta</span> +\n  <span class=\"typ-pol\">sum</span><span class=\"typ-math-op\">_</span><span class=\"typ-punct\">(</span>i=0<span class=\"typ-punct\">)</span><span class=\"typ-math-op\">^</span><span class=\"typ-pol\">nabla</span>\n    <span class=\"typ-punct\">(</span>Q<span class=\"typ-math-op\">_</span>i (a<span class=\"typ-math-op\">_</span>i <span class=\"typ-escape\">-</span> <span class=\"typ-pol\">epsilon</span>)<span class=\"typ-punct\">)</span> <span class=\"typ-math-op\">/</span> 2 <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/1e0781d81c792e1ddae4d3df17a58477.png\" alt=\"Preview\"></div></div>\n<p>Not all math constructs have special syntax. Instead, we use functions, just\nlike the <code>image</code> function we have seen before. For example, to insert a column\nvector, we can use the <a href=\"/en-US-v0.13.1/reference/math/vec/\"><code>vec</code></a> function. Within math mode, function\ncalls don't need to start with the <code>#</code> character.</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-math-delim\">$</span> v <span class=\"typ-escape\">:=</span> <span class=\"typ-func\">vec</span><span class=\"typ-punct\">(</span>x<span class=\"typ-math-op\">_</span>1<span class=\"typ-punct\">,</span> x<span class=\"typ-math-op\">_</span>2<span class=\"typ-punct\">,</span> x<span class=\"typ-math-op\">_</span>3<span class=\"typ-punct\">)</span> <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/9e3d2932792ea17dade45677ff1e982b.png\" alt=\"Preview\"></div></div>\n<p>Some functions are only available within math mode. For example, the\n<a href=\"/en-US-v0.13.1/reference/math/variants/#functions-cal\"><code>cal</code></a> function is used to typeset calligraphic letters commonly\nused for sets. The <a href=\"/en-US-v0.13.1/reference/math/\">math section of the reference</a> provides a\ncomplete list of all functions that math mode makes available.</p>\n<p>One more thing: Many symbols, such as the arrow, have a lot of variants. You can\nselect among these variants by appending a dot and a modifier name to a symbol's\nname:</p>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-math-delim\">$</span> a <span class=\"typ-pol\">arrow</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">squiggly</span> b <span class=\"typ-math-delim\">$</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.13.1/assets/d068108ad373e358feef9177152ea203.png\" alt=\"Preview\"></div></div>\n<p>This notation is also available in markup mode, but the symbol name must be\npreceded with <code>#sym.</code> there. See the <a href=\"/en-US-v0.13.1/reference/symbols/sym/\">symbols section</a>\nfor a list of all available symbols.</p>\n<h2 id=\"review\">Review</h2>\n<p>You have now seen how to write a basic document in Typst. You learned how to\nemphasize text, write lists, insert images, align content, and typeset\nmathematical expressions. You also learned about Typst's functions. There are\nmany more kinds of content that Typst lets you insert into your document, such\nas <a href=\"/en-US-v0.13.1/reference/model/table/\">tables</a>, <a href=\"/en-US-v0.13.1/reference/visualize/\">shapes</a>, and <a href=\"/en-US-v0.13.1/reference/text/raw/\">code blocks</a>. You\ncan peruse the <a href=\"/en-US-v0.13.1/reference/\" title=\"reference\">reference</a> to learn more about these and other features.</p>\n<p>For the moment, you have completed writing your report. You have already saved a\nPDF by clicking on the download button in the top right corner. However, you\nthink the report could look a bit less plain. In the next section, we'll learn\nhow to customize the look of our document.</p>"}}