Sorting LaTeX references

I don’t use .bib files and just populate bibliographies like:


\begin{thebibliography}{99}

\bibitem{Romanelli1989}
  F.~Romanelli, Phys. Fluids B \textbf{1}, 1018 (1989).

%.... (other citations)

One issue is that this can be out of order compared to the order of /cite{} in the text. The following python script goes through the citations in the text and prints the appropriately ordered bibliography. Replacing the bibliography must be done by hand, but it’s safer that way.


f=open("article.tex","rb")

cites=[]
A=f.tell()

for line in f.readlines():
  while(True):
    a=line.find(r"\cite{")
    if(a==-1):
      break
    else:
      line=line[a+5:]
      b=line.find("}")
      blah=line[1:b]
      if(blah!=""):
        B=blah.split(",")
        for e in B:
          if e not in cites:
            cites.append(e)
      line=line[b:]

refs=[]

f.seek(A)
last_line=""
for line in f.readlines():
  if(last_line[:5]==r"\bibi"):
    refs.append((last_line,line))
  last_line=line

for i in cites:
  for j in refs:
    end=j[0].find("}")                                                                                                                                        
    if(i==j[0][9:end]):
      print j[0][:-1]
      print j[1]
      continue

Author: Garth Whelan

~-~-~-~-~-~-~-~

Leave a Reply

Your email address will not be published. Required fields are marked *