|
import os |
|
import sys |
|
import subprocess |
|
|
|
def write_latex_preamble(): |
|
"""Write the preamble and ending lines for the tex document. |
|
Abstract and title are read from the corresponding md files""" |
|
with open('title.md') as f: |
|
title = f.readline() |
|
with open('Abstract.md') as f: |
|
lines = f.readlines() |
|
abstract = "".join(lines) |
|
|
|
before = """ |
|
\documentclass{{article}} |
|
|
|
\usepackage{{graphicx}} |
|
\usepackage[space]{{grffile}} |
|
\usepackage{{latexsym}} |
|
\usepackage{{amsfonts,amsmath,amssymb}} |
|
\usepackage{{url}} |
|
\usepackage[utf8]{{inputenc}} |
|
\usepackage{{fancyref}} |
|
\usepackage{{hyperref}} |
|
\hypersetup{{colorlinks=false,pdfborder={{0 0 0}},}} |
|
|
|
\\begin{{document}} |
|
\\title{{{title}}} |
|
\date{{\\today}} |
|
|
|
\\bibliographystyle{{plain}} |
|
\maketitle |
|
\\begin{{abstract}} |
|
{{{abstract}}} |
|
\end{{abstract}} |
|
""".format(title=title, abstract=abstract) |
|
|
|
after = """ |
|
\\bibliography{bibliography/converted_to_latex.bib} |
|
|
|
\end{document} |
|
""" |
|
with open('before.tex', 'w') as f: |
|
f.write(before) |
|
with open('after.tex', 'w') as f: |
|
f.write(after) |
|
|
|
def remove_generated_files(): |
|
"""Removes files made during PDF generation""" |
|
cmd = "rm *.aux *.log *.bbl *.blg *.out *.tex" |
|
p = subprocess.call(cmd, shell=True, stderr=subprocess.PIPE) |
|
|
|
def make_image_md(image_file): |
|
"""Make tex file for each image""" |
|
caption = "" |
|
caption_dir = os.path.dirname(image_file) |
|
caption_file = os.path.join(caption_dir, "caption.md") |
|
caption_tex_file = os.path.join(caption_dir, "caption.tex") |
|
cmd = "pandoc {} -f markdown -t latex -o {}".format(caption_file, caption_tex_file) |
|
p = subprocess.call(cmd, shell=True, stderr=subprocess.PIPE) |
|
with open(caption_tex_file, 'r') as imf: |
|
lines = imf.readlines() |
|
caption += "".join(lines) |
|
|
|
content = """ |
|
\\begin{{figure}}[h!] |
|
\\begin{{center}} |
|
\includegraphics[width=0.98\columnwidth]{{{image_file}}} |
|
\caption{{{caption}}} |
|
\end{{center}} |
|
\end{{figure}} |
|
""".format(image_file=image_file, caption=caption) |
|
md_name = "{}.md".format(os.path.basename(image_file)) |
|
with open(md_name,'w') as out: |
|
out.write(content) |
|
return md_name |
|
|
|
def make_tex_output(): |
|
"""Generate pandoc command and run it to generate the tex file""" |
|
with open("layout.md", "r") as f: |
|
lines = f.readlines() |
|
files = [] |
|
for l in lines: |
|
l = l.strip() |
|
if l=="Abstract.md": |
|
#skip Abstract.md as it's read in already |
|
#when generating the preabmle file |
|
continue |
|
elif "png" not in l: |
|
#Don't add image files in raw |
|
files.append(l) |
|
else: |
|
try: |
|
files.append(make_image_md(l)) |
|
except IOError: |
|
continue |
|
cmd_files = " ".join(files) |
|
cmd = "pandoc {} -f markdown+tex_math_single_backslash -t latex -o out.tex".format(cmd_files) |
|
|
|
print cmd |
|
p = subprocess.call(cmd, shell=True) |
|
return p |
|
|
|
def generate_pdf(): |
|
"""Concatenate generated tex files and run xelatex and bibtex""" |
|
cmd = "cat before.tex out.tex after.tex > final.tex && xelatex final\ |
|
&& bibtex final && xelatex final && xelatex final" |
|
p = subprocess.call(cmd, shell=True) |
|
return p |
|
|
|
if __name__=="__main__": |
|
remove_generated_files() |
|
write_latex_preamble() |
|
make_tex_output() |
|
p = generate_pdf() |
|
sys.exit(p) |