Skip to content

Instantly share code, notes, and snippets.

@pyokagan
Created October 31, 2012 01:01
Show Gist options
  • Select an option

  • Save pyokagan/3984172 to your computer and use it in GitHub Desktop.

Select an option

Save pyokagan/3984172 to your computer and use it in GitHub Desktop.
Converts a list of code files into a latex document.
#! /usr/bin/python3
"""Converts a list of code files into a latex document."""
from argparse import ArgumentParser
import re
import os
from pygments.lexers import get_lexer_for_filename
def filter_tex(x):
def escape(x):
return '\\{}'.format(x.group(1))
return re.sub(r'([_\\])', escape, x)
header=r"""
\documentclass[a4paper,english,11pt,final]{{article}}
\usepackage{{fixltx2e}} %LaTeX patches
\usepackage{{cmap}}
\usepackage[final]{{graphicx}}
\usepackage{{tabularx}}
\usepackage[left=1cm,top=2cm,bottom=2cm,right=1cm]{{geometry}}
\usepackage{{color}}
\usepackage{{amsmath}}
\usepackage{{float}}
\usepackage{{multirow}}
\usepackage{{array}}
\floatstyle{{ruled}}
\restylefloat{{figure}}
\usepackage{{siunitx}}
\usepackage{{pdfpages}}
\usepackage{{verbatim}}
\usepackage{{cite}}
\usepackage{{minted}}
\usepackage{{fancyhdr}}
\usemintedstyle{{bw}}
\pagestyle{{fancy}}
\usepackage[marginclue,footnote]{{fixme}}
%\usepackage[mediumspace,mediumqspace, squaren]{{SIunits}}
%%Body
\begin{{document}}
\title{{{title}}}
\author{{{author}}}
\date{{}}
\maketitle
"""
block = r"""
\section{{ {path} }}
\begin{{minted}}[linenos=true]{{{filetype}}}
{content}
\end{{minted}}
"""
footer = r"""
\end{{document}}
"""
def main():
p = ArgumentParser()
p.add_argument("-t", "--title", default = os.getcwd())
p.add_argument("--author", default = '')
p.add_argument("paths", nargs = "+")
args = p.parse_args()
blocks = [block.format(path = filter_tex(x), content = open(x).read(), filetype = get_lexer_for_filename(x).aliases[0]) for x in args.paths]
content = [header.format(title = args.title, author = args.author)] + blocks + \
[footer.format()]
print("\n".join(content))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment