pygacity.pythontex.texutils module

TeX utility functions for pygacity

pygacity.pythontex.texutils.Cp_as_tex(Cp_coeff: dict | list, decoration='*', sig: int = 5, inline: bool = False) str[source]

Formats a heat capacity polynomial as a LaTeX string.

Parameters:
  • Cp_coeff (dict or list) – dictionary with keys ‘a’, ‘b’, ‘c’, ‘d’ or list of four coefficients

  • decoration (str, optional) – decoration for Cp, e.g., ‘*’ for Cp^*, default is ‘*’

  • sig (int, optional) – number of significant figures for coefficients, default is 5

Returns:

retstr – LaTeX formatted heat capacity polynomial string

Return type:

str

pygacity.pythontex.texutils.algebraify(expression: str, varvals: dict[str, any] | list[str], varsig: dict[str, int] = {}) str | tuple[str, str][source]

Substitute variables into an algebraic expression with intelligent formatting.

Parameters:
  • expression (str) – Algebraic expression with variables in {varname} format

  • varvals (dict[str, any] or list[str]) – If list: variable names to substitute symbolically If dict: variable names mapped to their values (str for symbols, float for numerics)

  • varsig (dict[str, int], optional) – Variable names mapped to significant figures for numeric values

Returns:

If varvals is a list: returns the symbolic expression If varvals is a dict: returns (symbolic_expression, numerical_expression)

Return type:

str or tuple[str, str]

pygacity.pythontex.texutils.file_listing(filename: str, style: str = 'mypython') str[source]

Generates a LaTeX lstinputlisting command string for including a file.

Parameters:
  • filename (str) – the name of the file to include

  • style (str, optional) – the style to use for the listing, default is ‘mypython’

Returns:

LaTeX lstinputlisting command string

Return type:

str

pygacity.pythontex.texutils.format_sig(x: float | Quantity, sig: int = 5, use_tex: bool = True) str[source]

Formats a floating point number to a specified number of significant figures.

Parameters:
  • x (float or pint.Quantity) – the number to format

  • sig (int, optional) – number of significant figures, default is 5

  • use_tex (bool, optional) – whether to format in LaTeX scientific notation, default is True

Returns:

s – formatted string

Return type:

str

pygacity.pythontex.texutils.frac_or_int_as_tex(f: Fraction) str[source]

Formats a Fraction as a LaTeX string, using frac{}{} if necessary.

Parameters:

f (fr.Fraction) – the fraction to format

Returns:

LaTeX formatted string

Return type:

str

pygacity.pythontex.texutils.latex_quotes(s: str) str[source]

Converts straight double-quotes in a string to LaTeX-style curly quotes.

Each " is replaced alternately with ` `` ` (opening) and '' (closing).

Parameters:

s (str) – input string, possibly containing straight double-quote characters

Returns:

string with straight double-quotes replaced by LaTeX quote pairs

Return type:

str

pygacity.pythontex.texutils.polynomial_as_tex(p: Polynomial, x: str = 'x', coeff_round: int = 0)[source]

Formats a numpy Polynomial as a LaTeX string.

Parameters:
  • p (Polynomial) – the polynomial to format

  • x (str, optional) – multiplication symbol, default is ‘x’

  • coeff_round (int, optional) – number of decimal places to round coefficients, default is 0 (integer)

Returns:

LaTeX formatted polynomial string

Return type:

str

pygacity.pythontex.texutils.symneg(value: float, sig: int = 0) str[source]
pygacity.pythontex.texutils.table_as_tex(table: dict | DataFrame, sig: int = 5, drop_zeros: list[bool] = None, total_row: list[str] = [], index: bool = False, use_tex: bool = True) str[source]

A wrapper to Dataframe.to_latex() that takes a dictionary of heading: column items and generates a table

Parameters:
  • table (dict or pd.DataFrame) – dictionary of column_name: list_of_column_values or a ready DataFrame

  • sig (int, optional) – number of significant figures for floating point numbers; default is 5

  • drop_zeros (list of bool, optional) – list of booleans parallel to table keys indicating whether to drop rows with zero in that column; default is None

  • total_row (list, optional) – list of strings representing a total row to be added at the bottom of the table; default is empty list

  • index (bool, optional) – whether to include the DataFrame index in the LaTeX table; default is False

  • use_tex (bool, optional) – whether to format numbers using (…) so they appear in math mode; default is True

Returns:

tablestring – LaTeX formatted table string

Return type:

str