1 # -*- coding: utf-8 -*-
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
7 # authors Richard Heck and [SchAirport]
9 # Full author contact details are available in file CREDITS
11 # This script is intended to include a BibTeX-generated biblography
12 # in a LaTeX file, as publishers often want. It can be run manually
13 # on an exported LaTeX file, though it needs to be compiled first,
14 # so the bbl file will exist.
16 # It should also be possible to create a LyX converter to run this
17 # automatically. To set it up, create a format "ltxbbl"; make sure to
18 # check it as a document format. Then create a LaTeX-->ltxbbl converter,
20 # python -tt $$s/scripts/include_bib.py $$i $$o
21 # and give it the flags:
23 # You'll then have it in the export menu.
25 # We do not activate this converter by default, because there are problems
26 # when one tries to use multiple bibliographies.
28 # Please report any problems on the devel list.
30 from __future__ import print_function
34 def __init__(self, start = -1, end = -1):
38 class BibError(Exception):
39 def __init__(self, msg):
46 def InsertBib(fil, out):
47 ''' Inserts the contents of the .bbl file instead of the bibliography in a new .tex file '''
49 texlist = open(fil, 'r').readlines()
51 # multiple bibliographies
55 for i, line in enumerate(texlist):
56 if "\\bibliographystyle" in line:
58 elif "\\bibliography" in line:
60 elif "\\begin{btSect}" in line:
61 raise BibError("Cannot export sectioned bibliographies")
64 raise BibError("Cannot export multiple bibliographies.")
66 raise BibError("No biliography found!")
69 newlist = texlist[0:bibpos]
70 bblfile = fil[:-4] + ".bbl"
71 bbllist = open(bblfile, 'r').readlines()
73 newlist += texlist[bibpos + 1:]
75 outfile = open(out, 'w')
76 outfile.write("".join(newlist))
83 Usage: python include_bib.py file.tex [outfile.tex]
84 Includes the contents of file.bbl, which must exist in the
85 same directory as file.tex, in place of the \bibliography
86 command, and creates the new file outfile.tex. If no name
87 for that file is given, we create: file-bbl.tex.
90 if __name__ == "__main__":
92 if args <= 1 or args > 3:
96 # we might should make sure this is a tex file....
98 if infile[-4:] != ".tex":
99 print ("Error: " + infile + " is not a TeX file")
104 outfile = sys.argv[2]
106 outfile = infile[:-4] + "-bbl.tex"
108 newfile = InsertBib(infile, outfile)
109 print ("Wrote " + outfile)