[PYTHON] Automate the placement of LaTeX own style files

<!-Automatic placement of LaTeX own style files->

Overview

LaTeX user-defined style files enrich your life. It is said that you may be a little happy if you automate the work of placing style files in directories that are in your path.

Premise

Consider the following directory structure.

styles
|-- hoge -- hoge.sty
|-- piyo -- piyo.sty

LaTeX style files are stored in the directory with the same name like this. It is a good idea to put a README etc. in the directory.

Implementation

First, create a configuration file utilize.json in styles.

utilize.json


{
    "from": [
        "hoge",
        "piyo"
    ],
    "to": "/home/fuga/texmf/tex/latex"
}

from is a list of style names and to is the location of the style file. Next, create utility.py in styles as well.

utilize.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import subprocess
import json

full_this_dirname = os.path.dirname(os.path.abspath(__file__))
config_file = "utilize.json"

def copy(pkgname, to_dirname):
    u"""
A function that copies a specific package in a directory
    """
    #Directory copy
    #Full path of the original package
    full_org_pkgpath = os.path.join(full_this_dirname, pkgname)
    #Creating a package
    subprocess.call('sh -c "sudo cp -r {pkgname} {to_dirname}"'.format(pkgname=full_org_pkgpath, to_dirname=to_dirname), shell=True)

def main():
    with open(config_file) as fin:
        config = json.load(fin)
    for pkgname in config["from"]:
        copy(pkgname, config["to"])
    subprocess.call('sh -c "sudo mktexlsr"', shell=True)

if __name__ == "__main__":
    main()

If you execute utilize.py directly, it will read the configuration file, copy the directory, and mktexlsr. After that, if you make it executable with chmod u + x utilize.py, you can use the style file with ./utilize.py. happiness.

Recommended Posts

Automate the placement of LaTeX own style files
Change the style of matplotlib
The story of viewing media files in Django
Extract the table of image files with OneDrive & Python
Find out the location of Python class definition files.
#We will automate the data aggregation of PES! part1
Understand the attributes of Linux files (ls -l command)