JacquesCarette / Drasil

Generate all the things (focusing on research software)
https://jacquescarette.github.io/Drasil
BSD 2-Clause "Simplified" License
142 stars 26 forks source link

Percent (%) symbol is being rendered in italics in the HTML SRS #3748

Open BilalM04 opened 5 months ago

BilalM04 commented 5 months ago

The percent (%) symbol is being rendered in italics in the HTML SRS, when it should not be.

HTML: % is wrapped in an <em></em> tag. For example: https://github.com/JacquesCarette/Drasil/blob/21ff3588595f34c3135af6e6db538e16d5318767/code/stable/dblpend/SRS/HTML/DblPend_SRS.html#L2374

From briefly looking at the Drasil code, it seems that % is defined as an operator, thus it is printed as an expression.

smiths commented 5 months ago

Is % implemented as the modulus operator?

BilalM04 commented 5 months ago

Looking at the following code:

https://github.com/JacquesCarette/Drasil/blob/21ff3588595f34c3135af6e6db538e16d5318767/code/drasil-printers/lib/Language/Drasil/Printing/AST.hs#L9-L16

There doesn't seem to be an explicit modulus operator, just percent (labeled Perc). Thus, I'd assume in Drasil both percent and modulus are implemented together as Perc.

JacquesCarette commented 5 months ago

It seems weird to me that Perc is an operator. I would want to trace it some more (i.e. both upstream, as in who generates this Perc and downstream, i.e. how is it actually handled by the printers themselves.)

balacij commented 4 months ago

I would imagine that a % should be rendered in the same slice of code we use to render units -- @BilalM04 do you think that is possible? Do we think this is sensible?

BilalM04 commented 4 months ago

To my knowledge, units are rendered as expressions; thus, it would still be italicized. All of the printers wrap expressions in some sort or italics or in-line equation wrappers. To add on to the original issue, I investigated the problem further.

For printing, the Perc operator is wrapped in an Expr, which is where the italicization begins:

https://github.com/JacquesCarette/Drasil/blob/864e1385e94f900d8035f605a0b170b9c439dc61/code/drasil-printers/lib/Language/Drasil/Printing/Import/Sentence.hs#L30

Here is the Expr data type, which contains Ops:

https://github.com/JacquesCarette/Drasil/blob/864e1385e94f900d8035f605a0b170b9c439dc61/code/drasil-printers/lib/Language/Drasil/Printing/AST.hs#L28-L46

This Expr is then wrapped in a Spec:

https://github.com/JacquesCarette/Drasil/blob/864e1385e94f900d8035f605a0b170b9c439dc61/code/drasil-printers/lib/Language/Drasil/Printing/AST.hs#L50-L58

Thus, when printing, since the top level data type is a Spec then an Expr, the contents are then italicized.


Two possible solutions I thought of were:

1) Render Percent as a String. This means it would no longer be an Expr.

smiths commented 4 months ago

@BilalM04 you got my attention with "units are rendered as expressions; thus, it would still be italicized". The problem you are discussing is actually deeper than just the percent symbol. I wonder if this is a chance to fix our rendering of units in Drasil? According to SI rules, units should NOT be in italics. This rule can be seen in the NIST checklist item 6. Some of the units in Drasil are not in italics, while others are. For instance, in the double pendulum example, $a_{x1}$ has the correct font, but $F$ does not. It seems like the units that require division are correct, but the straight units are not.

We should stop rendering units in italics. I think that is the bigger picture problem to try to solve. If we stop using italics, then it fixes the percentage symbol, and everything else that is currently wrong. :smile:

If we can fix this, we should add it to our list of successes in Drasil. (When we do this we should also fix the spelling of cosmetic in the current example benefit.) I grade many SRS documents in my grad course every year and students struggle with the rule that units are not in italics. It is just too fiddly a detail for must people to keep track of consistently. We are currently doing the thing where it is wrong everywhere, but when we fix it, it will be right everywhere. :smile:

BilalM04 commented 4 months ago

@smiths and I discussed the rendering of units in italics in meeting #3791, I will reiterate the discussion here.

The reason % and units are being rendered in italics is due to the way HTML prints expressions. Due to previous issues on Mathjax being slow to render, the smaller expressions in HTML use plain HTML syntax rather than LaTeX. As a result, certain units and smaller expressions look visibly different, and are also in italics.

For certain expressions, it uses straight HTML, otherwise, it delegates to the TeX functions. Here is the code for rendering expressions in HTML:

https://github.com/JacquesCarette/Drasil/blob/864e1385e94f900d8035f605a0b170b9c439dc61/code/drasil-printers/lib/Language/Drasil/HTML/Print.hs#L158-L178

Additionally, all expressions in HTML are wrapped in an <em></em> tag here:

https://github.com/JacquesCarette/Drasil/blob/864e1385e94f900d8035f605a0b170b9c439dc61/code/drasil-printers/lib/Language/Drasil/HTML/Print.hs#L131

This issue in HTML can be solved by using TeX for all expressions and removing the <em> wrapper. Solely removing the <em> wrapper (which I tested) does not work as Drasil has no way of differentiang regular variables, which should be italicized, with units, which should not. @JacquesCarette is using TeX for all expressions in HTML viable, or are the speed differences too noticeable for us to use TeX?

Now, regarding the issue for LaTeX/PDF. The problem with italicization is actually not present in the LaTeX/PDF SRS. After comparing the rendered % in the SRS, with an italic % I manually wrote, I found the % symbol is not being generated in italics after all. Additionally, units are wrapped in \text{} rendering it as normal. I would like to make a correction in the original post, that percent symbol is not italics in the LaTeX version. This issue is only present in the HTML.

smiths commented 4 months ago

Thank you for the summary @BilalM04. We talked about doing an experiment to see the performance hit for rending all units inside \text{} for the html version. If the performance hit is reasonable, then we can fix the fact that units are displayed with the wrong font in the html documents. This won't fix the percentage symbol, but it will be an improvement.

BilalM04 commented 4 months ago

@smiths, using LaTeX for all expressions will actually fix the percentage issue as well. Please see the last paragraph of https://github.com/JacquesCarette/Drasil/issues/3748#issuecomment-2176261047.

To add on, here is the comparison:

image

The symbol on the left is $\%$, whereas the symbol on the right is \emph{\%}. Drasil renders the percent symbol as seen on the left hand side, which is the desired result; therefore, it is rendered correctly using LaTeX.

BilalM04 commented 4 months ago

I conducted an experiment to evaluate the performance between the existing HTML SRS and an alternative HTML SRS, where all expressions are rendered using Mathjax LaTeX. The tests were executed locally on the Brave browser (Chromium based), on a laptop operating on battery power (unplugged). The rendering of assets like images were excluded from the local tests (as they were not in the path), which resulted in quicker performance times than those anticipated in a hosted environment. Additionally, I have a decently powerful processor (Intel® Core™ i7-13700H), which further influenced the test results. I think caching is disabled in these tests.

NOTE: tests are run on the Projectile SRS.

Current Generated HTML SRS (LOCAL)

html_exprs_performance_summary

Full TeX Expressions HTML SRS (LOCAL)

full_TeX_performance_summary

Current Generated HTML SRS (HOSTED)

For reference, here is the performance stats for the hosted HTML SRS, which currently uses a mix of HTML and Mathjax for expressions. It is the same SRS as the first test, however it is the one hosted on the website rather than run locally.

hosted_srs

JacquesCarette commented 4 months ago

Fantastic data! All of these numbers are kind of large, i.e. more than 1s. Adding 300ms between current and full TeX is noticeable.

I'm wondering why the Scripting time is so large -- is it all MathJAX or do we have more scripting?

balacij commented 4 months ago

Some of my thoughts:

smiths commented 4 months ago

This is a really impressive analysis. It is great that it is based on data and the @balacij has theories that we can explore.

My personal preference is to not get distracted too much by this at this time. I think there are other issues that are more important at the moment.

Having said that, we could use this as a case study to highlight the benefits of Drasil. I'm fairly sure that if we improve the loading time of one webpage, we'll improve the loading time of all of them. Explaining the process of making this improvement would give us another anecdote to promote Drasil.