'Create tex files with Python with proper indentation
I am using Python to create the tex file for some of my LaTeX projects. The problem is that if I want to have proper indentation in my Python code then the resulting tex file has awful indents because the strings keep the spaces and tabs I add in Python. How can I remedy that?
A sample of my Python code is the following:
import os
from pathlib import Path
import subprocess
import shutil
# ==============================================================================
#
# ==============================================================================
report_files_path = Path("./report-files")
filenames = os.listdir(report_files_path)
filenames.sort()
with open("./source-files/main.tex", "w") as file:
file.write(
r"""
\documentclass{report}
\usepackage{preamble}
\begin{document}
\begin{titlepage}
\vspace*{\fill}
{\Large \textbf{Book}}
\today
\vspace{\fill}
\end{titlepage}
%
%
\tableofcontents
%
%
"""
)
for filename in filenames:
if ".tex" in filename:
file.write("\n")
file.write(f"\input{{../{Path(report_files_path, filename)}}}")
file.write("\n\n")
file.write(
r"""
%
%
"""
)
file.write("\n")
file.write("\end{document}")
# ==============================================================================
#
# ==============================================================================
command = (
r"""
cd source-files && \
latexmk \
-quiet \
-outdir=../output-files \
-shell-escape \
main.tex
"""
)
subprocess.run(command, shell = True)
shutil.copyfile(
"output-files/main.pdf",
"./book.pdf"
)
The resulting 'tex` file is the following:
\documentclass{report}
\usepackage{preamble}
\begin{document}
\begin{titlepage}
\vspace*{\fill}
{\Large \textbf{Book}}
\today
\vspace{\fill}
\end{titlepage}
%
%
\tableofcontents
%
%
\input{../report-files/report_1.tex}
\input{../report-files/report_2.tex}
%
%
\end{document}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
