latex module¶
This module provides utilities for managing LaTeX files, including compilation, creating figures and tables, and handling Beamer slides.
- compile_latex(x=None, compiler='pdflatex', keep_aux=False)¶
Compiles one or more LaTeX files.
- Parameters:
x (str, list, or None, optional) –
What to compile.
None— compile all.texfiles in the current directory.str(directory path) — compile all.texfiles in that directory.str(file path) — compile a single.texfile.listortuple— compile each file in the sequence.
Defaults to None.
compiler (str, optional) – LaTeX compiler executable to use. Common choices are
'pdflatex','lualatex', and'xelatex'. Defaults to'pdflatex'.keep_aux (bool, optional) – If False, delete auxiliary files after compilation. If True, keep them (useful for debugging). Defaults to False.
- Raises:
RuntimeError – If the requested compiler is not found in PATH.
ValueError – If a string argument is neither a valid directory nor a
.texfile.TypeError – If
xis not None, a string, or a list/tuple.
- pdf_latex(file_name, compiler='pdflatex', keep_aux=False)¶
Compiles a LaTeX file with BibTeX support using a four-pass compilation sequence: compiler twice, then BibTeX, then compiler twice more, to resolve all cross-references and bibliography entries.
- Parameters:
file_name (str) – Path to the
.texfile to compile.compiler (str, optional) – LaTeX compiler executable. Defaults to
'pdflatex'.keep_aux (bool, optional) – If False, delete auxiliary files after compilation. Defaults to False.
- Raises:
RuntimeError – If the compiler or
bibtexis not found in PATH, or if any compilation step fails.
- clean_aux_files(file_name)¶
Deletes LaTeX auxiliary files in the same directory as the given file. Removes files with the following extensions:
.aux,.log,.out,.gz,.snm,.nav,.toc,.blg,.bbl,.vrb.- Parameters:
file_name (str) – Path to any file in the target directory. The directory containing this file is cleaned; the file itself is not deleted.
- make_figure(image_filename, position='h', caption=None, label='', hspace='0cm', height='6.5cm', width=None, caption_top=True, center_image=True, filename=None)¶
Builds a LaTeX figure environment string.
- Parameters:
image_filename (str) – Path to the image file, as it will appear in the LaTeX source.
position (str, optional) – Float position specifier (
'h','t','b','p'). Defaults to'h'.caption (str, optional) – Caption text. Defaults to None.
label (str, optional) – Label for cross-referencing. Defaults to
''.hspace (str, optional) – Horizontal offset applied before the image. Defaults to
'0cm'.height (str, optional) – Image height as a LaTeX dimension string. Defaults to
'6.5cm'.width (str, optional) – Image width as a LaTeX dimension string. Defaults to None.
caption_top (bool, optional) – If True, place the caption above the image. Defaults to True.
center_image (bool, optional) – If True, wrap the image in a center environment. Defaults to True.
filename (str, optional) – If provided, write the LaTeX string to this file. Defaults to None.
- Returns:
LaTeX source for the figure environment.
- Return type:
str
- make_handout(slides_file_name, handout_file_name)¶
Generates a Beamer handout file from a slides file. Inserts
'handout,'into the\documentclassoptions on the first line of the slides file so that overlay animations are collapsed.Raises
ValueErrorif the first line of the source file does not contain a\documentclass[declaration, because silently writing a corrupted output file would be worse than failing loudly.- Parameters:
slides_file_name (str) – Path to the original Beamer slides file, with or without the
.texextension.handout_file_name (str) – Path for the generated handout file, with or without the
.texextension.
- Raises:
ValueError – If the first line of the source file does not contain a
\documentclass[...]options block.
- make_tabular(data, table_spec='', row_format=None, column_format=None, hlines=None, clines=None, pos='c', filename=None)¶
Builds a LaTeX tabular environment string.
- Parameters:
data (numpy.ndarray) – 2D array of cell values. Each element is converted to a string.
table_spec (str, optional) – Column alignment string (e.g.,
'lcc'). Defaults to''.row_format (dict, optional) – Row-level formatting. Keys are 1-based row numbers; values are lists of LaTeX command names without the leading backslash. For example,
{1: ['textbf']}bolds the first row. Defaults to None.column_format (dict, optional) – Column-level formatting. Same structure as
row_formatbut applied per column. Defaults to None.hlines (list, optional) – 1-based row numbers below which a horizontal rule is drawn. Use 0 for a rule above the first row. Defaults to None.
clines (dict, optional) – Partial horizontal rules. Keys are 1-based row numbers; values are lists of column-range strings (e.g.,
{2: ['1-3', '5-6']}). Defaults to None.pos (str, optional) – Vertical alignment of the tabular relative to surrounding text (
'b','c','t'). Defaults to'c'.filename (str, optional) – If provided, write the LaTeX string to this file. Defaults to None.
- Returns:
LaTeX source for the tabular environment.
- Return type:
str
- make_table(data, table_spec='', row_format=None, column_format=None, hlines=None, clines=None, position='h', caption=None, label='', caption_top=True, center_table=True, filename=None)¶
Builds a LaTeX table environment containing a tabular environment.
- Parameters:
data (numpy.ndarray) – 2D array of cell values.
table_spec (str, optional) – Column alignment string. Defaults to
''.row_format (dict, optional) – Row-level formatting; see
make_tabular. Defaults to None.column_format (dict, optional) – Column-level formatting; see
make_tabular. Defaults to None.hlines (list, optional) – Row numbers for horizontal rules; see
make_tabular. Defaults to None.clines (dict, optional) – Partial horizontal rules; see
make_tabular. Defaults to None.position (str, optional) – Float position specifier. Defaults to
'h'.caption (str, optional) – Caption text. Defaults to None.
label (str, optional) – Label for cross-referencing. Defaults to
''.caption_top (bool, optional) – If True, place caption above the table body. Defaults to True.
center_table (bool, optional) – If True, wrap the tabular in a center environment. Defaults to True.
filename (str, optional) – If provided, write the LaTeX string to this file. Defaults to None.
- Returns:
LaTeX source for the table environment.
- Return type:
str
- DataFrame_to_array(df, include_index=True, include_column_headers=True, keep_index_name=True)¶
Converts a pandas DataFrame to a NumPy array with optional headers and index column, suitable for passing directly to
make_tabularormake_table.- Parameters:
df (pandas.DataFrame) – Input DataFrame.
include_index (bool, optional) – Whether to include the row index as the first column. Defaults to True.
include_column_headers (bool, optional) – Whether to include column headers as the first row. Defaults to True.
keep_index_name (bool, optional) – Whether to preserve the index name in the top-left cell. Defaults to True.
- Returns:
Array representation of the DataFrame, with headers and index prepended according to the selected options.
- Return type:
numpy.ndarray
- python_script(script)¶
Executes one or more Python scripts as subprocesses. Appends
.pyto any filename that does not already have that extension.- Parameters:
script (str or list of str) – Filename or list of filenames of Python scripts to run.