Skip to content

Instantly share code, notes, and snippets.

@dluciv
Created March 30, 2017 07:05
Show Gist options
  • Save dluciv/ba5ffa28502e07c998783e3d4d48e7c5 to your computer and use it in GitHub Desktop.
Save dluciv/ba5ffa28502e07c998783e3d4d48e7c5 to your computer and use it in GitHub Desktop.
LaTeX `\cite{...}` order
#!/usr/bin/env ruby
# Author: Dmitry V. Luciv
# License: WTFPL 2.0 http://wtfpl.net
=begin
When typing many \bibitem's manually, instead of using BibTeX and friends,
it often becomes a pain to sort the bibliography in case when it should
appear in order of citations.
Use it as `citeorder.rb <my_lovely_article.tex >ordering.txt`
This tool is not unique, but at least is very lightweight.
=end
src = STDIN.read
cites = src.scan /\\cite\{(.+?)\}/m
refs = cites.map {|v| v[0].split ','}.flatten.map {|v| v.gsub('%', '').strip}.uniq
refs.each &method(:puts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment