Skip to content

Instantly share code, notes, and snippets.

@chr5tphr
Last active February 4, 2022 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chr5tphr/b2d25cbd71ba6a7f96d47a518667477c to your computer and use it in GitHub Desktop.
Save chr5tphr/b2d25cbd71ba6a7f96d47a518667477c to your computer and use it in GitHub Desktop.
Export latex project with a flat figure hierachy.
#!/usr/bin/env bash
set -u
usage() {
cat <<EOF
Usage: "${BASH_SOURCE[0]##*/}" [options] [extra_file ...]
Export latex project with a flat figure hierachy.
Positional arguments are additional files that should be added to the exported zip.
Available options:
-o, --output Path to output zip file (default: "export.zip")
-s, --sourcetex Path to main .tex (default: "main.tex")
-b, --bibtex Path to bibtex file (default: "references.bib")
-c, --compress Compress PDF-figures with ghostscript (lossy)
-k, --keepdir Do not delete temporary output directory
--logfigmap Log figure path map
-h, --help Print this help and exit
-v, --verbose Print script debug info
EOF
exit
}
die() {
echo -e "${1:-"Error!"}" >&2
exit "${2-1}"
}
parse_params() {
keepdir=0
compress=0
output='export.zip'
sourcetex='main.tex'
sourcebib='references.bib'
manifest=()
logfigmap=
while (($#)); do
case "${1-}" in
-h | --help) usage ;;
-v | --verbose) set -x ;;
-k | --keepdir) keepdir=1 ;;
-c | --compress) compress=1 ;;
-s | --source) sourcetex="${2-}"; shift ;;
-b | --bibtex) sourcebib="${2-}"; shift ;;
-o | --output) output="${2-}"; shift ;;
--logfigmap) logfigmap="${2-}"; shift ;;
--) manifest+=("${@:2}"); break ;;
-*) die "Unknown option: ${1-}" ;;
*) manifest+=("${1-}") ;;
esac
shift
done
[[ -z "${output}" ]] && die "Empty parameter: output"
[[ -z "${sourcetex}" ]] && die "Empty parameter: source"
[[ -z "${sourcebib}" ]] && die "Empty parameter: bibtex"
return 0
}
pdfcp() {
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -sOutputFile="${2}" "${1}"
}
main_export(){
OUTDIR="${output}.temp"
mkdir "$OUTDIR"
MAIN="main_$(date '+%Y%m%d%H%M%S')"
basesourcebib="${sourcebib##*/}"
# copy main, strip comments and rename bibtex file
sed -e '/^\s*%/d' -e 's/\([^\\]%\).*$/\1/' -e "s#\\\\bibliography{${basesourcebib%%.bib}}#\\\\bibliography{${MAIN}}#" < "${sourcetex}" > "${OUTDIR}/${MAIN}.tex"
cp "${sourcebib}" "${OUTDIR}/${MAIN}.bib"
# copy manifest files
for file in "${manifest[@]}"; do
cp "${file}" "${OUTDIR}/${file##*/}"
done
# parse "${sourcetex}"
mapfile -t FIGURES < <(sed -e '/\includegraphics/!d' -e '/ *\%/d' -e 's/^.*{\(.*\)}.*$/\1/g' < "${sourcetex}")
# truncate logfigmap
[ -n "${logfigmap}" ] && :>"${logfigmap}"
# replace each filename in tex and copy the appropriate figure
local -i figindex=0
for file in "${FIGURES[@]}"; do
newfile="$((figindex++))-${file##*/}"
sed -i "s#${file}#${newfile}#g" "${OUTDIR}/${MAIN}.tex"
ext="$(file --brief --extension "${file}")"
if ((compress)) && [ "${ext}" == 'pdf' ]; then
pdfcp "${file}" "${OUTDIR}/${newfile}"
else
cp "${file}" "${OUTDIR}/${newfile}"
fi
[ -n "${logfigmap}" ] && printf '%s\t%s\n' "$file" "$newfile" >> "${logfigmap}"
done
# add files to zip
cd ${OUTDIR}
zip -r "../${output}" .
latexmk -silent -pdf "${MAIN}.tex"
zip "../${output}" "${MAIN}.bbl"
# delete temporary directory
if ! ((keepdir)); then
cd ..
rm -rf "${OUTDIR}"
fi
}
parse_params "$@"
main_export
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment