Commit 28a1ca8a by Peter Baratta

Comment fixes

parent af1f8c1f
...@@ -5,7 +5,7 @@ That is, given a math string, parse it and render each branch of the result, ...@@ -5,7 +5,7 @@ That is, given a math string, parse it and render each branch of the result,
always returning valid latex. always returning valid latex.
Because intermediate values of the render contain more data than simply the Because intermediate values of the render contain more data than simply the
string of latex, store it in a custom class `LatexRendered` string of latex, store it in a custom class `LatexRendered`.
""" """
from calc import ParseAugmenter, add_defaults, SUFFIXES from calc import ParseAugmenter, add_defaults, SUFFIXES
...@@ -16,26 +16,26 @@ class LatexRendered(object): ...@@ -16,26 +16,26 @@ class LatexRendered(object):
Data structure to hold a typeset representation of some math. Data structure to hold a typeset representation of some math.
Fields: Fields:
-`latex` is a generated, valid latex string (as if it were standalone) -`latex` is a generated, valid latex string (as if it were standalone).
-`sans_parens` is usually the same as `latex` except without the outermost -`sans_parens` is usually the same as `latex` except without the outermost
parens (if applicable) parens (if applicable).
-`tall` is a boolean representing if the latex has any elements extending -`tall` is a boolean representing if the latex has any elements extending
above or below a normal height, specifically things of the form 'a^b' and above or below a normal height, specifically things of the form 'a^b' and
'\frac{a}{b}'. This affects the height of wrapping parenthesis. '\frac{a}{b}'. This affects the height of wrapping parenthesis.
""" """
def __init__(self, latex, parens=None, tall=False): def __init__(self, latex, parens=None, tall=False):
""" """
Instantiate with the latex representing the math Instantiate with the latex representing the math.
Optionally include parenthesis to wrap around it and the height. Optionally include parenthesis to wrap around it and the height.
`parens` must be one of '(', '[' or '{' `parens` must be one of '(', '[' or '{'.
`tall` is a boolean (see note above) `tall` is a boolean (see note above).
""" """
self.latex = latex self.latex = latex
self.sans_parens = latex self.sans_parens = latex
self.tall = tall self.tall = tall
# generate parens and overwrite self.latex # Generate parens and overwrite `self.latex`.
if parens is not None: if parens is not None:
left_parens = parens left_parens = parens
if left_parens == '{': if left_parens == '{':
...@@ -107,6 +107,7 @@ def function_closure(functions, casify): ...@@ -107,6 +107,7 @@ def function_closure(functions, casify):
if casify(fname) not in functions: if casify(fname) not in functions:
pass pass
# Wrap the input of the function with parens or braces.
inner = children[1].latex inner = children[1].latex
if fname == "sqrt": if fname == "sqrt":
inner = u"{{{expr}}}".format(expr=inner) inner = u"{{{expr}}}".format(expr=inner)
...@@ -116,6 +117,7 @@ def function_closure(functions, casify): ...@@ -116,6 +117,7 @@ def function_closure(functions, casify):
else: else:
inner = u"({expr})".format(expr=inner) inner = u"({expr})".format(expr=inner)
# Correctly format the name of the function.
if fname == "sqrt": if fname == "sqrt":
fname = ur"\sqrt" fname = ur"\sqrt"
elif fname == "log10": elif fname == "log10":
...@@ -125,8 +127,10 @@ def function_closure(functions, casify): ...@@ -125,8 +127,10 @@ def function_closure(functions, casify):
else: else:
fname = ur"\text{{{fname}}}".format(fname=fname) fname = ur"\text{{{fname}}}".format(fname=fname)
# Put it together.
latex = fname + inner latex = fname + inner
return LatexRendered(latex, tall=children[1].tall) return LatexRendered(latex, tall=children[1].tall)
# Return the function within the closure.
return render_function return render_function
...@@ -134,7 +138,8 @@ def render_power(children): ...@@ -134,7 +138,8 @@ def render_power(children):
""" """
Combine powers so that the latex is wrapped in curly braces correctly. Combine powers so that the latex is wrapped in curly braces correctly.
If you have 'a^(b+c)' don't include that last set of parens ('a^{b+c}'). Also, if you have 'a^(b+c)' don't include that last set of parens:
'a^{b+c}' is correct, whereas 'a^{(b+c)}' is extraneous.
""" """
if len(children) == 1: if len(children) == 1:
return children[0] return children[0]
...@@ -149,7 +154,7 @@ def render_power(children): ...@@ -149,7 +154,7 @@ def render_power(children):
def render_parallel(children): def render_parallel(children):
""" """
Simply combine elements with a double vertical line. Simply join the child nodes with a double vertical line.
""" """
children_latex = [k.latex for k in children if k.latex != "||"] children_latex = [k.latex for k in children if k.latex != "||"]
latex = r"\|".join(children_latex) latex = r"\|".join(children_latex)
...@@ -161,7 +166,7 @@ def render_frac(numerator, denominator): ...@@ -161,7 +166,7 @@ def render_frac(numerator, denominator):
r""" r"""
Given a list of elements in the numerator and denominator, return a '\frac' Given a list of elements in the numerator and denominator, return a '\frac'
Avoid parens if there is only thing in that part Avoid parens if they are unnecessary (i.e. the only thing in that part).
""" """
if len(numerator) == 1: if len(numerator) == 1:
num_latex = numerator[0].sans_parens num_latex = numerator[0].sans_parens
...@@ -181,9 +186,15 @@ def render_product(children): ...@@ -181,9 +186,15 @@ def render_product(children):
r""" r"""
Format products and division nicely. Format products and division nicely.
That is, group bunches of adjacent, equal operators. For every time it Group bunches of adjacent, equal operators. Every time it switches from
switches from numerator to denominator, call `render_frac`. Join these denominator to the next numerator, call `render_frac`. Join these groupings
groupings by '\cdot's. together with '\cdot's, ending on a numerator if needed.
Examples: (`children` is formed indirectly by the string on the left)
'a*b' -> 'a\cdot b'
'a/b' -> '\frac{a}{b}'
'a*b/c/d' -> '\frac{a\cdot b}{c\cdot d}'
'a/b*c/d*e' -> '\frac{a}{b}\cdot \frac{c}{d}\cdot e'
""" """
position = "numerator" # or denominator position = "numerator" # or denominator
fraction_mode_ever = False fraction_mode_ever = False
...@@ -194,29 +205,33 @@ def render_product(children): ...@@ -194,29 +205,33 @@ def render_product(children):
for kid in children: for kid in children:
if position == "numerator": if position == "numerator":
if kid.latex == "*": if kid.latex == "*":
pass pass # Don't explicitly add the '\cdot' yet.
elif kid.latex == "/": elif kid.latex == "/":
# Switch to denominator mode.
fraction_mode_ever = True fraction_mode_ever = True
position = "denominator" position = "denominator"
else: else:
numerator.append(kid) numerator.append(kid)
else: else:
if kid.latex == "*": if kid.latex == "*":
# render the current fraction and add it to the latex # Switch back to numerator mode.
# First, render the current fraction and add it to the latex.
latex += render_frac(numerator, denominator) + r"\cdot " latex += render_frac(numerator, denominator) + r"\cdot "
# reset back to beginning state # Reset back to beginning state
position = "numerator" position = "numerator"
numerator = [] numerator = []
denominator = [] denominator = []
elif kid.latex == "/": elif kid.latex == "/":
pass pass # Don't explicitly add a '\frac' yet.
else: else:
denominator.append(kid) denominator.append(kid)
# Add the fraction/numerator that we ended on.
if position == "denominator": if position == "denominator":
latex += render_frac(numerator, denominator) latex += render_frac(numerator, denominator)
else: else:
# We ended on a numerator--act like normal multiplication.
num_latex = r"\cdot ".join(k.latex for k in numerator) num_latex = r"\cdot ".join(k.latex for k in numerator)
latex += num_latex latex += num_latex
...@@ -226,7 +241,7 @@ def render_product(children): ...@@ -226,7 +241,7 @@ def render_product(children):
def render_sum(children): def render_sum(children):
""" """
Combine elements, including their operators. Concatenate elements, including the operators.
""" """
children_latex = [k.latex for k in children] children_latex = [k.latex for k in children]
latex = "".join(children_latex) latex = "".join(children_latex)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment