css technique for aligning decimals.ยค
all of my recent plotting techniques rely on access to cell values in css. when we have the values in css, along with their extent (min/max), we can adjust the values using math to align decimal points.
%%
our primary directive is calculate the order of magnitude of our values and their sign.
we only need the maximum number of digits and signs before the decimal;
we'll refer to these values with the prefix `--tens`
display\
```css
#aligned {
--tens-max: calc(
round(up, max(1, log(abs(var(--0-max)), 10)))
+ abs(min(0, sign(var(--0-max))))
);
--tens-min: calc(
round(up, max(1, log(abs(var(--0-min)), 10)))
+ abs(min(0, sign(var(--0-min))))
);
--tens-total: calc(max(var(--tens-max), var(--tens-min)));
}
```
this technique should work for any monospace sized font. we added background grids to verify alignment.
display\
```css
#aligned {
font-size: 36px;
font-family: monospace;
background: linear-gradient(90deg, transparent 98%, gray 2%);
background-size: 0.25em 1em;
tr {
background: unset;
}
}
```
for each cell value we use the `::before` element to insert a single character width for
each `--tens` unit. if padding is not removed then we'll end up with extra left whitespace.s
display\
```css
#aligned td {
text-align: left;
&::before {
display: inline-block;
content: "";
--tens: calc(
round(up, max(1, log(abs(var(--0)), 10)))
+ abs(min(0, sign(var(--0))))
);
width: calc(1ch * (var(--tens-total) - var(--tens)));
padding: 0;
}
}
```
import nbconvert_a11y.table
(df := DataFrame(
numpy.random.randn(30, 1)
).pow(9))
some values of `-0.000` dont fit the pattern because the are registered to css as zeros and are NOT negative.
this needs to amended in the table renderer.
df.table(id="aligned")