Skip to content

Instantly share code, notes, and snippets.

@riga
Last active March 29, 2021 10:50
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 riga/a117052615cb572292008dbe8dfff475 to your computer and use it in GitHub Desktop.
Save riga/a117052615cb572292008dbe8dfff475 to your computer and use it in GitHub Desktop.
Files and scripts required for CMS TDR documents on Overleaf

CMS TDR documents on Overleaf

This gist demonstrates how to prepare CMS TDR documents to work on Overleaf.

All of the following files should be copied to the root directory of your project:

  • build.sh: Compiles the document once and saves a tex document (and the current pdf) to a new directory, named "overleaf" by default, which can be used as a root document on Overleaf. Please note that changes to this document get lost once this build script is executed again.
  • build_docker.sh: Executes the build script in a docker container using the official tdr build image.
  • latexmkrc: Tells the LaTeX compiler on Overleaf where to find the tdr tool.

Usage

Both build.sh and build_docker.sh have the same arguments:

./build.sh        [PROJECT_NAME] [BUILD_DIR]
./build_docker.sh [PROJECT_NAME] [BUILD_DIR]

PROJECT_NAME: The name of the document, e.g. "AN-21-017". When empty,
              the name of the project directory is used.
BUILD_DIR   : The name of the build directory. Defaults to "overleaf".
#!/usr/bin/env bash
# Script that executes tdr to create the output tex and pdf files in the build directory.
action() {
local this_file="$( [ ! -z "$ZSH_VERSION" ] && echo "${(%):-%x}" || echo "${BASH_SOURCE[0]}" )"
local project_dir="$( cd "$( dirname "$this_file" )" && pwd )"
# make sure we build relative to the project directory
( cd "$project_dir" && build "$@")
}
build() {
# arguments
local project_name="${1:-$TDR_PROJECT}"
local build_dir="${2:-${TDR_DIR:-overleaf}}"
# cross platform helpers
_grep_Po() {
if [ "$( uname -s )" = "Darwin" ]; then
perl -nle "print $& if m{$1}" "${@:2}"
else
grep -Po "$@"
fi
}
_sed_i() {
if [ "$( uname -s )" = "Darwin" ]; then
sed -i "" "$@"
else
sed -i "$@"
fi
}
# run tdr and save logs
local log_file="$( mktemp )"
PATH="$PWD/utils:$PATH" tdr --style=an --temp_dir="$build_dir" build "$project_name" | tee "$log_file"
if [ "$?" != "0" ]; then
2>&1 echo "tdr script failed"
return "1"
fi
# if not already set, get the project name, i.e., the code of the note, paper, etc
if [ -z "$project_name" ]; then
project_name="$( cat "$log_file" | _grep_Po "^Building\:\s+.*\/\K([^\/\.]+)\.tex\s*$" | awk -F "." '{print $1}' )"
if [ -z "$project_name" ]; then
rm -f "$log_file"
2>&1 echo "could not extract project name"
return "2"
fi
fi
rm -f "$log_file"
# make changes to files in output directory
(
cd "$build_dir"
# remove unused files
rm -f *.{aux,bbl,blg,err,log,out,pdftex_out,bib}
# change the bibliography in the generated tex file
_sed_i "s/\\\bibliography{auto_generated}/\\\bibliography{${project_name}}/" "${project_name}_temp.tex"
# add a note to the document
_sed_i "1 i % generated with https://gist.github.com/riga/a117052615cb572292008dbe8dfff475" "${project_name}_temp.tex"
)
return "0"
}
action "$@"
#!/usr/bin/env bash
# Script that builds the document within a docker container using the build.sh script.
action() {
local this_file="$( [ ! -z "$ZSH_VERSION" ] && echo "${(%):-%x}" || echo "${BASH_SOURCE[0]}" )"
local project_dir="$( cd "$( dirname "$this_file" )" && pwd )"
# arguments
local project_name="$1"
local build_dir="${2:-overleaf}"
local abs_build_dir="$( [[ "$build_dir" != /* ]] && echo "${PWD%/}" )/$build_dir"
local container_pwd="/home/vscode/cms_paper/$( basename "$project_dir" )"
local container_build_dir="/home/vscode/cms_paper_build_dir"
# make sure the build dir exists
mkdir -p "$abs_build_dir"
# run the container
docker run \
--rm -ti \
-v "${project_dir}:${container_pwd}:ro" \
-v "${abs_build_dir}:${container_build_dir}" \
-w "${container_pwd}" \
-e "TDR_PROJECT=${project_name}" \
-e "TDR_DIR=${container_build_dir}" \
--entrypoint "bash" \
gitlab-registry.cern.ch/tdr/containers/tdrlatex \
"build.sh"
}
action "$@"
$ENV{'TEXINPUTS'}='../utils/general//:' . $ENV{'TEXINPUTS'};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment