Skip to content

Instantly share code, notes, and snippets.

@raullenchai
Created April 12, 2012 17:37
Show Gist options
  • Save raullenchai/2369502 to your computer and use it in GitHub Desktop.
Save raullenchai/2369502 to your computer and use it in GitHub Desktop.
Remove Unreferenced Bibitems in Latex/Tex File
"""
Remove Unreferenced Bibitems in Latex/Tex File
Author: Raullen Chai
Date: 2012-04-12
"""
from random import *
import re
"""
Usage:
Set BIB_FILE as your bib file
Set ARTICLE as your latex file
Put these two files and this script in the same dir
Run 'Python TidyBib.py'
Collect your new BIB_FILE in the same dir
"""
BIB_FILE = r'referenc.tex'
ARTICLE = r'author.tex'
f = open(BIB_FILE, 'r')
ref_lines = f.readlines()
f.close()
if len(ref_lines) == 0:
print 'Empty Bib?'
exit()
f = open(ARTICLE, 'r')
content_data = f.read()
f.close()
if len(content_data) == 0:
print 'Empty Article?'
exit()
f = open(BIB_FILE+'(new)', 'w')
for line in ref_lines:
bibitems = re.findall(r'bibitem{[^}]*}', line)
if len(bibitems) == 0:
f.write(line)
else:
for x in bibitems:
x = x.replace(r'bibitem{', '').replace(r'}', '').strip()
if content_data.find(x) != -1:
f.write(line)
break
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment