-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Christopher Hecker | ||
|
||
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: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Unify | ||
`unify` is a [Typst](https://github.com/typst/typst) package simplifying the typesetting of numbers, units, and ranges. It is the equivalent to LaTeX's `siunitx`, though not as mature. | ||
|
||
|
||
## Overview | ||
`unify` allows flexible numbers and units, and still mostly gets well typeset results. | ||
```typ | ||
#import "@preview/unify:0.7.1": num,qty,numrange,qtyrange | ||
$ num("-1.32865+-0.50273e-6") $ | ||
$ qty("1.3+1.2-0.3e3", "erg/cm^2/s", space: "#h(2mm)") $ | ||
$ numrange("1,1238e-2", "3,0868e5", thousandsep: "'") $ | ||
$ qtyrange("1e3", "2e3", "meter per second squared", per: "/", delimiter: "\"to\"") $ | ||
``` | ||
<img src="examples/overview.jpg" width="300"> | ||
|
||
Right now, physical, monetary, and binary units are supported. New issues or pull requests for new units are welcome! | ||
|
||
## Multilingual support | ||
The Unify package supports multiple languages. Currently, the supported languages are English and Russian. The fallback is English. If you want to add your language, you should add two files: `prefixes-xx.csv` and `units-xx.csv`, and in the `lib.typ` file you should fix the `lang-db` state for your files. | ||
|
||
## `num` | ||
`num` uses string parsing in order to typeset numbers, including separators between the thousands. They can have the following form: | ||
- `float` or `integer` number | ||
- either (`{}` stands for a number) | ||
- symmetric uncertainties with `+-{}` | ||
- asymmetric uncertainties with `+{}-{}` | ||
- exponential notation `e{}` | ||
|
||
Parentheses are automatically set as necessary. Use `thousandsep` to change the separator between the thousands, and `multiplier` to change the multiplication symbol between the number and exponential. | ||
|
||
*Note: Because units are evaluated in math mode, plain spaces (`thousandsep: " "`) will not have the desired effect. Use `thousandsep: "space"` instead.* | ||
|
||
|
||
## `unit` | ||
`unit` takes the unit in words or in symbolic notation as its first argument. The value of `space` will be inserted between units if necessary. Setting `per` to `symbol` will format the number with exponents (i.e. `^(-1)`), `fraction` or `/` using fraction, and `fraction-short` or `\\/` using in-line fractions. | ||
Units in words have four possible parts: | ||
- `per` forms the inverse of the following unit. | ||
- A written-out prefix in the sense of SI (e.g. `centi`). This is added before the unit. | ||
- The unit itself written out (e.g. `gram`). | ||
- A postfix like `squared`. This is added after the unit and takes `per` into account. | ||
|
||
The shorthand notation also has four parts: | ||
- `/` forms the inverse of the following unit. | ||
- A short prefix in the sense of SI (e.g. `k`). This is added before the unit. | ||
- The short unit itself (e.g. `g`). | ||
- An exponent like `^2`. This is added after the unit and takes `/` into account. | ||
|
||
Note: Use `u` for micro. | ||
|
||
The possible values of the three latter parts are loaded at runtime from `prefixes.csv`, `units.csv`, and `postfixes.csv` (in the library directory). Your own units etc. can be permanently added in these files. At runtime, they can be added using `add-unit` and `add-prefix`, respectively. The formats for the pre- and postfixes are: | ||
|
||
| pre-/postfix | shorthand | symbol | | ||
| ------------ | --------- | ------------ | | ||
| milli | m | upright("m") | | ||
|
||
and for units: | ||
|
||
| unit | shorthand | symbol | space | | ||
| ----- | --------- | ------------ | ----- | | ||
| meter | m | upright("m") | true | | ||
|
||
The first column specifies the written-out word, the second one the shorthand. These should be unique. The third column represents the string that will be inserted as the unit symbol. For units, the last column describes whether there should be space before the unit (possible values: `true`/`false`, `1`,`0`). This is mostly the cases for degrees and other angle units (e.g. arcseconds). | ||
If you think there are units not included that are of interest for other users, you can create an issue or PR. | ||
|
||
|
||
## `qty` | ||
`qty` allows a `num` as the first argument following the same rules. The second argument is a unit. If `rawunit` is set to true, its value will be passed on to the result (note that the string passed on will be passed to `eval`, so add escaped quotes `\"` if necessary). Otherwise, it follows the rules of `unit`. The value of `space` will be inserted between units if necessary, `thousandsep` between the thousands, and `per` switches between exponents and fractions. | ||
|
||
|
||
## `numrange` | ||
`numrange` takes two `num`s as the first two arguments. If they have the same exponent, it is automatically factorized. The range symbol can be changed with `delimiter`, and the space between the numbers and symbols with `space`. | ||
|
||
|
||
## `qtyrange` | ||
`qtyrange` is just a combination of `unit` and `range`. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#import "../lib.typ": * | ||
|
||
#set text(lang: "en") | ||
Working with English characters: | ||
$ num("-1.32865+-0.50273e-6") $ | ||
$ qty("1.3+1.2-0.3e3", "erg/cm^2/s", space: "#h(2mm)") $ | ||
$ numrange("1,1238e-2", "3,0868e5", thousandsep: "'") $ | ||
$ qtyrange("1e3", "2e3", "meter per second squared", per: "/", delimiter: "\"to\"") $ | ||
$ qty("55.36", "usd") $ | ||
|
||
Adding your own prefix and unit: | ||
#add-prefix("pre", "P", "upright(\"pre\")") | ||
#add-unit("unit", "U", "bold(\"unit\")") | ||
$ unit("PU") $ | ||
|
||
#set text(lang: "ru") | ||
Работа пакета с русскими символами: | ||
$ num("-1.32865+-0.50273e-6") $ | ||
$ qty("1.3+1.2-0.3e3", "erg/cm^2/s", space: "#h(2mm)") $ | ||
$ numrange("1,1238e-2", "3,0868e5", thousandsep: "'") $ | ||
$ qtyrange("1e3", "2e3", "meter per second squared", per: "/", delimiter: "\"до\"") $ | ||
|
||
#set text(lang: "de") | ||
Other languages fall back to English units: | ||
$ num("-1.32865+-0.50273e-6") $ | ||
$ qty("1.3+1.2-0.3e3", "erg/cm^2/s", space: "#h(2mm)") $ | ||
$ numrange("1,1238e-2", "3,0868e5", thousandsep: "'") $ | ||
$ qtyrange("1e3", "2e3", "meter per second squared", per: "/", delimiter: "\"to\"") $ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.