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

pint and siunitx #113

Open
jtplaarj opened this issue Jan 24, 2022 · 4 comments
Open

pint and siunitx #113

jtplaarj opened this issue Jan 24, 2022 · 4 comments
Assignees
Labels
enhancement New feature or request

Comments

@jtplaarj
Copy link

I am trying to use handcals with pint and siunitx.

The main problem is that in latex_repr it checks if the object has a _repr_latex_. Pint has that attribute but it is standard LaTeX, not siunitx. For that str()or printshould be used.
A minimal example:

from handcalcs.decorator import handcalc
from pint import UnitRegistry

ureg = UnitRegistry(auto_reduce_dimensions=True)
ureg.default_format = "Lx"


@handcalc()
def test_hand(a: str, b: str) -> str:
    c = a / b
    return c

r = test_hand(3.0 * ureg.meter, 5.0 * ureg.second ** 2)

print(r[0])
print(r[1])

The result is:

\begin{aligned}
c &= \frac{ a }{ b }  = \frac{ 3.0\ \mathrm{meter} }{ 5.0\ \mathrm{second}^{2} } &= 0.6\ \frac{\mathrm{meter}}{\mathrm{second}^{2}}  
\end{aligned}

\SI[]{0.6}{\meter\per\second\squared}

As you can see the result is using siunitx but the latex representation by handcalc, its not.

One solution would be to detect a pint object and use the str() method if default format is set to Lx, otherwise _repr_latex.

Is there anything I am missing to work around this question?

@connorferster
Copy link
Owner

connorferster commented Jan 24, 2022

For clarity, are you saying that the output of repr_latex() on pint objects outputs a Latex str that includes commands for siunitx?

@jtplaarj
Copy link
Author

No, the repr.latex is plain latex text. Since no str or print are invoked, it will never output siunitx.

@connorferster
Copy link
Owner

Sorry for the delayed response. I am prioritizing working better with pint. siunitx is not utilized at all in either MathJax or Katex (although it is an asked-for feature) so this would only be useful when outputting text to a .tex file for external rendering which I consider to be a secondary use for handcalcs. The tex output from handcalcs is intended to work across all latex rendering environments and using the siunitx code would be limited to a latex environment with the siunitx package installed.

So, while I will be focusing on having better interaction with pint, I don't know if I will have a flag or trigger for outputting the siunitx tex code since it is such a special use case.

@connorferster
Copy link
Owner

This will now work in the upcoming version by specifying the preferred string formatter in the global config. Default setting is "L" but you can specify "Lx" if you are working with pint objects and want to use the "Lx" formatter.

@connorferster connorferster self-assigned this Jul 4, 2022
@connorferster connorferster added the enhancement New feature or request label Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants