Skip to content

Commit e001444

Browse files
gris-chatelegaanz
andauthored
ape:0.4.4 (#4014)
Co-authored-by: Ana Gelez <[email protected]>
1 parent 9f854e9 commit e001444

30 files changed

+2419
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Ape for Typst
2+
3+
Tired of documents that look like they were formatted by a troop of baboons? Try Ape for Typst!
4+
5+
**Ape** is a Typst package designed to help you structure and style academic documents, such as course notes, reports, and theses. It offers a set of pre-configured layouts and utilities to make your documents look professional with minimal effort.
6+
7+
> **Note:** This package is primarily designed with French conventions in mind (e.g., specific shortcuts and default text in some functions), but it can be used for documents in other languages as well.
8+
9+
## Getting Started
10+
11+
To use **Ape**, initialize your document using the `doc` function:
12+
```typst
13+
#import "@preview/ape:0.4.4": *
14+
#show: doc.with(
15+
title: "My Course Title",
16+
authors: ("Author Name",),
17+
lang: "en", // Defaults to "fr"
18+
style: "numbered",
19+
title-page: true,
20+
outline: true,
21+
)
22+
23+
= Introduction
24+
Your content goes here...
25+
```
26+
27+
*Exemple :*
28+
![Exemple 1](exemples/Exemple1.png)
29+
30+
31+
## Configuration
32+
33+
The `doc` function accepts several parameters to customize your document:
34+
35+
| Parameter | Type | Default | Description |
36+
| :--- | :--- | :--- | :--- |
37+
| `title` | `string` | `"Titre"` | The main title of the document. |
38+
| `authors` | `array` | `()` | A list of author names. |
39+
| `lang` | `string` | `"fr"` | The document language (e.g., "en", "fr"). |
40+
| `style` | `string` | `"numbered"` | Global style of the document (see below). |
41+
| `maths-style` | `string` | `"normal"` | Style for math environments (`"normal"` or `"colored"`). |
42+
| `title-page` | `boolean` | `false` | Whether to generate a dedicated title page. |
43+
| `outline` | `boolean` | `false` | Whether to generate a table of contents. |
44+
| `smallcaps` | `boolean` | `true` | Whether to use small caps in headers/titles. |
45+
46+
### Available Styles
47+
- **`numbered`**: A structured layout with numbered sections, suitable for reports and books.
48+
- **`colored`**: A colorful design often used for course notes.
49+
- **`plain`**: A simple, minimalist layout.
50+
- **`presentation`**: A layout optimized for slides/presentations.
51+
52+
## Features
53+
54+
### Formatting Tools
55+
56+
* **`para(name, content)`**: Creates a labeled paragraph.
57+
```typst
58+
#para("Note", [This is a side note.])
59+
```
60+
* **`arrow-list(..items)`**: Creates a list with arrow bullets.
61+
```typst
62+
#arrow-list[First item][Second item]
63+
```
64+
* **`inbox(content)`**: Creates a simple boxed container.
65+
* Variants: `inbox2`, `inbox3`, `inbox4` offer different border and background styles.
66+
67+
### Math Environments
68+
69+
Ape provides environments for common mathematical structures. Their appearance changes based on the `maths-style` configuration.
70+
71+
* `def(title, content)`: Definition
72+
* `prop(title, content)`: Property
73+
* `theorem(title, content)`: Theorem
74+
* `lemme(title, content)`: Lemma
75+
* `corollaire(title, content)`: Corollary
76+
* `remarque(title, content)`: Remark
77+
* `exemple(title, content)`: Example
78+
* `exercice(title, content)`: Exercise
79+
* `demo(content)`: Proof (Démonstration)
80+
81+
```typst
82+
#def("Linear Algebra")[
83+
A vector space is...
84+
]
85+
```
86+
87+
### Shortcuts & Utilities
88+
89+
* **Physics Notations**:
90+
* `dt`, `dx`, `dtheta`
91+
* `grad`
92+
* `ar(x)` -> $vec{x}$ (Arrow)
93+
* `nar(x)` -> $||vec{x}||$ (Norm of arrow)
94+
95+
* **Math Sets & Operators**:
96+
* `Im`, `Ker`, `Vect`, `dim`, `card`
97+
* `GL` (General Linear Group), `SO` (Special Orthogonal Group)
98+
* `Union`, `Inter` (Big union/intersection)
99+
100+
### Plotting & Figures
101+
102+
Ape integrates with `cetz` for drawing and plotting.
103+
104+
* **`plotting(functions, ...)`**: A high-level wrapper to plot functions easily.
105+
```typst
106+
#plotting(
107+
(
108+
(fn: x => x * x, domain: (-2, 2), stroke: blue),
109+
),
110+
axis-style: "school-book"
111+
)
112+
```
113+
* **Drawing Utilities**:
114+
* `point(coordinates)`
115+
* `spring(start, end, ...)`
116+
* `base(origin, name1, name2, angle)`
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#import "styles/numbered.typ": *
2+
#import "styles/colored.typ": *
3+
#import "styles/plain.typ": *
4+
#import "styles/presentation.typ": *
5+
#import "styles/book.typ": *
6+
7+
#let apply-style(style, title, authors, content) = {
8+
set heading(numbering: "I)1)a)i)")
9+
10+
if style == "numbered" or style == "numbered-book" {
11+
return numbered(content, style)
12+
} else if style == "book" {
13+
return book(content)
14+
} else if style == "colored" {
15+
return colored(content)
16+
} else if style == "plain" {
17+
return plain(content)
18+
} else if style == "presentation" {
19+
return presentation(content, title, authors)
20+
} else {
21+
return numbered(content)
22+
}
23+
}
24+
25+
#let get-small-title(style, title, authors) = {
26+
if (style == "numbered-book") {
27+
style = "numbered"
28+
}
29+
import "styles/" + style + ".typ" as current-style
30+
31+
return current-style.get-small-title(title, authors)
32+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#import "../tools/miscellaneous.typ": content-to-string
2+
3+
#let code-display(content) = {
4+
// Code
5+
show raw: it => context {
6+
if ("py", "python", "ocaml", "ml", "sql").contains(it.lang) {
7+
let breakableVar = false
8+
let lines = (..it.lines,)
9+
10+
11+
// Remove empty lines at the beginning and end
12+
while lines.len() > 0 and content-to-string(lines.first()).trim() == "" {
13+
let _ = lines.remove(0)
14+
}
15+
while lines.len() > 0 and content-to-string(lines.last()).trim() == "" {
16+
let _ = lines.remove(-1)
17+
}
18+
19+
if (lines.len() >= 15) {
20+
breakableVar = true
21+
}
22+
23+
24+
block(
25+
clip: true,
26+
radius: calc.min(9pt, 4pt + 2pt * lines.len()),
27+
stroke: text.fill.lighten(50%) + 0.5pt,
28+
align(
29+
center,
30+
block(
31+
breakable: breakableVar,
32+
grid(
33+
columns: (measure([#lines.len()]).width + 10pt, 20fr),
34+
column-gutter: 0pt,
35+
inset: ((left: 5pt, right: 5pt, rest: 3pt), (left: 10pt, rest: 3pt)),
36+
align: (horizon + left, left),
37+
fill: (text.fill.lighten(75%), text.fill.lighten(88%)),
38+
[],
39+
[],
40+
..for i in range(lines.len()) {
41+
let l = lines.at(i)
42+
(str(i + 1), l.body)
43+
},
44+
[],
45+
[],
46+
),
47+
),
48+
),
49+
)
50+
} else {
51+
it
52+
}
53+
}
54+
content
55+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#import "outline.typ": *
2+
#import "apply-style.typ": *
3+
#import "header-footer.typ": *
4+
#import "front-pages.typ": *
5+
#import "../tools/shortcuts.typ": *
6+
#import "code-display.typ": *
7+
#import "quote-display.typ": *
8+
#import "../tools/formatting.typ": *
9+
#import "../tools/date.typ": *
10+
#import "../maths/maths-style.typ": maths-style-state, maths-num-state
11+
12+
= Documents
13+
14+
15+
/*
16+
Labels :
17+
- <title>
18+
- <section>
19+
- <subsection>
20+
*/
21+
22+
/*
23+
Style :
24+
- Numbered
25+
- Colored
26+
- Plain
27+
*/
28+
29+
/*
30+
Maths style :
31+
- normal
32+
- colored
33+
- boxed
34+
*/
35+
36+
/*
37+
Maths numbering :
38+
- normal
39+
- separate
40+
- none
41+
*/
42+
43+
44+
#let doc(
45+
lang: "fr",
46+
title: "Titre",
47+
authors: (),
48+
style: "numbered",
49+
maths-style: "normal",
50+
maths-num: "normal",
51+
title-page: false,
52+
outline: false,
53+
local-outline: false,
54+
outline-max-depth: 5,
55+
smallcaps: true,
56+
content,
57+
) = context {
58+
set page(margin: 1.5cm)
59+
set text(lang: lang, font: "New Computer Modern")
60+
maths-style-state.update(maths-style)
61+
maths-num-state.update(maths-num)
62+
63+
show: apply-style.with(style, title, authors)
64+
65+
let (first-real-pages, custom-outline) = get-outline(lang, smallcaps, outline-max-depth)
66+
67+
68+
if (first-real-pages.len() == 0) {
69+
first-real-pages.push(0)
70+
}
71+
72+
73+
show: shows-shortcuts
74+
show: code-display
75+
show: quote-display
76+
77+
// Pre-set
78+
show table: it => {
79+
block(clip: true, radius: 0.75em, stroke: it.stroke, it)
80+
}
81+
show image: it => {
82+
align(center, it)
83+
}
84+
85+
// content
86+
set par(justify: true)
87+
88+
if (style != "presentation") {
89+
show: header-footer.with(style, smallcaps, first-real-pages, title, authors)
90+
91+
front-pages(style, smallcaps, title, title-page, authors, outline, local-outline, custom-outline)
92+
93+
94+
set table(inset: 10pt, stroke: 0.4pt + text.fill.lighten(20%), align: center + horizon, fill: (x, y) => if (x == 0)
95+
or (y == 0) { text.fill.lighten(90%) })
96+
97+
set grid(column-gutter: 10pt, align: horizon)
98+
99+
100+
counter(heading).update(0)
101+
content
102+
}else{
103+
title-slide()
104+
counter(heading).update(0)
105+
content
106+
}
107+
}
108+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#import "apply-style.typ": get-small-title
2+
#import "outline.typ": get-local-outline
3+
4+
#let front-pages(style, small-caps, title, title-page, authors, outline, local-outline, custom-outline) = {
5+
let sc(c) = {
6+
if small-caps == true {
7+
return smallcaps(c)
8+
} else {
9+
return c
10+
}
11+
}
12+
13+
if title-page {
14+
align(
15+
center + horizon,
16+
[
17+
18+
#{
19+
if type(title) == str {
20+
par(leading: 2em, spacing: 3em, text(size: 5em, hyphenate: false, (strong(title))))
21+
} else {
22+
par(leading: 1.7em, text(size: 5em, hyphenate: false, (strong(title.at(1)))))
23+
par(leading: 1em, spacing: 3em, text(size: 3.25em, hyphenate: false, (title.at(0))))
24+
}
25+
}
26+
#v(1em)
27+
28+
#{
29+
if type(authors) == array {
30+
if authors.len() > 0 {
31+
[
32+
#text(size: 1.55em, sc(authors.at(0)))
33+
\
34+
]
35+
}
36+
if authors.len() > 1 {
37+
[
38+
#text(size: 1.55em, authors.slice(1).map(sc).join(" - "))
39+
]
40+
}
41+
} else {
42+
[
43+
#text(size: 1.55em, sc(authors))
44+
]
45+
}
46+
}
47+
48+
],
49+
)
50+
51+
pagebreak()
52+
if outline {
53+
custom-outline
54+
pagebreak()
55+
}
56+
57+
get-small-title(style, title, authors)
58+
if local-outline {
59+
get-local-outline()
60+
}
61+
62+
} else {
63+
if outline {
64+
custom-outline
65+
pagebreak()
66+
}
67+
68+
get-small-title(style, title, authors)
69+
70+
if local-outline {
71+
get-local-outline()
72+
}
73+
v(20pt)
74+
}
75+
}

0 commit comments

Comments
 (0)