Skip to content

Commit

Permalink
Use siunitx multi-uncertainty support
Browse files Browse the repository at this point in the history
This fixes #31
  • Loading branch information
Splines committed Apr 12, 2024
1 parent 6a84259 commit 89b9eca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
28 changes: 0 additions & 28 deletions src/api/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ def _export(filepath: str, print_completed: bool):
result_str = latexer.result_to_latex_cmd(result)
result_lines.append(result_str)

if not c.configuration.siunitx_fallback:
siunitx_setup = _uncertainty_names_to_siunitx_setup(uncertainty_names)
if siunitx_setup != "":
lines.append("% Commands to correctly print the uncertainties in siunitx:")
lines.append(siunitx_setup)
lines.append("")

lines.append("% Commands to print the results. Use them in your document.")
lines.extend(result_lines)

Expand All @@ -56,24 +49,3 @@ def _export(filepath: str, print_completed: bool):
f.write("\n".join(lines))
if print_completed:
print(f'Exported to "{filepath}"')


def _uncertainty_names_to_siunitx_setup(uncert_names: Set[str]) -> str:
"""
Returns the preamble for the LaTeX document to use the siunitx package.
"""
if len(uncert_names) == 0:
return ""

cmd_names = []
cmds = []
for name in uncert_names:
cmd_name = f"\\Uncert{Helpers.capitalize(name)}"
cmd_names.append(cmd_name)
cmds.append(rf"\NewDocumentCommand{{{cmd_name}}}{{}}{{_{{\text{{{name}}}}}}}")

string = "\n".join(cmds)
string += "\n"
string += rf"\sisetup{{input-digits=0123456789{''.join(cmd_names)}}}"

return string
15 changes: 11 additions & 4 deletions src/application/latex_better_siunitx_stringifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from application.helpers import Helpers
from application.stringifier import Stringifier
from domain.uncertainty import Uncertainty


class LatexBetterSiunitxStringifier(Stringifier):
Expand All @@ -22,7 +23,7 @@ class LatexBetterSiunitxStringifier(Stringifier):
value_prefix = ""
value_suffix = ""

error_name_prefix = r"\Uncert"
error_name_prefix = ""
error_name_suffix = ""

scientific_notation_prefix = "e"
Expand All @@ -33,13 +34,14 @@ class LatexBetterSiunitxStringifier(Stringifier):
# pylint: enable=duplicate-code

def _modify_uncertainty_name(self, name) -> str:
return Helpers.capitalize(name)
return ""

# pylint: disable-next=too-many-arguments
def _assemble_str_parts(
self,
sign: str,
value_rounded: str,
uncertainties: List[Uncertainty],
uncertainties_rounded: List[str],
should_use_parentheses: bool,
use_scientific_notation: bool,
Expand All @@ -51,9 +53,14 @@ def _assemble_str_parts(
if use_scientific_notation:
num_part += f" e{str(exponent)}"

uncert_descriptors = [u.name for u in uncertainties if u.name != ""]
uncert_descriptors_str = ""
if len(uncert_descriptors) > 0:
uncert_descriptors_str = f"[uncertainty-descriptors={{{','.join(uncert_descriptors)}}}]"

if unit != "":
string = rf"\qty{{{num_part}}}{{{unit}}}"
string = rf"\qty{uncert_descriptors_str}{{{num_part}}}{{{unit}}}"
else:
string = rf"\num{{{num_part}}}"
string = rf"\num{uncert_descriptors_str}{{{num_part}}}"

return string
2 changes: 2 additions & 0 deletions src/application/stringifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def create_str(self, value: Value, uncertainties: List[Uncertainty], unit: str)
return self._assemble_str_parts(
sign,
value_rounded,
uncertainties,
uncertainties_rounded,
should_use_parentheses,
use_scientific_notation,
Expand All @@ -89,6 +90,7 @@ def _assemble_str_parts(
self,
sign: str,
value_rounded: str,
uncertainties: List[Uncertainty],
uncertainties_rounded: List[str],
should_use_parentheses: bool,
use_scientific_notation: bool,
Expand Down

0 comments on commit 89b9eca

Please sign in to comment.