Last active
August 26, 2018 09:28
-
-
Save petRUShka/211547be85db39cc24ee4f0dfd0a0545 to your computer and use it in GitHub Desktop.
Script that converts biblatex (bibtex) styled latex document bibliography to standard bibitem bibliography (thebibliography)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################### | |
# # | |
# BibTeX (BibLaTeX) to LaTeX \bibitem converter # | |
# # | |
############################################################### | |
# # | |
# This script converts a file containing # | |
# BibTeX (BibLaTeX) entries to a list of \bibitems, # | |
# suitable for use in LaTeX. # | |
# # | |
# Special thanks to this blog post: # | |
# http://timothyandrewbarber.blogspot.com/2011 -> # | |
# /08/fundamental-thinking-convert-bibtex.html # | |
# for pointing me in the correct direction # | |
# and giving the instruction on how to do # | |
# this. # | |
# # | |
# Dependencies: perl, awk, sed, tr, latex, bibtex, bash # | |
# # | |
# Usage: # | |
# ./bibtex-to-latex-bibitem.sh file1.tex file2.tex > out.bib # | |
# # | |
############################################################### | |
# config | |
documentclass="russian" | |
bibliographystyle="plain" | |
# it can be something more interesting e.g.: | |
# bibliographystyle="ugost2008" | |
# variables | |
tex_filenames=$@ | |
tmp_dir=$(mktemp -d) | |
tmp_bib=$tmp_dir/refs.bib | |
tmp_tex=$tmp_dir/dummy.tex | |
tmp_tex_basename=$(basename $tmp_tex .tex) | |
# extract keys | |
# split keys separated by commas | |
# uniq without sorting to preserve order | |
keys=$(cat $tex_filenames | perl -lne 'print join "\n", m/\\cite\{([^}]+)}/g' | sed 's/,\s*/\n/g' | awk '!x[$0]++') | |
# DEBUG: print found keys | |
# for key in ${keys[@]} | |
# do | |
# echo $key 1>&2 | |
# done | |
echo "Count Keys: " $( expr $(echo "${keys[@]}" | wc -l) - 1 ) 1>&2 | |
# extract bib entries by keys | |
if [ -f $tmp_bib ]; then | |
echo "rm $tmp_bib" | |
rm $tmp_bib | |
fi | |
# bib_files=("${@:2}") | |
# Extract name of bib files from tex files | |
bib_files=$(cat $tex_filenames | perl -lne '/\\bibliography\{([^}]+)}/ or next; print $1' | sed 's/,\s*/\n/g' | sed '/\.bib$/! s/$/\.bib/' | tr '\n' ' '| awk '!x[$0]++') | |
for key in $keys | |
do | |
sed -n "/${key}/,/^\s*}\s*$/p" ${bib_files} >> $tmp_bib | |
done | |
# Create template | |
printf "\\\\documentclass[${documentclass}]{article}\\n" > $tmp_tex | |
printf '\\usepackage[utf8]{inputenc}\n' >> $tmp_tex | |
printf '\\usepackage{babel}\n' >> $tmp_tex | |
printf '\\begin{document}\n' >> $tmp_tex | |
printf '\\nocite{*}\n' >> $tmp_tex | |
printf "\\\\bibliography{${tmp_bib}}\\n" >> $tmp_tex | |
printf "\\\\bibliographystyle{${bibliographystyle}}\\n" >> $tmp_tex | |
printf '\\end{document}\n' >> $tmp_tex | |
# Create references | |
cd $tmp_dir | |
latex ${tmp_tex_basename} &>/dev/null | |
bibtex ${tmp_tex_basename} &>/dev/null | |
bibtex ${tmp_tex_basename} &>/dev/null | |
latex ${tmp_tex_basename} &>/dev/null | |
cat ${tmp_tex_basename}.bbl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://tex.stackexchange.com/questions/154215/biblatex-biber-to-bibitem
There is an alternative to parsing tex-file for
\cite
s and extracting necessary items by keys from extracted bib-files. And it is:Dummy file:
So v2 of this script should do it with biber instead of straightforward tex-file parser.