Skip to content

Commit

Permalink
Types, labels
Browse files Browse the repository at this point in the history
  • Loading branch information
sitandr committed Nov 6, 2023
1 parent afa835c commit 1c59996
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@
- [Semicolon syntax]()
- [Typst Snippets]()
- [Logos&Figures](./snippets/logos.md)
- [Labels](./snippets/labels.md)
- [Bibliography setup]()
- [Setting chapter headers]()
- [Page setup]()
- [Code formatting]()
- [Hyphenation]()
- [Math]()
- [Numbering]()
- [Operations](./snippets/math/operations.md)
- [Setting limits]()
- [Symbols]() <!--TODO: emptyset, replacing-->
- [Text&Content]()
Expand All @@ -55,7 +57,7 @@
- [Math]()
- [Presentations]()
- [Tables]()
- [Algorithms]()
- [Code]()
- [Themes]()
- [Misc]()
- [Typstonomicon, or The Code You Should Not Write]()
Expand Down
34 changes: 34 additions & 0 deletions src/basics/scripting/braces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
26 changes: 22 additions & 4 deletions src/basics/scripting/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand Down Expand Up @@ -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`)
Expand All @@ -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")
Expand Down
65 changes: 64 additions & 1 deletion src/basics/scripting/types_2.md
Original file line number Diff line number Diff line change
@@ -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`)
> [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)
```
8 changes: 4 additions & 4 deletions src/basics/tutorial/basic_styling.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/snippets/labels.md
Original file line number Diff line number Diff line change
@@ -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 <design>
#lorem(20)
= Implementation
In #ref-heading(<design>), we discussed...
```
16 changes: 16 additions & 0 deletions src/snippets/math/operations.md
Original file line number Diff line number Diff line change
@@ -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$)$
```

0 comments on commit 1c59996

Please sign in to comment.