2 # -*- coding: utf-8 -*-
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # authors Richard Heck and [SchAirport]
10 # Full author contact details are available in file CREDITS
12 # This script is intended to include a BibTeX-generated biblography
13 # in a LaTeX file, as publishers often want. It can be run manually
14 # on an exported LaTeX file, though it needs to be compiled first,
15 # so the bbl file will exist.
17 # It should also be possible to create a LyX converter to run this
18 # automatically. To set it up, create a format "ltxbbl"; make sure to
19 # check it as a document format. Then create a LaTeX-->ltxbbl converter,
21 # python -tt $$s/scripts/include_bib.py $$i $$0
22 # and give it the "needaux" flag. You'll then have it in the export menu.
23 # We do not activate this converter by default, because there are problems
24 # when one tries to use multiple bibliographies.
26 # Please report any problems on the devel list.
31 def __init__(self, start = -1, end = -1):
35 class BibError(Exception):
36 def __init__(self, msg):
43 def InsertBib(fil, out):
44 ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
46 texlist = open(fil, 'r').readlines()
48 # multiple bibliographies
52 for i, line in enumerate(texlist):
53 if "\\bibliographystyle" in line:
55 elif "\\bibliography" in line:
57 elif "\\begin{btSect}" in line:
58 raise BibError("Cannot export sectioned bibliographies")
61 raise BibError("Cannot export multiple bibliographies.")
63 raise BibError("No biliography found!")
66 newlist = texlist[0:bibpos]
67 bblfile = fil[:-4] + ".bbl"
68 bbllist = open(bblfile, 'r').readlines()
70 newlist += texlist[bibpos + 1:]
72 outfile = open(out, 'w')
73 outfile.write("".join(newlist))
80 Usage: python include_bib.py file.tex [outfile.tex]
81 Includes the contents of file.bbl, which must exist in the
82 same directory as file.tex, in place of the \bibliography
83 command, and creates the new file outfile.tex. If no name
84 for that file is given, we create: file-bbl.tex.
87 if __name__ == "__main__":
89 if args <= 1 or args > 3:
93 # we might should make sure this is a tex file....
95 if infile[-4:] != ".tex":
96 print "Error: " + infile + " is not a TeX file"
101 outfile = sys.argv[2]
103 outfile = infile[:-4] + "-bbl.tex"
105 newfile = InsertBib(infile, outfile)
106 print "Wrote " + outfile