Skip to content

pretty versions of some units in math mode#1437

Open
Alex-Jordan wants to merge 2 commits into
openwebwork:PG-2.21from
Alex-Jordan:prettyUnits
Open

pretty versions of some units in math mode#1437
Alex-Jordan wants to merge 2 commits into
openwebwork:PG-2.21from
Alex-Jordan:prettyUnits

Conversation

@Alex-Jordan

Copy link
Copy Markdown
Contributor

This sort of addresses #503, except by way of the newer contextUnits.pl. Each of the supported units from Units.pm that has a special unicode character will now print that way, if the unit is being used in math mode. Test with the problem below. It all looks fine in HTML. In hardcopy, we must be careful that the default font we use has glyphs for these characters. This is why two characters are used for degrees Fahrenheit, but a single character can be used for degrees Celsius. Also if someone switched away from xelatex over to pdflatex, these won't work. I'm unsure if we support not using xelatex.

Test with:

DOCUMENT();
loadMacros(qw(PGstandard.pl PGML.pl contextUnits.pl));

Context("Units")->withUnitsFor('length', 'temperature', 'angles', 'time', 'force', 'electricity', 'radiation');

$thickness = Compute('70 um');
$boils = Compute('100 degC');
$freezes = Compute('32 degF');
$zero = Compute('0 degK');
$pi = Compute('180 deg');
$us = Compute('us');
$angstrom = Compute('angstrom');
$bacterium = Compute('0.01 uN');
$charge = Compute('1 uC');
$farad = Compute('1 uF');
$ohm = Compute('ohm');
$kohm = Compute('kohm');
$Mohm = Compute('Mohm');
$uSv = Compute('uSv');

BEGIN_PGML
The thickness of a human hair is about [$thickness], or [`[$thickness]`].  
Water boils at [$boils], or [`[$boils]`].  
Water freezes at [$freezes], or [`[$freezes]`].  
Absolute zero is [$zero], or [`[$zero]`].  
[`\pi`] is [$pi], or [`[$pi]`].  
A very short unit of time is a [$us], or [`[$us]`].  
A very short distance is an [$angstrom], or [`[$angstrom]`].  
A bacterium has weight [$bacterium], or [`[$bacterium]`].  
A very small charge is a [$charge], or [`[$charge]`].  
A very small unit of capacitance is a [$farad], or [`[$farad]`].  
[$ohm], [$kohm], [$Mohm], or [`[$ohm]`], [`[$kohm]`], [`[$Mohm]`]  
[$uSv] or [`[$uSv]`]
END_PGML

ENDDOCUMENT();

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

Now this also prints pretty versions of the same units outside of math mode.

Comment thread macros/contexts/contextUnits.pl Outdated
@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Small thing, but the space before the degree symbol seems a little out of place to me.

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

Small thing, but the space before the degree symbol seems a little out of place to me.

That is not a change, in the sense that currently you have 180 deg. This changes the deg to °. But the space in between the quantity and the unit is a global separator. Something could be done wherever that separator is used, to omit that for special units where spaces are not appropriate.

@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Looking at the mathjax speech text, some seems good, 0.01 micronewtons, and others not so much, micro s or cap A ring. Also I noticed that degrees F vs degrees C get different speech text. degree C just gets but degree F gets degrees cap F. Though this might be something MathJax should deal with better, just some things I noticed in my testing.

@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Small thing, but the space before the degree symbol seems a little out of place to me.

That is not a change, in the sense that currently you have 180 deg. This changes the deg to °. But the space in between the quantity and the unit is a global separator. Something could be done wherever that separator is used, to omit that for special units where spaces are not appropriate.

I wasn't staying it was a change, but to me 180 deg seems proper, but 180 ° doesn't, I would always write it as 180°. I'm okay with it as is, just some things I've noticed and unsure if we want to address or not.

@Alex-Jordan

Alex-Jordan commented Jun 13, 2026 via email

Copy link
Copy Markdown
Contributor Author

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

I have a question. If the unit is initially defined like Compute('10 microns') should that display as "10 microns" or "10 µm"? There are a few other units with word versions that we may or may not want to respect.

@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Agreed, I wasn't sure if there was a solution or something we could do to improve it, I was just pointing it out.

@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

I have a question. If the unit is initially defined like Compute('10 microns') should that display as "10 microns" or "10 µm"? There are a few other units with word versions that we may or may not want to respect.

Would having some flag that this can be configured be useful to authors? I sometimes like to write units as 10 seconds and other times as 10 s.

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

I added a commit that essentially creates a list of unit symbols for which there will be no separator. Not sure I implemented it in the best way, but see what you think.

10 seconds will be 10 seconds. That is not the kind of thing happening here. What is happening here is that certain units like 10 angstrom will now be 10 Å. But at the same time, this does not change 10 angstroms or 10 Angstrom. Those still comes out as 10 angstroms or 10 Angstrom. My thinking was to only do this kind of prettying on the one lowercase singular-case unit where there is conventionally a special character. Not to just always print the unit the pretty way. The case of micron is especially worth thinking about, where it's actually a new word for micrometer.

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

There are also weird things like Compute('10 ㏀'), which will be recognized. And will display using that one weird ㏀ character. But won't display at all in print because our default font has no glyph for that. If an author used ㏀, I would be inclined to let that stay as ㏀, not try to change it to the two-character kΩ. But this PR does change kohm to kΩ.

My point is, I am wary about just deciding at the distribution level, how to display all equivalent units (like how s is equivalent to seconds). I think it is safer to just do things like specifically display um as µm.

@Alex-Jordan

Copy link
Copy Markdown
Contributor Author

I have a feeling I'm not doing one or both of these things here (pretty printing certain units, eliminating certain space separators) the right way. If @dpvc has a chance, it would be good to see what you think, as the designer of this context.

@somiaj

somiaj commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

@Alex-Jordan I'll leave it to you to decide what is best, I haven't used units yet, so don't have any experience, just pointing out things I saw. I didn't realize that this only affected a specific case of the units.

@drgrice1

Copy link
Copy Markdown
Member

Small thing, but the space before the degree symbol seems a little out of place to me.

I have also been bothered by the space between the number and the degree symbol. It is something I have thought about fixing at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants