2 # This file is part of LyX, the document processor.
3 # Licence details can be found in the file COPYING.
5 # authors Richard Kimberly Heck and [SchAirport]
7 # Full author contact details are available in file CREDITS
9 # This script is intended to include a BibTeX-generated biblography
10 # in a LaTeX file, as publishers often want. It can be run manually
11 # on an exported LaTeX file, though it needs to be compiled first,
12 # so the bbl file will exist.
14 # It should also be possible to create a LyX converter to run this
15 # automatically. To set it up, create a format "ltxbbl"; make sure to
16 # check it as a document format. Then create a LaTeX-->ltxbbl converter,
18 # $${python} $$s/scripts/include_bib.py $$i $$o
19 # and give it the flags:
21 # 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).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).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)