Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormattedNumber and post Latex and HTML conversion #134

Merged
merged 38 commits into from
Jan 27, 2024

Conversation

jagerber48
Copy link
Owner

@jagerber48 jagerber48 commented Jan 22, 2024

Inspired by #114.

Introduces the FormattedNumber object which is output by the Formatter. The FormattedNumber object inherits from str and behaves like the usual output of the Formatter. But FormattedNumber has _repr_latex_ and _repr_html_ which convert the sciform output string into latex or html compatible commands. The _repr_latex_ and _repr_html_ methods are hooks for IPython output display. See IPython Rich Display.

FormattedNumber also exposes the public as_latex() and as_html() methods to provide (public) access within python to the latex and html representations of the formatted number.

_repr_latex_ and _repr_html_ are implemented using the new sciform_to_latex and sciform_to_html functions. The sciform_to_latex function streamlines the conversion of formatted numbers to latex compared to the old latex= option to the Formatter.

The latex= option in the formatter has been removed. This has the advantage of (1) reducing the long list of formatting options available and (2) simplifying the formatting algorithm which had incurred some tricky edge case handling due to needing to support latex conversion during formatting.

Finally, the latex output by sciform_to_latex is a little different than the latex that used to be output using the latex= option. Previously (inspired by uncertainties) parentheses had been replaced with \left(...\right). The \left( and \right) delimiters auto-scale the vertical size of the delimiters in latex, so they are very convenient for formulas that might have tall elements like vertical fractions, integrals, etc. However, they have the downside that they incur horizontal space on the outsides of the delimiters. See spacing-around-left-and-right. This spacing caused complications when adding exponent strings outside of the enclosing parentheses. In ASCII mode a space was needed, e.g. (1.0 ± 0.1) k But when this was converted to latex via, e.g. \left(1.0\pm0.1\right)\text{ k} the space between the right parentheses and k was too large because of the extra space due to the \left(...\right) construct.

Since all sciform outputs are always single lines the vertical spacing benefits from \left(...\right) are not necessary. Going forward, these issues have been nicely handled by (1) dropping the \left(...\right) delimiters and just using regular parentheses (2) converting all spaces to explicity \: spaces in latex, and only wrapping text in \text{...} (no spaces). This leaves us with the unambiguous (1.0\:\pm\:0.1)\:\text{k}.

Copy link

codecov bot commented Jan 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c2f0f13) 100.00% compared to head (c155d5a) 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #134    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           23        25     +2     
  Lines         1086      1212   +126     
==========================================
+ Hits          1086      1212   +126     
Flag Coverage Δ
3.10 100.00% <100.00%> (ø)
3.11 100.00% <100.00%> (ø)
3.12 100.00% <100.00%> (ø)
3.9 99.91% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jagerber48 jagerber48 changed the title Feature/latex conversion FormattedNumber and post Latex and HTML conversion Jan 24, 2024
Copy link

@Batalex Batalex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure that converting symbols afterwards would be the best approach, but you proved me wrong.
This implementation adds value to the code base, and make it more maintainable IMO, nice job!

]

[tool.ruff.format]
docstring-code-format = true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I just learned about this option, I love it!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Batalex yes it's nice. I still use blackdoc to format code snippets in the .rst documentation files. Awaiting astral-sh/ruff#8237 so I can move entirely away from black and over to ruff.

Comment on lines 277 to 281
def _repr_html_(self: FormattedNumber) -> str:
return sciform_to_html(self)

def _repr_latex_(self: FormattedNumber) -> str:
return sciform_to_latex(self)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect.

@jagerber48 jagerber48 merged commit c788bb7 into main Jan 27, 2024
6 checks passed
@jagerber48 jagerber48 deleted the feature/latex_conversion branch January 27, 2024 13:06
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.

2 participants