from trouver.helper.files_and_folders import text_from_file
from trouver.helper.tests import _test_directorylatex.preamble
Divide the preamble from the rest of the document
Some macros and commands defined in the preamble seem to prevent the pylatexenc methods from properly identifying the document environment/node in a LaTeX document. To circumvent this, we define a function to divide the preamble from the rest of the document
divide_preamble
divide_preamble (text:str, document_environment_name:str='document')
Divide the preamble from the rest of a LaTeX document.
| Type | Default | Details | |
|---|---|---|---|
| text | str | LaTeX document | |
| document_environment_name | str | document | |
| Returns | tuple |
latex_file_path = _test_directory() / 'latex_examples' / 'example_with_a_command_with_begin.tex'
text = text_from_file(latex_file_path)
preamble, document = divide_preamble(text)
assert r'\begin{displaymath}' in preamble
assert r'Hyun Jong Kim' in preamble
assert r'Hyun Jong Kim' not in document
assert document.startswith(r'\begin{document}')
assert document.endswith('\\end{document}')Include .sty file’s content into preamble
Writers often define custom commands in .sty files
replace_inclusion_of_style_file_with_code
replace_inclusion_of_style_file_with_code (document:str, dir:os.PathLike)
*Replace style file inclusions in document with the code of the style files.
This function searches for occurrences of \usepackage{...}, \input{...}, \import{...}{...}, \includefrom{...}{...}, and \subincludefrom{...}{...} and replaces them with the actual contents of the corresponding .sty files, if available.*
| Type | Details | |
|---|---|---|
| document | str | |
| dir | PathLike | The directory containing the style file. |
| Returns | str | The modified document with style file inclusions replaced by their contents. |
The replace_inclusion_of_style_file_with_code function substitutes the code in style files into the appropriate locations in a latex document.
latex_folder = _test_directory() / 'latex_examples' / 'latex_example_with_style_file'
main_file = latex_folder / 'main.tex'
with open(main_file, 'r', encoding='utf-8') as file:
document = file.read()
output = replace_inclusion_of_style_file_with_code(document, latex_folder)
# preamble, body = divide_preamble(document)
# commands = custom_commands(preamble)
print(output)\documentclass{article}
% Include a style file
% Start of included style file: mystyle.sty
% My custom style definitions
\newcommand{\mystylecommand}[1]{\textbf{#1}}
% End of included style file: mystyle.sty
\begin{document}
% Include a non-style file
\input{nonstylefile}
Hello, world! This document uses styles defined in mystyle.sty.
\end{document}
C:\Users\hyunj\AppData\Local\Temp\ipykernel_2016\2190002304.py:40: UserWarning: Style file c:\Users\hyunj\Documents\Development\Python\trouver\nbs\_tests\latex_examples\latex_example_with_style_file\nonstylefile.sty not found. Keeping original command.
warnings.warn(f"Style file {file_path} not found. Keeping original command.", UserWarning)
It may be best practice to apply replace_inclusion_of_styl_file_with_code to the preamble of the document assuming that the latex document only includes style files in the preamble.
latex_folder = _test_directory() / 'latex_examples' / 'latex_example_with_style_file'
main_file = latex_folder / 'main.tex'
with open(main_file, 'r', encoding='utf-8') as file:
document = file.read()
preamble, body = divide_preamble(document)
output = replace_inclusion_of_style_file_with_code(preamble, latex_folder)
# commands = custom_commands(preamble)
print(output)\documentclass{article}
% Include a style file
% Start of included style file: mystyle.sty
% My custom style definitions
\newcommand{\mystylecommand}[1]{\textbf{#1}}
% End of included style file: mystyle.sty