I'd already created 150 references in a document using thebibliography, and was faced with the task of sorting them in order of first appearance in the text (a very common requirement in scientific journals).
you have two choices: (1) write the entries in thebibliography in citation order (2) use a .bib file, \bibliographystyle{unsrt} and BibTeX.
Both the above require a lot of work to either manually re-sort thebibliography or manually create a database .bib file.
https://code.google.com/p/latex-bibitemstyler/ seemed to be the solution, but I couldn't get it to work on my system and was working to a deadline. So I've written a brief and very inelegant Python 2.7 workaround which does the job and might help someone with a similar problem. If there could be a standard LaTeX command to sort thebibliography by order of first citation in the text in future releases, I'd be very grateful.
print"\n\nBibSort_v1\nSorts LaTeX 'thebibliography' in order of first citation in text\nDave Williams 26Mar2017\nRun the code in the same directory as your LaTeX source file\nUsing Python 2.x"
filename = raw_input("\n\nEnter LaTeX source filename (e.g. 'input.tex'): ")
fileobject = (open(filename, 'r'))
rawtext = fileobject.read()
fileobject.close()
start = '\\begin{document}'
end = '\\begin{thebibliography}'
bodytext = rawtext[rawtext.find(start)+len(start):rawtext.rfind(end)]
start = '\\begin{thebibliography}'
end = '\\end{thebibliography}'
bibliography = rawtext[rawtext.find(start)+len(start):rawtext.rfind(end)]
authorlist = []
for char in range(0,len(bodytext) - 10):
if bodytext[char:char+6] == '\\cite{':
author = ''
char +=6
while (bodytext[char] != '}'):
if (bodytext[char] == ' '):
char+=1
elif(bodytext[char] == ','):
char +=1
if author in authorlist:
author = ''
else:
authorlist.append(author)
author=''
else:
author += (bodytext[char])
char +=1
if author in authorlist:
pass
else:
authorlist.append(author)
labellist = []
reflist = []
for char in range(0,len(bibliography) - 7):
ref =''
if bibliography[char:char+9] == '\\bibitem{':
char+=9
label =''
while(bibliography[char] != '}'):
label += bibliography[char]
char+=1
labellist.append(label)
char+=1
while (bibliography[char:char+9] != '\\bibitem{'):
if char == len(bibliography) -1:
break
elif (bibliography[char]) == '\n':
char +=1
else:
ref += (bibliography[char])
char +=1
reflist.append(ref)
dictionary = dict(zip(labellist, reflist))
output =''
orphanlist =''
try:
for name in authorlist:
output += '\\bibitem{' + name + '}\n' + dictionary[name] + '\n\n'
if len(authorlist) < len(labellist):
output+="%" + "-"*80 + "\n"
output+= "%"+ " The following %d references are in thebibliography but not cited in the text:\n\n" % (len(reflist) - len(authorlist))
orphanlist = list(set(labellist)-set(authorlist))
for name in range(0, len(orphanlist)-1):
output += '\\bibitem{' + orphanlist[name] + '}\n' + dictionary[orphanlist[name]] + '\n\n'
print("\n\n%d unique references were found in the text and sorted in order of appearance in the text" % (len(authorlist)))
print("%d additional references were found in \\thebibliography which were not used in the text" % (len(orphanlist)))
print("\nNew bibliography saved as:\n%s\n\nUsing a text editor, Copy all contents of this file,\nand Paste over the existing mybibliography in your LaTeX source file:\n%s\n" % (filename[:-4]+'_NewBib.txt',filename))
fileobject = open(filename[:-4]+'_NewBib.txt', 'w')
fileobject.write(output)
fileobject.close
except (TypeError, ValueError), e:
print e
print "\nThere may be a mis-match between the labels in your bibliography\n and one or more labels in your in-text citations.\nPerhaps there is a typo, or you've used a capital letter in one\nbut not the other.\nPlease check your the flagged citation using 'find' in your\n Latex editor and run this Python script again."
unsrt
style does exactly this: a minimal example of your code would be very useful to give a more targeted answer. – Joseph Wright♦ May 4 '11 at 9:42.aux
and.bbl
files before it will work properly... – Seamus May 4 '11 at 17:13