Created
October 23, 2016 19:40
-
-
Save cerisara/a5b6f9e510763accbe1337011049ec44 to your computer and use it in GitHub Desktop.
Fast convert latex to markdown (part of Arxiv2Kindle)
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
import sys | |
def getText(l): | |
l=l.replace('\\citep','') | |
l=l.replace('\\cite','') | |
return l | |
with open(sys.argv[1],'rb') as f : ls = f.readlines() | |
empty=False | |
eq=False | |
for l in ls: | |
l=l.strip() | |
if len(l)==0: | |
if not empty: | |
empty=True | |
print("") | |
continue | |
if l[0]=='\\': | |
if eq: | |
if l.startswith('\\end{eq'): eq=False | |
else: print(l) | |
if l.startswith('\\section'): | |
print('\n# '+getText(l[9:-1])+'\n') | |
elif l.startswith('\\subsection'): | |
print('\n## '+getText(l[12:-1])+'\n') | |
elif l.startswith('\\subsubsection'): | |
print('\n### '+getText(l[15:-1])+'\n') | |
elif l.startswith('\\begin{eq'): | |
eq=True | |
if not empty: | |
empty=True | |
print("") | |
# supprime toutes les lignes qui commencent par \ | |
elif l[0]=='%': | |
# supprime toutes les lignes qui commencent par \ | |
if not empty: | |
empty=True | |
print(getText(l)) | |
else: | |
print(l) | |
empty=False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment