76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
|
import sys
|
||
|
import os
|
||
|
import re
|
||
|
|
||
|
list_files = {"Example.ipynb": "Example.tex"}
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
for orig_name, tex_name in list_files.items():
|
||
|
print(f"Converting file {orig_name} into {tex_name}.")
|
||
|
if not os.path.exists(orig_name):
|
||
|
print(f"Original file {os.path.abspath(orig_name)} could not be found!")
|
||
|
continue
|
||
|
|
||
|
|
||
|
print("Using black next comment out if you don't want to use it")
|
||
|
|
||
|
try:
|
||
|
os.system(f"black {orig_name}")
|
||
|
except:
|
||
|
pass
|
||
|
|
||
|
os.system(f"pandoc --listings -f ipynb -t latex {orig_name} -o {tex_name}")
|
||
|
|
||
|
if not os.path.exists(tex_name):
|
||
|
print(f"The file {os.path.abspath(tex_name)} does not exist.")
|
||
|
print("The file should now exist")
|
||
|
continue
|
||
|
|
||
|
with open(tex_name, "r", encoding="utf-8") as read_file:
|
||
|
content = read_file.read()
|
||
|
|
||
|
start_string = "\\hypertarget{start-here}{%\n\\section{Start here}\\label{start-here}}"
|
||
|
end_string = "\\hypertarget{end-here}{%\n\\section{End here}\\label{end-here}}"
|
||
|
content = content.split(start_string, 2)[1] if start_string in content else content
|
||
|
# print(content)
|
||
|
content = content.split(end_string)[0] if end_string in content else content
|
||
|
print(content)
|
||
|
|
||
|
# redefine code blocks as IPython blocks and add the minipage surrounding to force them on a single page.
|
||
|
content = re.sub(
|
||
|
r"\\begin\{lstlisting}\[language=Python](.+?)\\end\{lstlisting}",
|
||
|
"\\\\begin{minipage}{1.0\\\\textwidth}\n"
|
||
|
"\\\\begin{lstlisting}[language=iPython]\\g<1>"
|
||
|
"\\\\end{lstlisting}\n\\\\end{minipage}",
|
||
|
content,
|
||
|
flags=re.DOTALL,
|
||
|
)
|
||
|
|
||
|
# remove the listings code around a pandas table exported as latex.
|
||
|
content = re.sub(
|
||
|
r"\\begin\{lstlisting}\n*\\begin\{tabular}",
|
||
|
"\\\\begin{tabular}",
|
||
|
content,
|
||
|
flags=re.DOTALL,
|
||
|
)
|
||
|
content = re.sub(
|
||
|
r"\\end\{tabular}\n*\\end\{lstlisting}",
|
||
|
"\\\\end{tabular}",
|
||
|
content,
|
||
|
flags=re.DOTALL,
|
||
|
)
|
||
|
content = re.sub(
|
||
|
r"print\((.+?)\.to_latex\(.*?\)\)\n", "\\g<1>\\n", content, flags=re.DOTALL
|
||
|
)
|
||
|
|
||
|
content = re.sub(r"\n(%%capture\n)", "", content, flags=re.DOTALL)
|
||
|
content = re.sub(r"\n(plt.savefig\(.*?\))", "", content, flags=re.DOTALL)
|
||
|
|
||
|
|
||
|
content = content.strip()
|
||
|
|
||
|
# overwrite the original file
|
||
|
with open(tex_name, "w", encoding="utf-8") as write_file:
|
||
|
content = write_file.write(content)
|
||
|
|