| 1 |
% texdimens 1.1 |
| 2 |
% Jean-François Burnol |
| 3 |
% 2021/11/17 |
| 4 |
|
| 5 |
## Copyright and License |
| 6 |
|
| 7 |
Copyright (c) 2021 Jean-François Burnol |
| 8 |
|
| 9 |
The texdimens [CTAN package](https://ctan.org/pkg/texdimens) is |
| 10 |
distributed under the [LPPL 1.3c](https://ctan.org/license/lppl1.3c). |
| 11 |
|
| 12 |
## Usage |
| 13 |
|
| 14 |
Utilities and documentation related to TeX dimensional units, usable: |
| 15 |
|
| 16 |
- with Plain TeX: `\input texdimens` |
| 17 |
|
| 18 |
- with LaTeX: `\usepackage{texdimens}` |
| 19 |
|
| 20 |
For reporting issues, use the |
| 21 |
[package repository](https://github.com/jfbu/texdimens). |
| 22 |
|
| 23 |
## Aim of this package |
| 24 |
|
| 25 |
The aim of this package is to provide facilities to express dimensions |
| 26 |
(or dimension expressions evaluated by `\dimexpr`) using the various |
| 27 |
available TeX units, to the extent possible. |
| 28 |
|
| 29 |
## Macros of this package (summary) |
| 30 |
|
| 31 |
This package provides expandable macros: |
| 32 |
|
| 33 |
- `\texdimenUU` with `UU` standing for one of `pt`, `bp`, `cm`, `mm`, |
| 34 |
`in`, `pc`, `cc`, `nc`, `dd` and `nd`, |
| 35 |
- `\texdimenUUup` and `\texdimenUUdown` with `UU` as above except `pt`, |
| 36 |
- `\texdimenbothincm` and relatives, |
| 37 |
- `\texdimenbothbpmm` and relatives, |
| 38 |
- `\texdimenwithunit`. |
| 39 |
|
| 40 |
`\texdimenbp` takes on input some dimension or dimension expression and |
| 41 |
produces on output a decimal `D` such that `D bp` is guaranteed to be |
| 42 |
the same dimension as the input, *if* it admits any representation as `E bp`; |
| 43 |
else it will be either the closest match from above or from |
| 44 |
below. For this unit, as well as for `nd` and `dd` the difference is at |
| 45 |
most `1sp`. For other units (not `pt` of course) the distance will |
| 46 |
usually be larger than `1sp` and one does not know if the approximant |
| 47 |
from the other direction would have been better or worst. |
| 48 |
|
| 49 |
The variants `\texdimenbpup` and `\texdimenbpdown` expand slightly less |
| 50 |
fast than `\texdimenbp` but they allow to choose the direction of |
| 51 |
approximation (in absolute value). |
| 52 |
|
| 53 |
The macros associated to the other units have |
| 54 |
the same descriptions. |
| 55 |
|
| 56 |
`\texdimenbothincm`, respectively `\texdimenbothbpmm`, find the largest |
| 57 |
(in absolute value) dimension not exceeding the input and exactly |
| 58 |
representable both with the `in` and `cm` units, respectively exactly |
| 59 |
representable both with the `bp` and `mm` units. |
| 60 |
|
| 61 |
`\texdimenwithunit{<dimen1>}{<dimen2>}` produces a decimal `D` such that |
| 62 |
`D \dimexpr dimen2\relax` is parsed by TeX into the same dimension as |
| 63 |
`dimen1` if this is at all possible. If `dimen2<1pt` all TeX dimensions |
| 64 |
`dimen1` are attainable. If `dimen2>1pt` not all `dimen1` are |
| 65 |
attainable. If not attainable, the decimal `D` will ensure a closest |
| 66 |
match from below or from above but one does not know if the |
| 67 |
approximation from the other direction is better or worst. |
| 68 |
|
| 69 |
In a sense, this macro divides `<dimen1>` by `<dimen2>`, see additional |
| 70 |
details in the complete macro description. |
| 71 |
|
| 72 |
## Quick review of basics: TeX points and scaled points |
| 73 |
|
| 74 |
TeX dimensions are represented internally by a signed integer which is |
| 75 |
in absolute value at most `0x3FFFFFFF`, i.e. `1073741823`. The |
| 76 |
corresponding unit is called the "scaled point", i.e. `1sp` is `1/65536` |
| 77 |
of one TeX point `1pt`, or rather `1pt` is represented internally as `65536`. |
| 78 |
|
| 79 |
If `\foo` is a dimen register: |
| 80 |
|
| 81 |
- `\number\foo` produces the integer `N` such as `\foo` is the same as `Nsp`, |
| 82 |
|
| 83 |
- inside `\numexpr`, `\foo` is replaced by `N`, |
| 84 |
|
| 85 |
- `\the\foo` produces a decimal `D` (with at most five places) followed |
| 86 |
with `pt` (catcode 12 tokens) and this output `Dpt` can serve as input |
| 87 |
in a dimen assignment to produce the same dimension as `\foo`. One can |
| 88 |
also use the catcode 11 characters `pt` for this. Digits and decimal |
| 89 |
mark must have their standard catcode 12. |
| 90 |
|
| 91 |
When TeX encounters a dimen denotation of the type `Dpt` it will |
| 92 |
compute `N` in a way equivalent to `N = round(65536 D)` where ties are |
| 93 |
rounded away from zero. Only 17 decimal places of `D` are |
| 94 |
kept as it can be shown that going beyond can not change the result. |
| 95 |
|
| 96 |
When `\foo` has been assigned as `Dpt`, `\the\foo` will produce some |
| 97 |
`Ept` where `E` is not necessarily the same as `D`. But it is guaranteed |
| 98 |
that `Ept` defines the same dimension as `Dpt`. |
| 99 |
|
| 100 |
## Further units known to TeX on input |
| 101 |
|
| 102 |
TeX understands on input further units: `bp`, `cm`, `mm`, `in`, `pc`, |
| 103 |
`cc`, `nc`, `dd` and `nd`. It also understands font-dependent units |
| 104 |
`ex` and `em`, and PDFTeX adds the `px` dimension unit. Japanese |
| 105 |
engines also add specific units. |
| 106 |
|
| 107 |
The `ex`, `em`, and `px` units are handled somewhat differently by |
| 108 |
(pdf)TeX than `bp`, `cm`, `mm`, `in`, `pc`, `cc`, `nc`, `dd` and `nd` |
| 109 |
units. For the former (let's use the generic notation `uu`), the exact |
| 110 |
same dimensions are obtained from an input `D uu` where `D` is some |
| 111 |
decimal or from `D <dimen>` where `<dimen>` stands for some dimension |
| 112 |
register which records `1uu` or `\dimexpr1uu\relax`. In contrast, among |
| 113 |
the latter, i.e. the core TeX units, this is false except for the `pc` |
| 114 |
unit. |
| 115 |
|
| 116 |
TeX associates (explicitly for the core units, implicitly for the units |
| 117 |
corresponding to internal dimensions) to each unit `uu` a fraction `phi` |
| 118 |
which is a conversion factor. For the internal dimensions `ex`, `em`, |
| 119 |
`px` or in the case of multiplying a dimension by a decimal, this `phi` |
| 120 |
is morally `f/65536` where `f` is the integer such that `1 uu=f sp`. |
| 121 |
For core units however, the hard-coded ratio `n/d` never has a |
| 122 |
denominator `d` which is a power of `2`, except for the `pc` whose |
| 123 |
associated ratio factor is `12/1` (and arguably for the `sp` for which |
| 124 |
morally `phi` is `1/65536` but we keep it separate from the general |
| 125 |
discussion; as well as `pt` with its unit conversion factor). |
| 126 |
|
| 127 |
Here is a table with the hard-coded conversion factors: |
| 128 |
|
| 129 |
uu phi reduced real approximation 1uu in sp= \the<1uu> |
| 130 |
(Python output) [65536phi] |
| 131 |
-- ---------- ------- ------------------ --------- ---------- |
| 132 |
bp 7227/7200 803/800 1.00375 65781 1.00374pt |
| 133 |
nd 685/642 same 1.0669781931464175 69925 1.06697pt |
| 134 |
dd 1238/1157 same 1.070008643042351 70124 1.07pt |
| 135 |
mm 7227/2540 same 2.8452755905511813 186467 2.84526pt |
| 136 |
pc 12/1 12 12.0 786432 12.0pt |
| 137 |
nc 1370/107 same 12.80373831775701 839105 12.80373pt |
| 138 |
cc 14856/1157 same 12.84010371650821 841489 12.8401pt |
| 139 |
cm 7227/254 same 28.45275590551181 1864679 28.45274pt |
| 140 |
in 7227/100 same 72.27 4736286 72.26999pt |
| 141 |
|
| 142 |
The values of `1uu` in the `sp` and `pt` units are irrelevant and even |
| 143 |
misleading regarding the TeX parsing of `D uu` input. Notice for |
| 144 |
example that `\the\dimexpr1bp\relax` gives `1.00374pt` but the actual |
| 145 |
conversion factor is `1.00375` (and |
| 146 |
`1.00375pt=65782sp>1bp`...). Similarly `\the\dimexpr1in\relax` outputs |
| 147 |
`72.26999pt` and is represented internally as `4736286sp` but the actual |
| 148 |
conversion factor is `72.27=7227/100`, and |
| 149 |
`72.27pt=4736287sp>1in`... And for the other units except the `pc`, the |
| 150 |
conversion factors are not decimal numbers, so even less likely to match |
| 151 |
`\the<1uu>` as listed in the last column. Their denominators are not |
| 152 |
powers of `2` so they don't match exactly either `(1uu in sp)/65536` but |
| 153 |
are only close. |
| 154 |
|
| 155 |
When TeX parses an assignment `U uu` with a decimal `U` and a unit `uu`, |
| 156 |
be it a core unit, or a unit corresponding to an internal dimension, |
| 157 |
it first handles `U` as with the `pt` unit. |
| 158 |
This means that it computes `N = round(65536*U)`. It then multiplies |
| 159 |
this `N` by the conversion factor `phi` and truncates towards zero the |
| 160 |
mathematically exact result to obtain an integer `T`: |
| 161 |
`T=trunc(N*phi)`. The assignment `Uuu` is concluded by defining the |
| 162 |
value of the dimension to be `Tsp`. |
| 163 |
|
| 164 |
Regarding the core units, we always have `phi>1`. |
| 165 |
The increasing |
| 166 |
sequence `0<=trunc(phi)<=trunc(2phi)<=...` is thus *strictly increasing* |
| 167 |
and, as `phi` is never astronomically close to `1`, |
| 168 |
**it always has jumps**: not all TeX dimensions can be obtained from an |
| 169 |
assignment using a core unit distinct from the `pt` (and `sp` of course, |
| 170 |
but we already said it was kept out of the discussion here). |
| 171 |
|
| 172 |
On the other hand when `phi<1`, then the sequence `trunc(N phi)` is not |
| 173 |
strictly increasing, already because `trunc(phi)=0` and besides here |
| 174 |
`phi=f/65536`, so the `65536` integers `0..65535` are mapped to `f` |
| 175 |
integers `0..(f-1)` inducing non one-to-oneness. But |
| 176 |
all integers in the `0..(2**30-1)` range will be attained for some input, |
| 177 |
so there is surjectivity. |
| 178 |
|
| 179 |
The "worst" unit is the largest i.e. the `in` whose conversion factor is |
| 180 |
`72.27`. The simplest unit to understand is the `pc` as it corresponds |
| 181 |
to an integer ratio `12`: only dimensions which in scaled points are |
| 182 |
multiple of `12` are exactly representable in the `pc` unit. |
| 183 |
|
| 184 |
This also means that some dimensions expressible in one unit may not be |
| 185 |
available with another unit. For example, and perhaps surprisingly, |
| 186 |
there is no decimal `D` which would achieve `1in==Dcm`: the "step" |
| 187 |
between attainable dimensions is `72--73sp` for the `in` and `28--29sp` |
| 188 |
for the `cm`, and as `1in` differs internally from `2.54cm` by only |
| 189 |
`12sp` it is impossible to adjust |
| 190 |
either the `in` side or the `cm` side to obtain equality. |
| 191 |
|
| 192 |
In particular `1in==2.54cm` is **false** in TeX, but it is true that |
| 193 |
`100in==254cm`... (it is already true that `50in==127cm`). It is also |
| 194 |
false that `10in==25.4cm` but it is true that `10in==254mm`... It is |
| 195 |
false though that `1in==25.4mm`! |
| 196 |
|
| 197 |
>>> (\dimexpr1in, \dimexpr2.54cm); |
| 198 |
@_1 4736286, 4736274 |
| 199 |
>>> (\dimexpr10in, \dimexpr25.4cm); |
| 200 |
@_2 47362867, 47362855 |
| 201 |
>>> (\dimexpr100in, \dimexpr254cm); |
| 202 |
@_3 473628672, 473628672 |
| 203 |
|
| 204 |
>>> (\dimexpr1in, \dimexpr25.4mm); |
| 205 |
@_4 4736286, 4736285 |
| 206 |
>>> (\dimexpr10in, \dimexpr254mm); |
| 207 |
@_5 47362867, 47362867 |
| 208 |
|
| 209 |
`\maxdimen` can be expressed only with `pt`, `bp`, and `nd`. For the |
| 210 |
other core units the maximal attainable dimensions in `sp` unit are given in |
| 211 |
the middle column of the next table. |
| 212 |
|
| 213 |
maximal allowed the corresponding minimal TeX dimen denotation |
| 214 |
(with 5 places) maximal attainable dim. causing "Dimension too large" |
| 215 |
--------------- ------------------------- -------------------------- |
| 216 |
16383.99999pt 1073741823sp (=\maxdimen) 16383.99999237060546875pt |
| 217 |
16322.78954bp 1073741823sp (=\maxdimen) 16322.78954315185546875bp |
| 218 |
15355.51532nd 1073741823sp (=\maxdimen) 15355.51532745361328125nd |
| 219 |
15312.02584dd 1073741822sp 15312.02584075927734375dd |
| 220 |
5758.31742mm 1073741822sp 5758.31742095947265625mm |
| 221 |
1365.33333pc 1073741820sp 1365.33333587646484375pc |
| 222 |
1279.62627nc 1073741814sp 1279.62627410888671875nc |
| 223 |
1276.00215cc 1073741821sp 1276.00215911865234375cc |
| 224 |
575.83174cm 1073741822sp 575.83174896240234375cm |
| 225 |
226.70540in 1073741768sp 226.70540618896484375in |
| 226 |
|
| 227 |
Perhaps for these various peculiarities with dimensional units, TeX does |
| 228 |
not provide an output facility for them similar to what `\the` achieves for |
| 229 |
the `pt`. |
| 230 |
|
| 231 |
## Macros of this package (full list) |
| 232 |
|
| 233 |
This project requires the `\dimexpr`, `\numexpr` e-TeX extensions. It |
| 234 |
also requires the `\expanded` primitive (available in all engines since |
| 235 |
TeXLive 2019). |
| 236 |
|
| 237 |
The macros provided by the package are all expandable, even |
| 238 |
f-expandable. They parse their arguments via `\dimexpr` so can be nested |
| 239 |
(with appropriate units added, as the outputs always are bare decimal |
| 240 |
numbers). |
| 241 |
|
| 242 |
The notation `<dim. expr.>` in the macro descriptions refers to a |
| 243 |
*dimensional expression* as accepted by `\dimexpr`. The syntax has some |
| 244 |
peculiarities: among them the fact that `-(...)` (for example `-(3pt)`) |
| 245 |
is illegal, one must use alternatives such as `0pt-(...)` or a |
| 246 |
sub-expression `-\dimexpr...\relax` for example. |
| 247 |
|
| 248 |
Negative dimensions behave as if replaced by their absolute value, then |
| 249 |
at last step the sign (if result is not zero) is applied (so "down" means |
| 250 |
"towards zero", and "up" means "away from zero"). |
| 251 |
|
| 252 |
Remarks about "Dimension too large" issues: |
| 253 |
|
| 254 |
1. For input `X` equal to `\maxdimen` (or differing by a few `sp`'s) and |
| 255 |
those units `uu` for which `\maxdimen` is not exactly representable |
| 256 |
(i.e. all core units except `pt`, `bp` and `nd`), the output `D` of |
| 257 |
the "up" macros `\texdimen<uu>up{X}`, if used as `Duu` in a dimension |
| 258 |
assignment or expression, will (as is logical) trigger a "Dimension |
| 259 |
too large" error. |
| 260 |
2. For `dd`, `nc` and `in`, it turns out that `\texdimen<uu>{X}` chooses |
| 261 |
the "up" approximant for `X` equal to or very near `\maxdimen` (check |
| 262 |
the respective macro documentations), |
| 263 |
i.e. the output `D` is such that `Duu` is the first virtually |
| 264 |
attainable dimension beyond `\maxdimen`. Hence `Duu` will trigger on |
| 265 |
use a "Dimension too large error". With the other units for which |
| 266 |
`\maxdimen` is not attainable exactly, `\texdimen<uu>{\maxdimen}` |
| 267 |
output is by luck the "down" approximant. |
| 268 |
3. Similarly the macro `\texdimenwithunit{D1pt}{D2pt}` covers the entire |
| 269 |
dimension range, but its output `F` for `D1pt` equal to or very close |
| 270 |
to `\maxdimen` may be such that `F<D2pt>` represents a dimension |
| 271 |
beyond `\maxdimen`, if the latter is not exactly representable. |
| 272 |
Hence `F<D2pt>` would trigger "Dimension too large" on use. This can |
| 273 |
only happen if `D2pt>1pt` and (roughly) `D1pt>\maxdimen-D2sp`. As |
| 274 |
`D2sp` is less than `0.25pt`, this is not likely to occur in real |
| 275 |
life practice except if deliberately targeting `\maxdimen`. For |
| 276 |
`D2pt<1pt`, all dimensions `D1pt` are exactly representable, in |
| 277 |
particular `\maxdimen`, and the output `F` will always be such that |
| 278 |
TeX parses `F<D2pt>` into exactly the same dimension as `D1pt`. |
| 279 |
|
| 280 |
### `\texdimenpt{<dim. expr.>}` |
| 281 |
|
| 282 |
> Does `\the\dimexpr <dim. expr.> \relax` then removes the `pt`. |
| 283 |
|
| 284 |
### `\texdimenbp{<dim. expr.>}` |
| 285 |
|
| 286 |
> Produces a decimal (with up to five decimal places) `D` such that `Dbp` |
| 287 |
> represents the dimension exactly if possible. If not possible it |
| 288 |
> will differ by `1sp` from the original dimension, but it is not |
| 289 |
> known in advance if it will be above or below. |
| 290 |
|
| 291 |
> `\maxdimen` on input produces `16322.78954` and indeed is realized as |
| 292 |
> `16322.78954bp`. |
| 293 |
|
| 294 |
### `\texdimenbpdown{<dim. expr.>}` |
| 295 |
|
| 296 |
> Produces a decimal (with up to five decimal places) `D` such that `Dbp` |
| 297 |
> represents the dimension exactly if possible. If not possible it |
| 298 |
> will be smaller by `1sp` from the original dimension. |
| 299 |
|
| 300 |
### `\texdimenbpup{<dim. expr.>}` |
| 301 |
|
| 302 |
> Produces a decimal (with up to five decimal places) `D` such that `Dbp` |
| 303 |
> represents the dimension exactly if possible. If not possible it |
| 304 |
> will be larger by `1sp` from the original dimension. |
| 305 |
|
| 306 |
### `\texdimennd{<dim. expr.>}` |
| 307 |
|
| 308 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnd` |
| 309 |
> represents the dimension exactly if possible. If not possible it |
| 310 |
> will differ by `1sp` from the original dimension, but it is not |
| 311 |
> known in advance if it will be above or below. |
| 312 |
|
| 313 |
> `\maxdimen` on input produces `15355.51532` and indeed is realized as |
| 314 |
> `15355.51532nd`. |
| 315 |
|
| 316 |
### `\texdimennddown{<dim. expr.>}` |
| 317 |
|
| 318 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnd` |
| 319 |
> represents the dimension exactly if possible. If not possible it |
| 320 |
> will be smaller by `1sp` from the original dimension. |
| 321 |
|
| 322 |
### `\texdimenndup{<dim. expr.>}` |
| 323 |
|
| 324 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnd` |
| 325 |
> represents the dimension exactly if possible. If not possible it |
| 326 |
> will be larger by `1sp` from the original dimension. |
| 327 |
|
| 328 |
### `\texdimendd{<dim. expr.>}` |
| 329 |
|
| 330 |
> Produces a decimal (with up to five decimal places) `D` such that `Ddd` |
| 331 |
> represents the dimension exactly if possible. If not possible it |
| 332 |
> will differ by `1sp` from the original dimension, but it is not |
| 333 |
> known in advance if it will be above or below. |
| 334 |
|
| 335 |
> Warning: the output for `\maxdimen` is `15312.02585` but `15312.02585dd` |
| 336 |
> will trigger on use "Dimension too large" error. |
| 337 |
> `\maxdimen-1sp` is the maximal input for which the output remains |
| 338 |
> less than `\maxdimen` (max attainable dimension: `\maxdimen-1sp`). |
| 339 |
|
| 340 |
### `\texdimendddown{<dim. expr.>}` |
| 341 |
|
| 342 |
> Produces a decimal (with up to five decimal places) `D` such that `Ddd` |
| 343 |
> represents the dimension exactly if possible. If not possible it |
| 344 |
> will be smaller by `1sp` from the original dimension. |
| 345 |
|
| 346 |
### `\texdimenddup{<dim. expr.>}` |
| 347 |
|
| 348 |
> Produces a decimal (with up to five decimal places) `D` such that `Ddd` |
| 349 |
> represents the dimension exactly if possible. If not possible it |
| 350 |
> will be larger by `1sp` from the original dimension. |
| 351 |
> |
| 352 |
> If input is `\maxdimen`, then `Ddd` virtually represents |
| 353 |
> `\maxdimen+1sp` and will trigger on use "Dimension too large". |
| 354 |
|
| 355 |
### `\texdimenmm{<dim. expr.>}` |
| 356 |
|
| 357 |
> Produces a decimal (with up to five decimal places) `D` such that `Dmm` |
| 358 |
> represents the dimension exactly if possible. If not possible it |
| 359 |
> will either be the closest from below or from above, but it is not |
| 360 |
> known in advance which one (and it is not known if the other choice |
| 361 |
> would have been closer). |
| 362 |
|
| 363 |
> `\maxdimen` as input produces on output `5758.31741` and indeed the |
| 364 |
> maximal attainable dimension is `5758.31741mm` (`\maxdimen-1sp`). |
| 365 |
|
| 366 |
### `\texdimenmmdown{<dim. expr.>}` |
| 367 |
|
| 368 |
> Produces a decimal (with up to five decimal places) `D` such that `Dmm` |
| 369 |
> represents the dimension exactly if possible. If not possible it |
| 370 |
> will be largest representable dimension smaller than the original one. |
| 371 |
|
| 372 |
### `\texdimenmmup{<dim. expr.>}` |
| 373 |
|
| 374 |
> Produces a decimal (with up to five decimal places) `D` such that `Dmm` |
| 375 |
> represents the dimension exactly if possible. If not possible it |
| 376 |
> will be smallest representable dimension larger than the original one. |
| 377 |
> |
| 378 |
> If input is `\maxdimen`, then `Dmm` virtually represents |
| 379 |
> `\maxdimen+2sp` and will trigger on use "Dimension too large". |
| 380 |
|
| 381 |
### `\texdimenpc{<dim. expr.>}` |
| 382 |
|
| 383 |
> Produces a decimal (with up to five decimal places) `D` such that `Dpc` |
| 384 |
> represents the dimension exactly if possible. If not possible it |
| 385 |
> will be the closest representable one (in case of tie, the approximant |
| 386 |
> from above is chosen). |
| 387 |
|
| 388 |
> `\maxdimen` as input produces on output `1365.33333` and indeed the |
| 389 |
> maximal attainable dimension is `1365.33333pc` (`\maxdimen-3sp`). |
| 390 |
|
| 391 |
### `\texdimenpcdown{<dim. expr.>}` |
| 392 |
|
| 393 |
> Produces a decimal (with up to five decimal places) `D` such that `Dpc` |
| 394 |
> represents the dimension exactly if possible. If not possible it |
| 395 |
> will be largest representable dimension smaller than the original one. |
| 396 |
|
| 397 |
### `\texdimenpcup{<dim. expr.>}` |
| 398 |
|
| 399 |
> Produces a decimal (with up to five decimal places) `D` such that `Dpc` |
| 400 |
> represents the dimension exactly if possible. If not possible it |
| 401 |
> will be smallest representable dimension larger than the original one. |
| 402 |
> |
| 403 |
> If input is `>\maxdimen-3sp`, then `Dpc` virtually represents |
| 404 |
> `\maxdimen+9sp` and will trigger on use "Dimension too large". |
| 405 |
|
| 406 |
### `\texdimennc{<dim. expr.>}` |
| 407 |
|
| 408 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnc` |
| 409 |
> represents the dimension exactly if possible. If not possible it |
| 410 |
> will either be the closest from below or from above, but it is not |
| 411 |
> known in advance which one (and it is not known if the other choice |
| 412 |
> would have been closer). |
| 413 |
|
| 414 |
> Warning: the output for `\maxdimen-1sp` is `1279.62628` but `1279.62628nc` |
| 415 |
> will trigger on use "Dimension too large" error. |
| 416 |
> `\maxdimen-2sp` is the maximal input for which the output remains |
| 417 |
> less than `\maxdimen` (max attainable dimension: `\maxdimen-9sp`). |
| 418 |
|
| 419 |
### `\texdimenncdown{<dim. expr.>}` |
| 420 |
|
| 421 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnc` |
| 422 |
> represents the dimension exactly if possible. If not possible it |
| 423 |
> will be largest representable dimension smaller than the original one. |
| 424 |
|
| 425 |
### `\texdimenncup{<dim. expr.>}` |
| 426 |
|
| 427 |
> Produces a decimal (with up to five decimal places) `D` such that `Dnc` |
| 428 |
> represents the dimension exactly if possible. If not possible it |
| 429 |
> will be smallest representable dimension larger than the original one. |
| 430 |
> |
| 431 |
> If input is `>\maxdimen-9sp`, then `Dnc` virtually represents |
| 432 |
> `\maxdimen+4sp` and will trigger on use "Dimension too large". |
| 433 |
|
| 434 |
### `\texdimencc{<dim. expr.>}` |
| 435 |
|
| 436 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcc` |
| 437 |
> represents the dimension exactly if possible. If not possible it |
| 438 |
> will either be the closest from below or from above, but it is not |
| 439 |
> known in advance which one (and it is not known if the other choice |
| 440 |
> would have been closer). |
| 441 |
|
| 442 |
> `\maxdimen` as input produces on output `1276.00215` and indeed the |
| 443 |
> maximal attainable dimension is `1276.00215cc` (`\maxdimen-2sp`). |
| 444 |
|
| 445 |
### `\texdimenccdown{<dim. expr.>}` |
| 446 |
|
| 447 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcc` |
| 448 |
> represents the dimension exactly if possible. If not possible it |
| 449 |
> will be largest representable dimension smaller than the original one. |
| 450 |
|
| 451 |
### `\texdimenccup{<dim. expr.>}` |
| 452 |
|
| 453 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcc` |
| 454 |
> represents the dimension exactly if possible. If not possible it |
| 455 |
> will be smallest representable dimension larger than the original one. |
| 456 |
> |
| 457 |
> If input is `>\maxdimen-2sp`, then `Dcc` virtually represents |
| 458 |
> `\maxdimen+11sp` and will trigger on use "Dimension too large". |
| 459 |
|
| 460 |
### `\texdimencm{<dim. expr.>}` |
| 461 |
|
| 462 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcm` |
| 463 |
> represents the dimension exactly if possible. If not possible it |
| 464 |
> will either be the closest from below or from above, but it is not |
| 465 |
> known in advance which one (and it is not known if the other choice |
| 466 |
> would have been closer). |
| 467 |
|
| 468 |
> `\maxdimen` as input produces on output `575.83174` and indeed the |
| 469 |
> maximal attainable dimension is `575.83174cm` (`\maxdimen-1sp`). |
| 470 |
|
| 471 |
### `\texdimencmdown{<dim. expr.>}` |
| 472 |
|
| 473 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcm` |
| 474 |
> represents the dimension exactly if possible. If not possible it |
| 475 |
> will be largest representable dimension smaller than the original one. |
| 476 |
|
| 477 |
### `\texdimencmup{<dim. expr.>}` |
| 478 |
|
| 479 |
> Produces a decimal (with up to five decimal places) `D` such that `Dcm` |
| 480 |
> represents the dimension exactly if possible. If not possible it |
| 481 |
> will be smallest representable dimension larger than the original one. |
| 482 |
> |
| 483 |
> If input is `\maxdimen`, then `Dcm` virtually represents |
| 484 |
> `\maxdimen+28sp` and will trigger on use "Dimension too large". |
| 485 |
|
| 486 |
### `\texdimenin{<dim. expr.>}` |
| 487 |
|
| 488 |
> Produces a decimal (with up to five decimal places) `D` such that `Din` |
| 489 |
> represents the dimension exactly if possible. If not possible it |
| 490 |
> will either be the closest from below or from above, but it is not |
| 491 |
> known in advance which one (and it is not known if the other choice |
| 492 |
> would have been closer). |
| 493 |
|
| 494 |
> Warning: the output for `\maxdimen-18sp` is `226.70541` but `226.70541in` |
| 495 |
> will trigger on use "Dimension too large" error. |
| 496 |
> `\maxdimen-19sp` is the maximal input for which the output remains |
| 497 |
> less than `\maxdimen` (max attainable dimension: `\maxdimen-55sp`). |
| 498 |
|
| 499 |
### `\texdimenindown{<dim. expr.>}` |
| 500 |
|
| 501 |
> Produces a decimal (with up to five decimal places) `D` such that `Din` |
| 502 |
> represents the dimension exactly if possible. If not possible it |
| 503 |
> will be largest representable dimension smaller than the original one. |
| 504 |
|
| 505 |
### `\texdimeninup{<dim. expr.>}` |
| 506 |
|
| 507 |
> Produces a decimal (with up to five decimal places) `D` such that `Din` |
| 508 |
> represents the dimension exactly if possible. If not possible it |
| 509 |
> will be smallest representable dimension larger than the original one. |
| 510 |
> |
| 511 |
> If input is `>\maxdimen-55sp`, then `Din` virtually represents |
| 512 |
> `\maxdimen+17sp` and will trigger on use "Dimension too large". |
| 513 |
|
| 514 |
### `\texdimenbothcmin{<dim. expr.>}` |
| 515 |
|
| 516 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 517 |
> `Din` is the largest dimension not exceeding the original one (in |
| 518 |
> absolute value) and exactly representable both in the `in` and `cm` |
| 519 |
> units. |
| 520 |
|
| 521 |
### `\texdimenbothincm{<dim. expr.>}` |
| 522 |
|
| 523 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 524 |
> `Dcm` is the largest dimension not exceeding the original one (in |
| 525 |
> absolute value) and exactly representable both in the `in` and `cm` |
| 526 |
> units. Thus both expressions `\texdimenbothcmin{<dim. expr.>}in` and |
| 527 |
> `\texdimenbothincm{<dim. expr.>}cm` represent the same dimension. |
| 528 |
|
| 529 |
### `\texdimenbothcminpt{<dim. expr.>}` |
| 530 |
|
| 531 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 532 |
> `Dpt` is the largest dimension not exceeding the original one (in |
| 533 |
> absolute value) and exactly representable both in the `in` and `cm` |
| 534 |
> units. It thus represents the same dimension as the one determined by |
| 535 |
> `\texdimenbothcmin` and `\texdimenbothincm`. |
| 536 |
|
| 537 |
### `\texdimenbothincmpt{<dim. expr.>}` |
| 538 |
|
| 539 |
> Alias for `\texdimenbothcminpt`. |
| 540 |
|
| 541 |
### `\texdimenbothcminsp{<dim. expr.>}` |
| 542 |
|
| 543 |
> Produces an integer (explicit digit tokens) `N` such that `Nsp` is the |
| 544 |
> largest dimension not exceeding the original one in absolute value and |
| 545 |
> exactly representable both in the `in` and `cm` units. |
| 546 |
|
| 547 |
### `\texdimenbothincmsp{<dim. expr.>}` |
| 548 |
|
| 549 |
> Alias for `\texdimenbothcminsp`. |
| 550 |
|
| 551 |
### `\texdimenbothbpmm{<dim. expr.>}` |
| 552 |
|
| 553 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 554 |
> `Dmm` is the largest dimension smaller (in absolute value) than the |
| 555 |
> original one and exactly representable both in the `bp` and `mm` |
| 556 |
> units. |
| 557 |
|
| 558 |
### `\texdimenbothmmbp{<dim. expr.>}` |
| 559 |
|
| 560 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 561 |
> `Dbp` is the largest dimension smaller (in absolute value) than the |
| 562 |
> original one and exactly representable both in the `bp` and `mm` |
| 563 |
> units. Thus `\texdimenbothmmbp{<dim. expr.>}bp` is the same |
| 564 |
> dimension as `\texdimenbothbpmm{<dim. expr.>}mm`. |
| 565 |
|
| 566 |
### `\texdimenbothbpmmpt{<dim. expr.>}` |
| 567 |
|
| 568 |
> Produces a decimal (with up to five decimal places) `D` such that |
| 569 |
> `Dpt` is the largest dimension not exceeding the original one and |
| 570 |
> exactly representable both in the `bp` and `mm` units. |
| 571 |
|
| 572 |
### `\texdimenbothmmbppt{<dim. expr.>}` |
| 573 |
|
| 574 |
> Alias for `\texdimenbothbpmmpt`. |
| 575 |
|
| 576 |
### `\texdimenbothbpmmsp{<dim. expr.>}` |
| 577 |
|
| 578 |
> Produces an integer (explicit digit tokens) `N` such that `Nsp` |
| 579 |
> is the largest dimension not exceeding the original one and |
| 580 |
> exactly representable both in the `bp` and `mm` units. |
| 581 |
|
| 582 |
### `\texdimenbothmmbpsp{<dim. expr.>}` |
| 583 |
|
| 584 |
> Alias for `\texdimenbothbpmmsp`. |
| 585 |
|
| 586 |
### `\texdimenwithunit{<dim. expr. 1>}{<dim expr. 2>}` |
| 587 |
|
| 588 |
> Produces a decimal `D` such that `D\dimexpr <dim expr. 2>\relax` is |
| 589 |
> considered by TeX the same as `<dim. expr. 1>` if at all possible. If |
| 590 |
> the (assumed non zero) second argument `<dim2>` is at most `1pt` (in |
| 591 |
> absolute value), then this is always possible. If the second argument |
| 592 |
> `<dim2>` is `>1pt` then this is not always possible and the output `D` |
| 593 |
> will ensure for `D<dim2>` to be a closest match to the first argument |
| 594 |
> `dim1` either from above or below, but one does not know if the other |
| 595 |
> direction would have given a better or worst match. |
| 596 |
> |
| 597 |
> `\texdimenwithunit{<dim>}{1bp}` and `\texdimenbp{<dim>}` are not |
| 598 |
> the same: The former produces a decimal `D` such that `D\dimexpr 1bp\relax` |
| 599 |
> is represented internally as is `<dim>` if at all possible, |
| 600 |
> whereas the latter produces a decimal `D` such that `D bp` is the one |
| 601 |
> aiming at being the same as `<dim>`. Using `D\dimexpr 1bp\relax` implies |
| 602 |
> a conversion factor equal to `65781/65536`, whereas `D bp` involves |
| 603 |
> the `803/800` conversion factor. |
| 604 |
> |
| 605 |
> `\texdimenwithunit{D1pt}{D2pt}` output is close to the mathematical |
| 606 |
> ratio `D1/D2`. But notwithstanding the various unavoidable "errors" |
| 607 |
> arising from conversion of decimal inputs to binary internals, and |
| 608 |
> from the latter to the former, the output `R` will tend to be |
| 609 |
> on average slightly larger (in its last decimal) than mathematical |
| 610 |
> `D1/D2`. The root cause being that the specification for `R` is that |
| 611 |
> `R<D2pt>` must be exactly `<D1pt>` after TeX parsing, if at all |
| 612 |
> possible; and it turns out this is always possible for `D2pt<1pt`. The |
| 613 |
> final step in the TeX parsing of a multiplication of a dimension by a |
| 614 |
> scalar is a *truncation* to an integer multiple of the `sp=1/65536pt` |
| 615 |
> unit, not a rounding. So `R` is basically (i.e. before conversion to a |
| 616 |
> decimal) `ceil(D1/D2,16)`, or to be more precise it is obtained as |
| 617 |
> `ceil(N1/N2,16)` with `D1pt->N1sp`, `D2pt->N2sp` and the second |
| 618 |
> argument of `ceil` means that `16` binary places are used. This |
| 619 |
> formula is the one used for `D2pt<1pt`, for `D2pt>1pt` the mathematics |
| 620 |
> is different, but the implication that `R` has a (less significant) |
| 621 |
> bias to be "shifted upwards" (in its last decimal place) compared to |
| 622 |
> the (rounded) value `D1/D2` or rather `N1/N2` still stands. |
| 623 |
|
| 624 |
## Change log |
| 625 |
|
| 626 |
### [1.1 (2021/11/17)](https://github.com/jfbu/texdimens/compare/1.0...1.1#files_bucket) |
| 627 |
|
| 628 |
- internal refactorings across the entire code base aiming at (small) |
| 629 |
efficiency gains from optimized TeX token manipulations |
| 630 |
- in particular, the algorithm for `\texdimenwithunit{<dim1>}{<dim2>}` |
| 631 |
in the "`dim2<1pt`" branch got modified (output unchanged) |
| 632 |
- all macros now f-expandable (this was already the case at `1.0` except |
| 633 |
for `\texdimenwithunit` with arguments of opposite signs, the |
| 634 |
second one not exceeding `1pt` in absolute value) |
| 635 |
- the `\expanded` primitive is required (present in all engines since |
| 636 |
TeXLive 2019) |
| 637 |
- the usual batch of documentation additions or fix-ups, also in |
| 638 |
code comments (fix in particular issues #21, #22) |
| 639 |
- addition of this Change log to the pdf documentation |
| 640 |
- addition of the highlighted commented source code to the pdf documentation |
| 641 |
|
| 642 |
### [1.0 (2021/11/10)](https://github.com/jfbu/texdimens/compare/0.99d...1.0#files_bucket) |
| 643 |
|
| 644 |
- new: `\texdimenbothbpmm` and relatives (feature request #10) |
| 645 |
- breaking: `\texdimenwithunit` output for second argument `<1pt` still |
| 646 |
obeys specs but is closer to mathematical ratio (feature request #16) |
| 647 |
- enhanced: all `up/down` macros (i.e. also for the `dd`, `nc`, `in` |
| 648 |
units) accept the full range of dimensions (feature request #18) |
| 649 |
- enhanced: `\texdimenwithunit`'s second argument is now allowed |
| 650 |
to be negative (feature request #13) |
| 651 |
|
| 652 |
### [0.99a-d (2021/11/04)](https://github.com/jfbu/texdimens/compare/0.99...0.99d#files_bucket) |
| 653 |
|
| 654 |
- documentation in TeX/LaTeX installations available in pdf format |
| 655 |
- the usual batch of documentation additions or fix-ups |
| 656 |
- let the CTAN `README.md` be much shortened, and provide `texdimens.md` |
| 657 |
as the one matching the repo `README.md` |
| 658 |
- fix bugs of `\texdimenwithunit{<dim1>}{<dime2>}` for `dim1=0pt` or |
| 659 |
`dim2=1pt` (#3, #4, #6, #8) |
| 660 |
|
| 661 |
### [0.99 (2021/11/02)](https://github.com/jfbu/texdimens/compare/0.9...0.99#files_bucket) |
| 662 |
|
| 663 |
- new: `\texdimenwithunit{<dim1>}{<dim2>}` (feature request #2) |
| 664 |
|
| 665 |
### [0.9 (2021/07/21)](https://github.com/jfbu/texdimens/compare/0.9delta...0.9#files_bucket) |
| 666 |
|
| 667 |
- new: `\texdimenbothincm` and relatives |
| 668 |
- breaking: use `\texdimen` prefix for all macros |
| 669 |
|
| 670 |
### [0.9delta (2021/07/15)](https://github.com/jfbu/texdimens/compare/0.9gamma...0.9delta#files_bucket) |
| 671 |
|
| 672 |
- internal refactorings |
| 673 |
|
| 674 |
### [0.9gamma (2021/07/14)](https://github.com/jfbu/texdimens/compare/0.9beta...0.9gamma#files_bucket) |
| 675 |
|
| 676 |
- new: `\texdiminbpdown` (now `\texdimenbpdown`), `\texdiminbpup` |
| 677 |
(now `\texdimenbpup`) and similar named macros associated with |
| 678 |
the other units |
| 679 |
|
| 680 |
### [0.9beta (2021/06/30)](https://github.com/jfbu/texdimens/compare/54f4eb13...0.9beta#files_bucket) |
| 681 |
|
| 682 |
- initial release: provides `\texdiminbp` (now `\texdimenbp`) and |
| 683 |
similar named macros for the units `nd`, `dd`, `mm`, `pc`, `nc`, `cc`, |
| 684 |
`cm`, `in` |
| 685 |
|
| 686 |
|
| 687 |
## Acknowledgements |
| 688 |
|
| 689 |
Thanks to Denis Bitouzé for raising an |
| 690 |
[issue](https://github.com/latex3/latex3/issues/953) |
| 691 |
on the LaTeX3 tracker which became the initial stimulus for this package. |
| 692 |
|
| 693 |
Thanks to Ruixi Zhang for reviving the above linked-to thread and |
| 694 |
opening up on the package issue tracker the |
| 695 |
[issue #2](https://github.com/jfbu/texdimens/issues/2) asking to add |
| 696 |
handling of the `ex` and `em` cases. This was done at release `0.99` via |
| 697 |
the addition of `\texdimenwithunit`. |
| 698 |
|
| 699 |
Renewed thanks to Ruixi Zhang for analyzing at |
| 700 |
[issue #10](https://github.com/jfbu/texdimens/issues/10) what is at |
| 701 |
stake into finding dimensions exactly representable both in the `bp` and |
| 702 |
`mm` units. Macros `\texdimenbothbpmm` and `\texdimenbothmmbp` now |
| 703 |
address this (release `1.0`). |
| 704 |
|
| 705 |
<!-- |
| 706 |
--> |