Created
December 4, 2012 18:18
-
-
Save restrepo/4207109 to your computer and use it in GitHub Desktop.
Convert a LaTeX master file into a flatten LaTeX with all the files included
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Convert a master latex file, | |
into a single document by including | |
automatically all the LaTeX documents | |
which are arguments of | |
\include or \input | |
ignoring any \includeonly | |
""" | |
import sys | |
if len(sys.argv)==3: | |
masterfile=sys.argv[1] | |
flattenfile=sys.argv[2] | |
else: | |
sys.exit('USAGE: %s masterfile.tex flattenfile.tex' %sys.argv[0]) | |
filetex=open(masterfile,'r') | |
texlist=filetex.readlines() | |
finaltex=open(flattenfile,'w') | |
for i in texlist: | |
if i.find(r'\input{')==0 or i.find(r'\include{')==0: | |
includetex=open(i.split('{')[-1].split('}')[0]+'.tex','r') | |
finaltex.write(includetex.read()) | |
finaltex.write('\n') | |
elif i.find(r'\includeonly{')==0: | |
finaltex.write(i.replace(r'\includeonly{',r'%\includeonly{')) | |
else: | |
finaltex.write(i) | |
filetex.close() | |
finaltex.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment