Skip to content

Commit

Permalink
Update to v0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Leedehai committed Dec 4, 2023
1 parent ae714fb commit d541bc6
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 80 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
physica.pdf
demo.pdf
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
# The `physica` package for Typst
# The physica package for Typst

[![CI](https://github.com/Leedehai/typst-physics/actions/workflows/ci.yml/badge.svg)](https://github.com/Leedehai/typst-physics/actions/workflows/ci.yml)
[![Latest release](https://img.shields.io/github/v/release/Leedehai/typst-physics.svg?color=gold)][latest-release]

:green_book: The [manual](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf).

Available in the collection of [Typst packages](https://typst.app/docs/packages/): `#import "@preview/physica:0.8.1": *`
Available in the collection of [Typst packages](https://typst.app/docs/packages/): `#import "@preview/physica:0.9.0": *`

> physica _noun_.
> * Latin, study of nature
This [Typst](https://typst.app) package provides handy typesetting utilities for
physics, including:
natural sciences, including:
* Braces,
* Vectors and vector fields,
* Matrices,
Expand Down Expand Up @@ -37,29 +40,32 @@ See the [manual](https://github.com/Leedehai/typst-physics/blob/master/physica-m

### With `typst` package management (recommended)

See https://github.com/typst/packages.
See https://github.com/typst/packages. If you are using the Typst's web app,
packages listed there are readily available; if you are using the Typst
compiler locally, it downloads packages on-demand and caches them on-disk, see
[here](https://github.com/typst/packages#downloads) for details.

<p align="center">
<img src="https://github.com/Leedehai/typst-physics/assets/18319900/f2a3a2bd-3ef7-4383-ab92-9a71affb4e12" width="173" alt="effect">
</p>

```typst
// Style 1
#import "@preview/physica:0.8.1": *
#import "@preview/physica:0.9.0": *
$ curl (grad f), tensor(T, -mu, +nu), pdv(f,x,y,[1,2]) $
```

```typst
// Style 2
#import "@preview/physica:0.8.1": curl, grad, tensor, pdv
#import "@preview/physica:0.9.0": curl, grad, tensor, pdv
$ curl (grad f), tensor(T, -mu, +nu), pdv(f,x,y,[1,2]) $
```

```typst
// Style 3
#import "@preview/physica:0.8.1"
#import "@preview/physica:0.9.0"
$ physica.curl (physica.grad f), physica.tensor(T, -mu, +nu), physica.pdv(f,x,y,[1,2]) $
```
Expand All @@ -68,18 +74,25 @@ $ physica.curl (physica.grad f), physica.tensor(T, -mu, +nu), physica.pdv(f,x,y,

Similar to examples above, but import with the undecorated file path like `"physica.typ"`.

## Manual
## Typst version

See the manual [physica-manual.pdf](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf) for a more comprehensive coverage, a PDF file
generated directly with the [Typst](https://typst.app) binary.
The version requirement for the compiler is in [typst.toml](typst.toml)'s
`compiler` field. If you are using an unsupported Typst version, the compiler
will throw an error. You may want to update your compiler with `typst update`,
or choose an earlier version of the `physica` package.

CLI Version:
Developed with compiler version:

```sh
$ typst --version
typst 0.9.0 (7bb4f6df)
typst 0.10.0 (70ca0d25)
```

## Manual

See the manual [physica-manual.pdf](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf) for a more comprehensive coverage, a PDF file
generated directly with the [Typst](https://typst.app) binary.

To regenerate the manual, use command

```sh
Expand All @@ -98,8 +111,6 @@ explore the design together. Some features might warrant a package on its own.
[physica-manual.pdf](https://github.com/Leedehai/typst-physics/blob/master/physica-manual.pdf). This does not scale well. I plan to add programmatic
testing by comparing rendered pictures with golden images.

* Please nudge Typst maintainers to resolve https://github.com/typst/typst/issues/240 by scaling up given glyphs in the middle of [`lr()`](https://typst.app/docs/reference/math/lr#functions-lr) :grinning:

## License

* Code: the [MIT License](LICENSE.txt).
Expand Down
Binary file modified physica-manual.pdf
Binary file not shown.
25 changes: 12 additions & 13 deletions physica-manual.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// This document is shared under the Creative Commons BY-ND 4.0 license.
#import "physica.typ": *

#let version = "0.8.1"
#let version = "0.9.0"

#set page(
numbering: "1/1",
header: align(right)[The `physica` package, version #version],
header: align(right)[#text(8pt)[The `physica` package\ version #version]],
)

#set heading(numbering: "1.")
Expand All @@ -19,20 +19,19 @@

#align(center)[
Leedehai \
#linkurl("https://github.com/leedehai/typst-physics", "https://github.com/leedehai/typst-physics")
]

#align(center)[
Version #version, October 25, 2023 \
Doc updated: November 26, 2023
#linkurl("GitHub repo", "https://github.com/leedehai/typst-physics") |
#linkurl("Typst index", "https://typst.app/docs/packages/")
]

#set par(justify: true)

*NOTE (2023-04-02):* #linkurl("Typst", "https://typst.app") is version 0.x and evolving, and this package evolves with it. Also, the package itself is under development and fine-tuning. While the major version stays 0, no backward compatibility is guaranteed.
#v(1em)

#align(center)[
/ physica: _noun_. Latin, study of nature.
]

Indexed at
#linkurl("https://typst.app/docs/packages/", "https://typst.app/docs/packages/").
#v(1em)

#outline(indent: true)
#pagebreak(weak: true)
Expand All @@ -43,7 +42,7 @@ Indexed at

#linkurl("Typst", "https://typst.app") is typesetting framework aiming to become the next generation alternative to LATEX. It excels in its friendly user experience and performance.

The `physica` package provides handy Typst typesetting functions that make academic writing for physics simpler and faster, by simplifying otherwise very complex and repetitive expressions in the domain of physics.
The `physica` package provides handy Typst typesetting functions that make academic writing for natural sciences simpler and faster, by simplifying otherwise very complex and repetitive expressions in the domain of natural sciences.

This manual itself was generated using the Typst CLI and the `physica` package, so hopefully this document is able to provide you with a sufficiently self evident demonstration of how this package shall be used.

Expand All @@ -54,7 +53,7 @@ This manual itself was generated using the Typst CLI and the `physica` package,
With `typst`'s #linkurl("package management", "https://github.com/typst/packages"):

```typst
#import "@preview/physica:0.8.1": *
#import "@preview/physica:0.9.0": *
$ curl (grad f), pdv(,x,y,z,[2,k]), tensor(Gamma,+k,-i,-j) = pdv(vb(e_i),x^j)vb(e^k) $
```
Expand Down
38 changes: 5 additions & 33 deletions physica.typ
Original file line number Diff line number Diff line change
Expand Up @@ -344,61 +344,33 @@
#let bra(f) = $lr(angle.l #f|)$
#let ket(f) = $lr(|#f angle.r)$

// Quadratic interpolation that takes in a height in pt units.
// Intended input-output pairs: {10.59,1},{23.01,0.8},{26.93,0.7}.
// This is imperfect, hacky and has corner cases unresolved, but until
// https://github.com/typst/typst/issues/240 is resolved there is no better way.
// Motivated by https://github.com/Leedehai/typst-physics/issues/16
#let __height_mul_factor(h) = {
-0.0005757 * h * h + 0.0032409 * h + 1.03024
}

// Credit: thanks to peng1999@ and szdytom@'s suggestions of measure() and
// phantoms. The hack works until https://github.com/typst/typst/issues/240 is
// addressed by Typst.
#let braket(..sink) = style(styles => {
let args = sink.pos() // array
assert(args.len() == 1 or args.len() == 2, message: "expecting 1 or 2 args")

let bra = args.at(0)
let ket = args.at(1, default: bra)

let height = measure($ bra ket $, styles).height;
let phantom = box(
height: height * __height_mul_factor(height.pt()),
width: 0pt, inset: 0pt, stroke: none);
$ lr(angle.l bra lr(|phantom#h(0pt)) ket angle.r) $
$ lr(angle.l bra#h(0pt)mid(bar.v)#h(0pt)ket angle.r) $
})

// Credit: until https://github.com/typst/typst/issues/240 is addressed by Typst
// we use the same hack as braket().
#let ketbra(..sink) = style(styles => {
let args = sink.pos() // array
assert(args.len() == 1 or args.len() == 2, message: "expecting 1 or 2 args")

let bra = args.at(0)
let ket = args.at(1, default: bra)
let ket = args.at(0)
let bra = args.at(1, default: ket)

let height = measure($ bra ket $, styles).height;
let phantom = box(
height: height * __height_mul_factor(height.pt()),
width: 0pt, inset: 0pt, stroke: none);
$ lr(bar.v bra#h(0pt)phantom angle.r)lr(angle.l phantom#h(0pt)ket bar.v) $
$ lr(bar.v ket#h(0pt)mid(angle.r#h(0pt)angle.l)#h(0pt)bra bar.v) $
})

#let innerproduct = braket
#let iprod = innerproduct
#let outerproduct = ketbra
#let oprod = outerproduct

// Credit: until https://github.com/typst/typst/issues/240 is addressed by Typst
// we use the same hack as braket().
#let matrixelement(n, M, m) = style(styles => {
let height = measure($ #n #M #m $, styles).height;
let phantom = box(
height: height * __height_mul_factor(height.pt()),
width: 0pt, inset: 0pt, stroke: none);
$ lr(angle.l #n lr(|#M#h(0pt)phantom|) #m angle.r) $
$ lr(angle.l #n#h(0pt)mid(bar.v)#h(0pt)#M#h(0pt)mid(bar.v)#h(0pt)#m angle.r) $
})

#let mel = matrixelement
Expand Down
67 changes: 53 additions & 14 deletions pkg-repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,81 @@

import argparse
import os
import re
import shutil
import sys

from pathlib import Path

FILES = [
abbr_home_path = lambda path: str(path).replace(os.path.expanduser('~'), '~', 1)

def check_version(args):
found_old_ver_strs = False
for filename in os.listdir(Path(__file__).parent):
path = Path(Path(__file__).parent, filename)
if filename.split('.')[-1] not in ["md", "typ", "toml"]:
continue
with open(path, 'r') as f:
for i, line in enumerate(f.readlines()):
# The version strings in these lines are not this package's, so
# they shouldn't be checked. Very naive filtering, but it
# suffices for now.
if (filename.endswith(".toml") and
re.match(r"^compiler = \".+\"", line)):
continue
if re.match(r"[Tt]ypst v?.+", line):
continue
old_vers = list(
filter(lambda e: e != args.version,
re.findall(r"\d+\.\d+\.\d+", line)))
if len(old_vers):
found_old_ver_strs = True
print(
f"{filename}:{i+1} refers to a wrong version:\n" +
f"\t\x1b[2m{line.rstrip()}\x1b[0m")
if found_old_ver_strs:
sys.exit(">> Please fix the lines above.")
else:
print(">> Checked version strings are updated in files.")

FILES_TO_COPY = [
"physica.typ",
"README.md",
"typst.toml",
"LICENSE.txt",
]

def copy_file(dest, file):
source_path = Path(__file__, "..", file).resolve()
target_path = Path(dest, file).resolve()
print(f"{source_path} -->\n\t{target_path}")
def copy_file(dest, filename):
source_path = Path(__file__).parent.joinpath(filename).resolve()
target_path = Path(dest, filename).resolve()
print(f"\x1b[2mCopying {filename}, {os.path.getsize(source_path)} B\x1b[0m")
shutil.copyfile(source_path, target_path)

def copy_files(args):
# https://github.com/typst/packages/tree/main#submission-guidelines
physica_dir = Path(
args.repo_dir, 'packages', 'preview', 'physica', f'{args.version}')
os.makedirs(physica_dir, exist_ok = False)
print(f"Destination: {physica_dir}")
for file in FILES:
copy_file(physica_dir, file)
print("ls:")
try:
os.makedirs(physica_dir, exist_ok = True)
except FileExistsError:
sys.exit(
f"Directory already exists: {abbr_home_path(physica_dir)}")
print(f">> {len(FILES_TO_COPY)} files to copy.")
for filename in FILES_TO_COPY:
copy_file(physica_dir, filename)
print(f">> Files in {abbr_home_path(physica_dir)}:")
print(os.listdir(physica_dir))

def main():
parser = argparse.ArgumentParser('Copy files to official Typst packages repo')
parser.add_argument('--version', required=True, help="version, e.g. 0.7.5")
parser.add_argument('--repo-dir', required=True, help="repo root")
parser = argparse.ArgumentParser('Copy files to the Typst packages repo')
parser.add_argument(
'--version', required=True, help="semantic version, major.minor.build")
parser.add_argument(
'--repo-dir', required=True, help="root of that Typst packages repo")
args = parser.parse_args()

check_version(args)
copy_files(args)

if __name__ == "__main__":
sys.exit(main())
sys.exit(main())
14 changes: 8 additions & 6 deletions typst.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
[package]
name = "physica"
version = "0.8.1"
version = "0.9.0"
description = "Neat derivative, differential, field, matrix, braket, tensor, hbar, etc."
authors = ["Leedehai"]
repository = "https://github.com/Leedehai/typst-physics"
keywords = [
"physics", "brace", "integral", "gradient", "divergence", "curl", "vector",
"field", "matrix", "diagonal", "Jacobian", "Hessian", "Dirac", "braket",
"differential", "derivative", "partial", "dv", "odv", "pdv", "Planck", "hbar",
"tensor", "isotope", "signal", "electromagnetism", "mechanics", "quantum",
"physics", "mathematics", "brace", "set", "product", "evaluate", "restrict",
"integral", "gradient", "divergence", "curl", "vector", "field", "laplacian",
"matrix", "determinant", "diagonal", "identity", "Jacobian", "Hessian",
"Dirac", "braket", "differential", "derivative", "partial", "dv", "odv",
"pdv", "Planck", "hbar", "tensor", "isotope", "signal", "electromagnetism",
"mechanics", "quantum", "relativity", "imaginary", "notation",
]
compiler = "0.8.0"
compiler = "0.10.0"
exclude = ["*.pdf", "*.py"]
entrypoint = "physica.typ"
license = "MIT"

0 comments on commit d541bc6

Please sign in to comment.