diff --git a/LaTeXDatax/datax.py b/LaTeXDatax/datax.py index d4c29fd..d35a22b 100644 --- a/LaTeXDatax/datax.py +++ b/LaTeXDatax/datax.py @@ -1,4 +1,6 @@ # datax.py +import sys + def printvariable(tag,variable,f): """ Prints variable named "tag" with value "variable" to an iostream "f", @@ -31,10 +33,14 @@ def printvariable(tag,variable,f): if isinstance(variable,str): print('\\pgfkeyssetvalue{/datax/%s}{%s}'%(tag,variable),file=f) return - else: - number = variable - unit = '' - form = '%.4g' + if 'pint' in sys.modules: # Is this entire block an illegal waste of resources? Maybe. Will it cause more overhead time than I have already spent seeking a better solution? No. + from pint import Quantity + if isinstance(variable, Quantity): + print('\\pgfkeyssetvalue{{/datax/{}}}{{{:Lx}}}'.format(tag,variable),file=f) # ignores format specification + return + number = variable + unit = '' + form = '%.4g' else: # variable is tuple if len(variable) == 3: number = variable[0] @@ -56,13 +62,15 @@ def printvariable(tag,variable,f): formatstring = '\\pgfkeyssetvalue{/datax/%s}{\\SI{'+form+'}{%s}}' print(formatstring%(tag,number,unit),file=f) -def printvariables(f,**variables): +def printvariables(filename,**variables): """ Do `printvariable` for each of the given keyword arguments. + + NB! filename is not a string, but an iostream. It is named thusly to minimize the number of names unavailable to the user. """ - print('% File auto-generated by LaTeXDatax.py. Will be overwritten.',file=f) + print('% File auto-generated by LaTeXDatax.py. Will be overwritten.',file=filename) for tag,variable in variables.items(): - printvariable(tag,variable,f) + printvariable(tag,variable,filename) def datax(filename="data.tex",**variables): """ diff --git a/README.md b/README.md index cf2978c..0854dd4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This is a python interface for [the datax LaTeX package [ctan]](https://ctan.org ## Installation ``` -pip install "LaTeXDatax" +pip install LaTeXDatax ``` ## Usage @@ -31,6 +31,15 @@ In the LaTeX document: \end{document} ``` +## Pint integration +```python +from pint import Quantity +from LaTeXDatax import datax +datax(filename = "data.tex", E = Quantity(24,"kg m / s^2")) +``` + +Works as you would hope, in large part because of how well [pint](https://pint.readthedocs.io/) implements siunitx strings. Hat off. + ## Looking for contributors I don't know python very well. This package works, but if you have ideas for how to improve it, or you spot some python faux pas, feel free to make a PR or get in touch! diff --git a/setup.py b/setup.py index bfe9c72..91449ba 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='LaTeXDatax', - version='1.1', + version='1.2', author='David Gustavsson', author_email='david.e.gustavsson@gmail.com', description='Export individual data from a Python script to a LaTeX document', diff --git a/tests/test_LaTeXDatax.py b/tests/test_LaTeXDatax.py index 9d291e6..4562656 100644 --- a/tests/test_LaTeXDatax.py +++ b/tests/test_LaTeXDatax.py @@ -1,6 +1,7 @@ import unittest, io from LaTeXDatax import printvariables +from pint import Quantity class TestDatax(unittest.TestCase): def test_datax(self): @@ -14,6 +15,7 @@ def test_datax(self): c = (3.141592,"\\meter"), d = (3.141592,"\\meter","%.2g"), e = (3.141592,"%.2g"), + f = Quantity(3.141592,"\\meter"), ) f.seek(0) written = f.read() @@ -24,6 +26,7 @@ def test_datax(self): \\pgfkeyssetvalue{/datax/c}{\\SI{3.142}{\\meter}} \\pgfkeyssetvalue{/datax/d}{\\SI{3.1}{\\meter}} \\pgfkeyssetvalue{/datax/e}{\\num{3.1}} +\\pgfkeyssetvalue{/datax/f}{\\SI[]{3.141592}{\\meter}} """ self.assertEqual(written,target)