Skip to content

latex_formula

codes.latex_formula

Latex formula representation.

Classes:

codes.latex_formula.LatexFormula dataclass

LatexFormula(
    return_symbol: str,
    result: str,
    intermediate_result: str = "",
    equation: str = "",
    numeric_equation: str = "",
    numeric_equation_with_units: str = "",
    comparison_operator_label: str = "=",
    unit: str = "",
)

Latex formula representation.

Depending on the context this could include the unit, the formula, the result, etc.

Attributes:

  • return_symbol (str) –

    The symbol to return

  • result (str) –

    The result of the formula

  • intermediate_result (str, default "") –

    An intermediate result of the formula

  • equation (str, default "") –

    The formula with symbols

  • numeric_equation (str, default "") –

    The formula with values (numbers)

  • numeric_equation_with_units (str, default "") –

    The formula with values (numbers) and units

  • comparison_operator_label (str, default "=") –

    The label for the comparison operators between the return symbol and the result. Could be changed for inequalities.

  • unit (str, default "") –

    The unit of the result

codes.latex_formula.LatexFormula.complete property

complete: str

Complete representation of the formula.

Returns:

  • str

    Return symbol = equation = numeric_equation = intermediate_result = result

codes.latex_formula.LatexFormula.complete_with_units property

complete_with_units: str

Complete representation of the formula with units.

Returns:

  • str

    Return symbol = equation = numeric_equation_with_units = intermediate_result = result

codes.latex_formula.LatexFormula.short property

short: str

Minimal representation of the formula.

Returns:

  • str

    Return symbol = result

codes.latex_formula.latex_fraction

latex_fraction(numerator: str | float, denominator: str | float) -> str

Return a string which will output: \frac{numerator}{denominator} in latex.

Examples:

>>> latex_fraction(1, 2)
str(\frac{1}{2})

Parameters:

  • numerator (str | float) –

    The numerator of the fraction.

  • denominator (str | float) –

    The denominator of the fraction.

Returns:

  • str

    The latex string

Source code in blueprints/codes/latex_formula.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def latex_fraction(numerator: str | float, denominator: str | float) -> str:
    r"""Return a string which will output: \frac{numerator}{denominator} in latex.

    Examples
    --------
    >>> latex_fraction(1, 2)
    str(\frac{1}{2})

    Parameters
    ----------
    numerator: str | float
        The numerator of the fraction.
    denominator: str | float
        The denominator of the fraction.

    Returns
    -------
    str
        The latex string

    """
    return f"\\frac{{{numerator}}}{{{denominator}}}"

codes.latex_formula.latex_max_curly_brackets

latex_max_curly_brackets(*args: str | float) -> str

Return a string which will output: max{arg_1; arg_2; ...; arg_N} in latex.

It will also automatically ensure floats are converted to latex text.

Examples:

>>> latex_max_curly_brackets(1, 2)
str(\max \left\{1; 2\right\})

Parameters:

  • args (str | float, default: () ) –

    The arguments of the max function.

Returns:

  • str

    The latex representation of the max function.

Source code in blueprints/codes/latex_formula.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def latex_max_curly_brackets(*args: str | float) -> str:
    r"""Return a string which will output: max{arg_1; arg_2; ...; arg_N} in latex.

    It will also automatically ensure floats are converted to latex
    text.

    Examples
    --------
    >>> latex_max_curly_brackets(1, 2)
    str(\max \left\{1; 2\right\})

    Parameters
    ----------
    args: str
        The arguments of the max function.

    Returns
    -------
    str
        The latex representation of the max function.
    """
    arguments = [str(arg) for arg in args]
    return f"\\max \\left\\{{{'; '.join(arguments)}\\right\\}}"

codes.latex_formula.latex_min_curly_brackets

latex_min_curly_brackets(*args: str | float) -> str

Return a string which will output: min{arg_1; arg_2; ...; arg_N} in latex.

It will also automatically ensure floats are converted to latex text.

Examples:

>>> latex_min_curly_brackets(1, 2)
str(\min \left\{1; 2\right\})

Parameters:

  • args (str | float, default: () ) –

    The arguments of the min function.

Returns:

  • str

    The latex representation of the min function.

Source code in blueprints/codes/latex_formula.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
def latex_min_curly_brackets(*args: str | float) -> str:
    r"""Return a string which will output: min{arg_1; arg_2; ...; arg_N} in latex.

    It will also automatically ensure floats are converted to latex
    text.

    Examples
    --------
    >>> latex_min_curly_brackets(1, 2)
    str(\min \left\{1; 2\right\})

    Parameters
    ----------
    args: str
        The arguments of the min function.

    Returns
    -------
    str
        The latex representation of the min function.
    """
    arguments = [str(arg) for arg in args]
    return f"\\min \\left\\{{{'; '.join(arguments)}\\right\\}}"

codes.latex_formula.latex_replace_symbols

latex_replace_symbols(
    template: str,
    replacements: dict[str, str],
    unique_symbol_check: bool = True,
) -> str

Replace symbols in a LaTeX template string based on the provided dictionary.

This function searches the template for symbols specified in the replacements and replaces them with their corresponding values (and units). It also checks for the occurrence of symbols based on the unique_symbol_check parameter, raising an error if necessary.

Examples:

>>> latex_template = r"\frac{K_{MOD}}{B}"
>>> replacements = {"K_{MOD}": "1.0", "B": "y"}
>>> latex_replace_symbols(latex_template, replacements)
'\frac{1.0}{y}'

Parameters:

  • template (str) –

    The original LaTeX string containing symbols to replace.

  • replacements (dict[str, str]) –

    A dictionary where keys are symbols to be replaced and values are their replacements.

  • unique_symbol_check (bool, default: True ) –

    If True (default), raises an error if a symbol appears more than once in the template. If False, multiple occurrences will be replaced without error.

Returns:

  • str

    The modified LaTeX string with symbols replaced by values (and units).

Raises:

  • ValueError

    If a symbol in the dictionary is not found in the template, or if a symbol appears more than once when unique_symbol_check is True.

Source code in blueprints/codes/latex_formula.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
def latex_replace_symbols(template: str, replacements: dict[str, str], unique_symbol_check: bool = True) -> str:
    r"""
    Replace symbols in a LaTeX template string based on the provided dictionary.

    This function searches the template for symbols specified in the
    replacements and replaces them with their corresponding
    values (and units). It also checks for the occurrence of symbols based on the
    unique_symbol_check parameter, raising an error if necessary.

    Examples
    --------
    >>> latex_template = r"\frac{K_{MOD}}{B}"
    >>> replacements = {"K_{MOD}": "1.0", "B": "y"}
    >>> latex_replace_symbols(latex_template, replacements)
    '\frac{1.0}{y}'

    Parameters
    ----------
    template: str
        The original LaTeX string containing symbols to replace.

    replacements: dict[str, str]
        A dictionary where keys are symbols to be replaced and values
        are their replacements.

    unique_symbol_check: bool, optional
        If True (default), raises an error if a symbol appears more
        than once in the template. If False, multiple occurrences
        will be replaced without error.

    Returns
    -------
    str
        The modified LaTeX string with symbols replaced by values (and units).

    Raises
    ------
    ValueError
        If a symbol in the dictionary is not found in the template,
        or if a symbol appears more than once when unique_symbol_check is True.
    """
    _filled_latex_string: str = template
    for symbol, replacement in replacements.items():
        occurrences = _filled_latex_string.count(symbol)

        # Check for the presence of the symbol in the template
        if occurrences == 0:
            raise ValueError(f"Symbol '{symbol}' not found in the template.")

        # If single_symbol_search is True, check for multiple occurrences
        if unique_symbol_check and occurrences > 1:
            raise ValueError(f"Symbol '{symbol}' found multiple times in the template.")

        # Replace the symbol with its replacement
        _filled_latex_string = _filled_latex_string.replace(symbol, replacement)

    return _filled_latex_string