homework class

class homework(filename=None)

Initializes an instance of the homework class.

Parameters:

filename (str, optional) – Name of the LaTeX file to save answers to. Default is None.

Attributes

filename: str

File name for the LaTeX output file.

latex_answers: dict

Dictionary for storing answers as LaTeX commands.

Methods

add_latex_answer(name, value, precision=4)

Adds an answer as a LaTeX command to the latex_answers dictionary.

Parameters:
  • name (str) – Name of the new LaTeX command. Must conform to LaTeX naming conventions.

  • value (str or numeric) – Answer to be stored. Strings are stored as-is, numeric values are formatted.

  • precision (int, optional) – Number of decimal places to round numeric values. Default is 4.

Returns:

None

Return type:

None

Adds:
  • latex_answers[name]: The formatted value as a LaTeX command.

gs_answer(value, tolerance=8, precision=0.005)

Generates a Gradescope-compatible formatted string for an answer.

Parameters:
  • value (str or numeric) – Answer to format for Gradescope.

  • tolerance (float, optional) – Error tolerance for the answer. Default is 8.

  • precision (float, optional) – Precision for rounding numeric values. Default is 0.005.

Returns:

A Gradescope-compatible formatted string.

Return type:

str

write_answer_file(filename)

Exports LaTeX answers as new commands in a .tex file.

Parameters:

filename (str) – Name of the LaTeX file to save answers to. Appends .tex if missing.

Returns:

None

Return type:

None