Skip to content

Instantly share code, notes, and snippets.

@mgbckr
Last active August 4, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgbckr/52dddeff51a9b192919f6acd0975a787 to your computer and use it in GitHub Desktop.
Save mgbckr/52dddeff51a9b192919f6acd0975a787 to your computer and use it in GitHub Desktop.
Create joint references across main text and supplement for Science publication in LaTeX
# Science publications want the citations to be numbered sequentially across the main text and then the supplement.
# Additionally all references (including those in the supplement) are to appear in the main text.
# Reference: https://www.sciencemag.org/authors/instructions-preparing-initial-manuscript#format-supplemental
# After struggling a bit with the `xr` package in Overleaf, I gave up and just scripted this quick and dirty hack.
#
# Instructions:
# * place `% CITATION HANDLE` right before you bibliography in the main file
# and right after `\begin{document}` in the supplement
# * run the file via `bash prepare_references_for_science_publication.sh`
#
# Requirements (change the script if you need other executables / formats):
# * a working LaTeX installation
# * `pdflatex`
# * `latexmk`
#
# If you are using Overleaf, clone your repository, run the script and push the changes.
# names of main and supplementary files (no extension)
MAIN_NAME=main
SUPP_NAME=supplementary
# this is where `\nocite` citations will be inserted in each document
# put this right before you bibliography in the main file
# and right after `\begin{document}` in the supplement
CITATION_HANDLE="% CITATION HANDLE"
# handles for updating citations; no need to change this
START_STRING="% CITATIONS - START"
END_STRING="% CITATIONS - END"
# remove previous `\nocite` if they are there
if grep -q "${START_STRING}" "${MAIN_NAME}.tex"; then
echo "CITATIONS found"
sed "/${START_STRING}/,/${END_STRING}/d" "${MAIN_NAME}.tex" > "${MAIN_NAME}_tmp.tex"
mv "${MAIN_NAME}.tex" "${MAIN_NAME}_backup.tex"
mv "${MAIN_NAME}_tmp.tex" "${MAIN_NAME}.tex"
fi
if grep -q "${START_STRING}" "${SUPP_NAME}.tex"; then
echo "CITATIONS found"
sed "/${START_STRING}/,/${END_STRING}/d" "${SUPP_NAME}.tex" > "${SUPP_NAME}_tmp.tex"
mv "${SUPP_NAME}.tex" "${SUPP_NAME}_backup.tex"
mv "${SUPP_NAME}_tmp.tex" "${SUPP_NAME}.tex"
fi
# build files
latexmk -pdf ${MAIN_NAME}.tex
latexmk -pdf ${SUPP_NAME}.tex
# extract citations
grep -h citation ${MAIN_NAME}.aux | sed /citation/s//nocite/ > citations_main.tex
grep -h citation ${SUPP_NAME}.aux | sed /citation/s//nocite/ > citations_supp.tex
# insert citations (via `\input`)
sed -i -e "/${CITATION_HANDLE}/a ${END_STRING}" "${MAIN_NAME}.tex"
sed -i -e "/${CITATION_HANDLE}/r citations_supp.tex" "${MAIN_NAME}.tex"
sed -i -e "/${CITATION_HANDLE}/a ${START_STRING}" "${MAIN_NAME}.tex"
sed -i -e "/${CITATION_HANDLE}/a ${END_STRING}" "${SUPP_NAME}.tex"
sed -i -e "/${CITATION_HANDLE}/r citations_main.tex" "${SUPP_NAME}.tex"
sed -i -e "/${CITATION_HANDLE}/a ${START_STRING}" "${SUPP_NAME}.tex"
# build files again with updated references (rerun latexmk)
latexmk -pdf ${MAIN_NAME}.tex
latexmk -pdf ${SUPP_NAME}.tex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment