Skip to content

Commit 8411db5

Browse files
authored
czbloch:0.1.0 (typst#4297)
1 parent 3735e11 commit 8411db5

File tree

6 files changed

+635
-0
lines changed

6 files changed

+635
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2025 Víctor Ibáñez
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# czbloch
2+
3+
Draw Bloch spheres in Typst with configurable state vectors, axes, angle annotations, and sphere styles.
4+
5+
![Examples](example.jpg)
6+
7+
## Quick Start
8+
9+
```typst
10+
#import "@preview/czbloch:0.1.0"
11+
12+
#set page(width: auto, height: auto, margin: 8pt)
13+
14+
= Basic States
15+
16+
#stack(
17+
dir: ltr,
18+
spacing: 16pt,
19+
czbloch.bloch(..czbloch.zero),
20+
czbloch.bloch(..czbloch.one),
21+
czbloch.bloch(..czbloch.plus),
22+
czbloch.bloch(..czbloch.minus),
23+
)
24+
```
25+
26+
## API
27+
28+
### `bloch(...)`
29+
30+
Main drawing function.
31+
32+
```typst
33+
#czbloch.bloch(
34+
length: 2cm,
35+
radius: 1,
36+
debug: false,
37+
padding: none,
38+
show-axis: true,
39+
phi: 0deg,
40+
theta: 0deg,
41+
state-color: rgb("#ae3fee"),
42+
sphere-style: "circle",
43+
angle-labels: true,
44+
polar-labels: true,
45+
)
46+
```
47+
48+
Parameters:
49+
50+
- `length`: Final rendered canvas size.
51+
- `radius`: Logical radius of the Bloch sphere inside the canvas.
52+
- `debug`: Enables CeTZ debug rendering.
53+
- `padding`: CeTZ canvas padding.
54+
- `show-axis`: Draw Cartesian axes and labels.
55+
- `phi`: Azimuthal angle of the state vector.
56+
- `theta`: Polar angle of the state vector.
57+
- `state-color`: Stroke/fill color for the state arrow.
58+
- `sphere-style`: `"circle"`, `"sphere"`, or `none`.
59+
- `angle-labels`: Show `phi` and `theta` labels.
60+
- `polar-labels`: Show `|0>` and `|1>` pole labels.
61+
62+
### Presets
63+
64+
Useful preset states exported by the package:
65+
66+
- `czbloch.zero`: `(phi: 0deg, theta: 0deg)`
67+
- `czbloch.one`: `(phi: 0deg, theta: 180deg)`
68+
- `czbloch.plus`: `(phi: 0deg, theta: 90deg)`
69+
- `czbloch.minus`: `(phi: 180deg, theta: 90deg)`
70+
71+
Use with argument spread:
72+
73+
```typst
74+
#czbloch.bloch(..czbloch.plus, state-color: green)
75+
```
76+
77+
## More Examples
78+
79+
```typst
80+
#import "@preview/czbloch:0.1.0" as czbloch
81+
82+
#stack(
83+
dir: ttb,
84+
spacing: 12pt,
85+
86+
// Sphere-style shading
87+
czbloch.bloch(..czbloch.one, sphere-style: "sphere"),
88+
89+
// Custom state
90+
czbloch.bloch(
91+
phi: 50deg,
92+
theta: 30deg,
93+
state-color: orange,
94+
angle-labels: false,
95+
),
96+
97+
// Wrapped angles are supported
98+
czbloch.bloch(
99+
phi: -10deg,
100+
theta: 240deg,
101+
sphere-style: "sphere",
102+
),
103+
)
104+
```
95.7 KB
Loading
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#import "@preview/czbloch:0.1.0" as czbloch
2+
3+
#set page(margin: 0.1cm, height: auto, width: auto)
4+
5+
#let opts-1 = (angle-labels: false)
6+
#let opts-2 = (angle-labels: false, sphere-style: "sphere")
7+
8+
#let examples = (
9+
(czbloch.bloch(state-color: red), `czbloch.bloch(state-color: red)`),
10+
(
11+
czbloch.bloch(..czbloch.one, sphere-style: "sphere"),
12+
`czbloch.bloch(.., sphere-style: "sphere")`,
13+
),
14+
(
15+
czbloch.bloch(..czbloch.plus, ..opts-2, state-color: lime),
16+
`czbloch.bloch(.., angle-labels: false)`,
17+
),
18+
(
19+
czbloch.bloch(..czbloch.minus, ..opts-1, state-color: fuchsia),
20+
none,
21+
),
22+
(
23+
czbloch.bloch(phi: 50deg, theta: 30deg, ..opts-1, state-color: green),
24+
none,
25+
),
26+
(
27+
czbloch.bloch(phi: -10deg, theta: 120deg, ..opts-2, state-color: orange),
28+
`czbloch.bloch(phi: -100deg, theta: 120deg, ..)`,
29+
),
30+
)
31+
32+
#set align(center)
33+
34+
#for pair in examples.chunks(2) {
35+
let drawings = pair.map(d => d.at(0))
36+
let code = pair.map(d => d.at(1))
37+
table(
38+
stroke: none,
39+
columns: 2,
40+
column-gutter: 0.5cm,
41+
..drawings,
42+
..code
43+
)
44+
}

0 commit comments

Comments
 (0)