connorferster / handcalcs

Python library for converting Python calculations into rendered latex.
Apache License 2.0
5.59k stars 427 forks source link

How to remove whitespaces and gaps? #209

Closed srinidhi-br closed 3 months ago

srinidhi-br commented 3 months ago

How can I remove the whitespace and gaps in the equation while using "short"? See marked in red below. Thank you image

0av commented 3 months ago

Hello,

I have changed the definitions of format_calc_line and format_long_calc_line in handcalcs.py:

def format_calc_line(line: CalcLine, **config_options) -> CalcLine:
    latex_code = line.latex
    latex_code = latex_code.replace("=", "&=", 1)
    comment_space = ""
    comment = ""
    if line.comment:
        comment_space = "\\;"
        comment = format_strings(line.comment, comment=True)
    line.latex = f"{latex_code} {comment_space} {comment}\n"
    return line
def format_calc_line(line: NumericCalcLine, **config_options) -> NumericCalcLine:
    latex_code = line.latex
    latex_code = latex_code.replace("=", "&=", 1)
    comment_space = ""
    comment = ""
    if line.comment:
        comment_space = "\\;"
        comment = format_strings(line.comment, comment=True)
    line.latex = f"{latex_code} {comment_space} {comment}\n"
    return line
def format_long_calc_line(line: LongCalcLine, **config_options) -> LongCalcLine:
    """
    Return line with .latex attribute formatted with line breaks suitable
    for positioning within the "\aligned" latex environment.
    """
    latex_code = line.latex
    long_latex = latex_code.replace("=", "=\\\\&=", 2)
    long_latex = long_latex.replace("=\\\\&=", "&=", 1)
    line_break = ""
    comment_space = ""
    comment = ""
    if line.comment:
        comment_space = "\\;"
        comment = format_strings(line.comment, comment=True)
    line.latex = f"{long_latex} {comment_space} {comment}{line_break}"
    return line

Output: image

srinidhi-br commented 3 months ago

Thanks, this works