Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sharmaeklavya2
Last active October 5, 2022 03:09
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 sharmaeklavya2/874584aff11025ad2dcb942f70aec4e7 to your computer and use it in GitHub Desktop.
Save sharmaeklavya2/874584aff11025ad2dcb942f70aec4e7 to your computer and use it in GitHub Desktop.
Downloads style/class files necessary to compile a LaTeX paper.
#!/usr/bin/env python3
"""Downloads style/class files necessary to compile a LaTeX paper."""
import sys
import os
from os.path import join as pjoin
import argparse
from urllib.request import urlopen
from zipfile import ZipFile
CONFIGS = {
'lipics': {
'version': '2021.1.2',
'zip_url': 'https://submission.dagstuhl.de/styles/download-tag/lipics/v2021.1.2/authors/zip', # noqa
'zip_fname': 'authors-lipics-v2021.zip',
'files': {
'lipics-v2021.cls': None,
'lipics-logo-bw.pdf': None,
'cc-by.pdf': None,
'orcid.pdf': None,
},
},
'sn-jnl': {
'zip_url': 'https://media.springernature.com/full/springer-cms/rest/v1/content/18782940/data/Download+the+journal+article+template+package', # noqa
'zip_fname': 'sn-article-template.zip',
'files': {
'sn-article-template/sn-jnl.cls': 'sn-jnl.cls',
'sn-article-template/bst/sn-mathphys.bst': 'sn-mathphys.bst',
},
},
}
def preprocess_configs():
for config in CONFIGS.values():
files_dict = config['files']
for k, v in files_dict.items():
if v is None:
files_dict[k] = k
preprocess_configs()
def check_and_download(fpath, url):
if os.path.exists(fpath):
print(fpath, 'found', file=sys.stderr)
else:
print('downloading', url, file=sys.stderr)
with urlopen(url) as fp:
data = fp.read()
with open(fpath, 'wb') as fp:
fp.write(data)
def put_files(config, out_dir):
out_dir = out_dir or ''
found = True
for fname in config['files'].values():
if not os.path.exists(pjoin(out_dir, fname)):
found = False
print(pjoin(out_dir, fname), 'not found; retrieval/extraction needed', file=sys.stderr)
break
if found:
print('all files found', file=sys.stderr)
return
zip_fpath = pjoin(out_dir, config['zip_fname'])
check_and_download(zip_fpath, config['zip_url'])
with ZipFile(zip_fpath) as zfp:
for zfname, ufname in config['files'].items():
with zfp.open(zfname, 'r') as fp:
data = fp.read()
with open(pjoin(out_dir, ufname), 'wb') as fp:
fp.write(data)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('source', choices=CONFIGS.keys())
parser.add_argument('-o', help="output directory (default: cwd)")
args = parser.parse_args()
put_files(CONFIGS[args.source], args.o)
if __name__ == '__main__':
main()
@sharmaeklavya2
Copy link
Author

sharmaeklavya2 commented Jul 3, 2022

Usually, external sources like LIPIcs and SpringerNature offer a zip file for download. This zip file contains the style/class files that you need, but you'd need to figure out where to place these files, and the zip file also contains other files that are not needed for compilation but clutter your working directory. Also, if you work on multiple computers, you'd need to find the URLs to the zip files and perform all these steps again. This script works around these problems by automating the process of downloading the zip file, extracting only the files that are necessary, and placing them at the right path in your working directory.

Place (or symlink) this file in the directory containing your LaTeX source files. Run this file with a single command-line argument specifying the external source. Run this file with --help for more info about command-line use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment