#!/usr/bin/env python # # Python script to set up a latex article with Makefile. # Automatically recognises if you are on macosx rather than linux # and creates an appropriate Makefile. # Takes one argument: the name of the directory to place the files into. # Options: -h for help # -b for an article with bibliography # -s for a "small" article (effectivel a blank page, no title) # import sys, os, time, pwd, string, getpass, getopt, posixpath def usage(): print "Usage: %s [-h] [-b] [-s] " \ % posixpath.basename(sys.argv[0]) print """\ -h: this message -b: create article with bibliography file -s: create small file (no title, etc). Incompatable with -b.""" #default settings short = 0 do_bib = 0 if sys.platform == "darwin": target = "pdf" ltxcmd = "pdflatex" viewcmd = "open -a 'Preview'" else: target = "ps" ltxcmd = "latex" viewcmd = "gv" # process command line args try : options, args = getopt.getopt(sys.argv[1:], "hbs") except getopt.error : usage() sys.exit(1) for o, a in options : if o == "-h" : usage() sys.exit(0) elif o == "-b" : do_bib = 1 elif o == "-s" : short = 1 if len(args) == 0 or (short and do_bib) : usage() sys.exit(1) document = args[0] if do_bib : MAKEFILE = """\ all : %s.%s \t%s $< %%.dvi : %%.tex \tlatex $< \tbibtex `basename $< .tex` \tlatex $< \tlatex $< %%.pdf : %%.tex \tpdflatex $< \tbibtex `basename $< .tex` \tpdflatex $< \tpdflatex $< %%.ps : %%.dvi \tdvips -t a4 -o $@ $< clean : \trm -f *.ps *.log *.dvi *.aux *.bbl *.blg *~ *.pdf """ % (document, target, viewcmd) else : MAKEFILE = """\ all : %s.%s \t%s $< %%.dvi : %%.tex \tlatex $< %%.pdf : %%.tex \tpdflatex $< %%.ps : %%.dvi \tdvips -t a4 -o $@ $< clean : \trm -f *.ps *.log *.dvi *.aux *~ *.blg *.bbl """ % (document, target, viewcmd) if not short and do_bib : LATEXSOURCE = """\ \\documentclass[11pt]{article} \\usepackage{a4wide} \\usepackage[longnamesfirst,round]{natbib} \\author{%s} \\title{%s} \\date{%s} \\begin{document} \\maketitle \\linespread{1.3}\\normalsize \\section{Introduction} Your paragraph material goes here. Parenthetical citations look like this: \citep{%s99}. Textual citations look like this: \citet{%s99}. \\bibliographystyle{plainnat} \\bibliography{%s} \\end{document} """ % (string.split(pwd.getpwuid(os.getuid())[4], ",")[0], document, time.strftime("%d %B %Y", time.localtime(time.time())), getpass.getuser(), getpass.getuser(), document) elif not short : LATEXSOURCE = """\ \\documentclass[11pt]{article} \\usepackage{a4wide} \\author{%s} \\title{%s} \\date{%s} \\begin{document} \\maketitle \\linespread{1.3}\\normalsize \\section{Introduction} Your paragraph material goes here. \\end{document} """ % (string.split(pwd.getpwuid(os.getuid())[4], ",")[0], document, time.strftime("%d %B %Y", time.localtime(time.time()))) else : LATEXSOURCE = """\ \\documentclass[11pt]{article} \\usepackage{a4wide} \\pagestyle{empty} \\begin{document} Write something here \\end{document} """ BIBLIOGRAPHY = """\ @Article{%s99, author = {%s}, title = {The Test Entry}, journal = {Journal of Stuff}, year = {1999}, OPTkey = {}, volume = {3}, number = {2}, pages = {123--135}, OPTmonth = {}, OPTnote = {}, OPTannote = {} } """ % (getpass.getuser(), string.split(pwd.getpwuid(os.getuid())[4], ",")[0]) # make the document's directory error = os.system("mkdir %s" % document) # crash out if the document directory already exists if error > 0 : sys.exit(1) # make the document's Makefile f = open("%s/Makefile" % document, "w") f.write(MAKEFILE) f.close() # make the document's latex source file f = open("%s/%s.tex" % (document, document), "w") f.write(LATEXSOURCE) f.close() # make the document's bibliography if necessary if do_bib : f = open("%s/%s.bib" % (document, document), "w") f.write(BIBLIOGRAPHY) f.close()