Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge Latex files while working in individual files
% Many Latex configurations
\begin{document}
\section{File 1}
\end{document}
% Many Latex configurations
\begin{document}
\section{File 2}
\end{document}
% Many Latex configurations
\begin{document}
\input{./merged.tex}
\end{document}
import re
def file_get_contents(filename):
with open(filename) as f:
return f.read()
def file_put_contents(filename, data):
with open(filename, 'w') as f:
f.write(data)
f.close()
files = [
'./file_1.tex',
'./file_2.tex',
]
merge = ''
for _file in files:
a = file_get_contents(_file)
m = re.match(r'[\s\S]*begin\{document\}([\s\S]*)\\end\{document\}', a)
if (m):
merge += m.group(1)
else:
print('Document content not found.')
file_put_contents('./merged.tex', merge)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.