From 1c59996d5717c2d87882e170ee91ff12c3f9d218 Mon Sep 17 00:00:00 2001 From: sitandr Date: Tue, 7 Nov 2023 00:41:18 +0300 Subject: [PATCH] Types, labels --- src/SUMMARY.md | 4 +- src/basics/scripting/braces.md | 34 +++++++++++++++ src/basics/scripting/types.md | 26 +++++++++-- src/basics/scripting/types_2.md | 65 +++++++++++++++++++++++++++- src/basics/tutorial/basic_styling.md | 8 ++-- src/snippets/labels.md | 23 ++++++++++ src/snippets/math/operations.md | 16 +++++++ 7 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 src/snippets/labels.md create mode 100644 src/snippets/math/operations.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9170a1a..a75f211 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -35,6 +35,7 @@ - [Semicolon syntax]() - [Typst Snippets]() - [Logos&Figures](./snippets/logos.md) + - [Labels](./snippets/labels.md) - [Bibliography setup]() - [Setting chapter headers]() - [Page setup]() @@ -42,6 +43,7 @@ - [Hyphenation]() - [Math]() - [Numbering]() + - [Operations](./snippets/math/operations.md) - [Setting limits]() - [Symbols]() - [Text&Content]() @@ -55,7 +57,7 @@ - [Math]() - [Presentations]() - [Tables]() - - [Algorithms]() + - [Code]() - [Themes]() - [Misc]() - [Typstonomicon, or The Code You Should Not Write]() diff --git a/src/basics/scripting/braces.md b/src/basics/scripting/braces.md index 64591c4..0716a8c 100644 --- a/src/basics/scripting/braces.md +++ b/src/basics/scripting/braces.md @@ -44,6 +44,40 @@ That's when we use `{}`: #f("world") ``` +## Return + +**Important**: by default braces return anything that "returns" into them. For example, + +``` +#let change_world() = { + // some code there changing everything in the world + str(4e7) + // another code changing the world +} + +#let g() = { + "Hahaha, I will change the world now! " + change_world() + " So here is my long evil monologue..." +} + +#g() +``` + +To avoid returning everything, return only what you want explicitly, otherwise everything will be joined: + +``` +#let f() = { + "Some long text" + // Crazy numbers + "2e7" + return none +} + +// Returns nothing +#f() +``` + ## Default values What we made just now was inventing "default values". diff --git a/src/basics/scripting/types.md b/src/basics/scripting/types.md index e018d78..957128e 100644 --- a/src/basics/scripting/types.md +++ b/src/basics/scripting/types.md @@ -22,6 +22,15 @@ We have already seen it. A type that represents what is displayed in document. **Important:** It is very hard to convert _content_ to _plain text_, as _content_ may contain *anything*! Sp be careful when passing and storing content in variables. +## None (`none`) + +Nothing. Also known as `null` in other languages. It isn't displayed, converts to empty content. + +``` +#none +#repr(none) +``` + ## String (`str`) > [Link to Reference](https://typst.app/docs/reference/foundations/str/). @@ -71,23 +80,28 @@ You can convert a value to an integer with this type's constructor. #n \ #(n += 1) \ #n \ -#calc.pow(2, n) +#calc.pow(2, n)\ +#type(n)\ +#repr(n) +``` +``` #(1 + 2) \ #(2 - 5) \ #(3 + 4 < 8) +``` +``` #0xff \ #0o10 \ #0b1001 +``` +``` #int(false) \ #int(true) \ #int(2.7) \ #(int("27") + int("4")) - -#type(n)\ -#repr(n) ``` ## Float (`float`) @@ -105,11 +119,15 @@ However, precision may be lost. #calc.pow(2, n) \ #(0.2 + 0.1) \ #type(n) +``` +``` #3.14 \ #1e4 \ #(10 / 4) +``` +``` #float(40%) \ #float("2.7") \ #float("1e5") diff --git a/src/basics/scripting/types_2.md b/src/basics/scripting/types_2.md index 02e160a..5cc0021 100644 --- a/src/basics/scripting/types_2.md +++ b/src/basics/scripting/types_2.md @@ -1,5 +1,68 @@ # Types, part II +In Typst, most of things are **immutable**. You can't change content, you can just create new using this one (for example, using addition). + +Immutability is very important for Typst since it tries to be _as pure language as possible_. Functions do nothing outside of returning some value. + +However, purity is partly "broken" by these types. They are *super-useful* and not adding them would make Typst much pain. + +However, using them adds complexity. + ## Arrays (`array`) -## Dictionaries (`dict`) \ No newline at end of file +> [Link to Reference](https://typst.app/docs/reference/foundations/array/). + +Mutable object that stores data with their indices. + +### Working with indices +``` +#let values = (1, 7, 4, -3, 2) + +// take value at index 0 +#values.at(0) \ +// set value at 0 to 3 +#(values.at(0) = 3) +// negative index => start from the back +#values.at(-1) \ +// add index of something that is even +#values.find(calc.even) +``` + +### Iterating methods +``` +#let values = (1, 7, 4, -3, 2) + +// leave only what is odd +#values.filter(calc.odd) \ +// create new list of absolute values of list values +#values.map(calc.abs) \ +// reverse +#values.rev() \ +// convert array of arrays to flat array +#(1, (2, 3)).flatten() \ +// join array of string to string +#(("A", "B", "C") + .join(", ", last: " and ")) +``` + +## Dictionaries (`dict`) + +> [Link to Reference](https://typst.app/docs/reference/foundations/dictionary/). + +Dictionaries are objects that store a string "key" and a value, associated with that key. + +``` +#let dict = ( + name: "Typst", + born: 2019, +) + +#dict.name \ +#(dict.launch = 20) +#dict.len() \ +#dict.keys() \ +#dict.values() \ +#dict.at("born") \ +#dict.insert("city", "Berlin ") +#("name" in dict) +``` \ No newline at end of file diff --git a/src/basics/tutorial/basic_styling.md b/src/basics/tutorial/basic_styling.md index ecc0a10..c412a91 100644 --- a/src/basics/tutorial/basic_styling.md +++ b/src/basics/tutorial/basic_styling.md @@ -77,10 +77,10 @@ For example, let's make all quotes there authored by that book: That allows you to set the defaults for the document in general as you want: ``` -set par(justify: true) -set list(indent: 1em) -set enum(indent: 1em) -set page(numbering: "1") +#set par(justify: true) +#set list(indent: 1em) +#set enum(indent: 1em) +#set page(numbering: "1") - List item - List item diff --git a/src/snippets/labels.md b/src/snippets/labels.md new file mode 100644 index 0000000..1d369cf --- /dev/null +++ b/src/snippets/labels.md @@ -0,0 +1,23 @@ +# Labels + +## Get chapter of label + +``` +#let ref-heading(label) = locate(loc => { + let elems = query(label, loc) + if elems.len() != 1 { + panic("found multiple elements") + } + let element = elems.first() + if element.func() != heading { + panic("label must target heading") + } + link(label, element.body) +}) + += Design +#lorem(20) + += Implementation +In #ref-heading(), we discussed... +``` \ No newline at end of file diff --git a/src/snippets/math/operations.md b/src/snippets/math/operations.md new file mode 100644 index 0000000..e3bcf24 --- /dev/null +++ b/src/snippets/math/operations.md @@ -0,0 +1,16 @@ +# Operations + +## Fractions + +``` +$ +p/q, p slash q, p\/q +$ +``` + +### Slightly moved: +``` +#let mfrac(a, b) = move(a, dy: -0.2em) + "/" + move(b, dy: 0.2em, dx: -0.1em) + +$A\/B, #mfrac($A$, $B$)$ +``` \ No newline at end of file