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

Use siunitx multi uncertainty descriptors #46

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions src/api/export.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from typing import Set
from api.latexer import get_latexer
from api.res import _res_cache
import api.config as c
from application.helpers import Helpers


def export(filepath: str):
Expand Down Expand Up @@ -41,13 +38,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 +46,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
20 changes: 13 additions & 7 deletions src/application/latex_better_siunitx_stringifier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List

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


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

uncertainty_name_prefix = r"\Uncert"
uncertainty_name_prefix = ""
uncertainty_name_suffix = ""

scientific_notation_prefix = "e"
Expand All @@ -32,16 +32,17 @@ class LatexBetterSiunitxStringifier(Stringifier):
unit_suffix = ""
# pylint: enable=duplicate-code

def _modify_uncertainty_name(self, name) -> str:
return Helpers.capitalize(name)
def _modify_uncertainty_name(self, _name) -> str:
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,
_should_use_parentheses: bool,
use_scientific_notation: bool,
exponent: int,
unit: str,
Expand All @@ -51,9 +52,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