Skip to content
Snippets Groups Projects
Commit 1f88045d authored by Vanessa Stehr's avatar Vanessa Stehr
Browse files

Parse array into latex

parent 55a27f15
No related branches found
No related tags found
No related merge requests found
...@@ -12,8 +12,46 @@ def toLatex(dsiString: str): ...@@ -12,8 +12,46 @@ def toLatex(dsiString: str):
""" """
tree = _parseDsi(dsiString) tree = _parseDsi(dsiString)
latexArray = [] latexArray = []
match len(tree):
case 1: # no fractions
for node in tree[0]:
latexArray.append(_nodeToLatex(node))
case 2: # one fraction
latexArray.append(r'\frac')
for frac in tree:
latexArray.append(r'{')
for node in frac:
latexArray.append(_nodeToLatex(node))
latexArray.append(r'}')
case 3: # more than one fraction
for i in range(len(tree)):
if i > 0:
latexArray.append(r'{\color{red}/}')
for node in tree[i]:
latexArray.append(_nodeToLatex(node))
return ''.join(latexArray) return ''.join(latexArray)
def _nodeToLatex(node: list):
"""generates a latex string from a node with three elements: prefix, unit, power
Args:
node (list): node with three elements: prefix, unit, power
Returns:
str: latex representation
"""
latexString = ""
if node[0]:
latexString += _dsiPrefixesLatex[node[0]]
try:
latexString += _dsiUnitsLatex[node[1]]
except KeyError:
latexString += r'{\color{red}\mathrm{'+node[1]+r'}}'
if node[2]:
latexString += r'^' + node[2]
return latexString
def _parseDsi(dsiString: str): def _parseDsi(dsiString: str):
"""parses a D-SI string into a list of lists: """parses a D-SI string into a list of lists:
List format: List format:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment