Skip to content

Instantly share code, notes, and snippets.

@filipemeneses
Last active March 9, 2017 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipemeneses/062299582f3beaa68cd12ad432472b4c to your computer and use it in GitHub Desktop.
Save filipemeneses/062299582f3beaa68cd12ad432472b4c to your computer and use it in GitHub Desktop.
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