-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
src/sciform/formatter.py
Outdated
def _repr_html_(self: FormattedNumber) -> str: | ||
return sciform_to_html(self) | ||
|
||
def _repr_latex_(self: FormattedNumber) -> str: | ||
return sciform_to_latex(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is perfect.
…the other way around)
Inspired by #114.
Introduces the
FormattedNumber
object which is output by theFormatter
. TheFormattedNumber
object inherits fromstr
and behaves like the usual output of theFormatter
. ButFormattedNumber
has_repr_latex_
and_repr_html_
which convert thesciform
output string into latex or html compatible commands. The_repr_latex_
and_repr_html_
methods are hooks forIPython
output display. See IPython Rich Display.FormattedNumber
also exposes the publicas_latex()
andas_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 newsciform_to_latex
andsciform_to_html
functions. Thesciform_to_latex
function streamlines the conversion of formatted numbers to latex compared to the oldlatex=
option to theFormatter
.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 supportlatex
conversion during formatting.Finally, the latex output by
sciform_to_latex
is a little different than the latex that used to be output using thelatex=
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 andk
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}
.