Skip to content

Instantly share code, notes, and snippets.

@cerisara
Created October 23, 2016 19:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cerisara/a5b6f9e510763accbe1337011049ec44 to your computer and use it in GitHub Desktop.
Save cerisara/a5b6f9e510763accbe1337011049ec44 to your computer and use it in GitHub Desktop.
Fast convert latex to markdown (part of Arxiv2Kindle)
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