卒論環境周り

f:id:mizukisonoko:20170309115720p:plain

無事提出!ありがとうございました。

論文

背景 & 環境

卒論はLatexが必須だった気がするのでLatexで書く。
ただ、学内のTemplateを使ってCompileしないといけなかったりして
しんどいのでMarkdownで自動生成&Deployする環境を作った。

使ったもの

学内環境でCompileしないといけなかったのとたまたまFabricを別のやつで使っていたので使った。

code

# -*- coding: utf-8 -*-
from fabric.api import *
from fabric.colors import *
from fabric.operations import *
 
from config import passwd, user
import os
 
env.hosts = ["***.ac.jp"]
env.port =  22
env.user =  user
env.password = passwd
env.tmp_filename_prefix = "temporary"
env.thesis_filename = "thesis.tex"
env.template = "thesis_template.tex"
 
@task
def compile():
  print(magenta("+++++++++++++++++++"))
  print(magenta("+ *    *       *  +"))
  print(magenta("+     Start!      +"))
  print(magenta("+  *            * +"))
  print(magenta("+++++++++++++++++++"))
 
  print(yellow("++++++++++++++++++++++++++"))
  print(yellow("+  Remove previous files +"))
  print(yellow("++++++++++++++++++++++++++"))
  if os.path.exists(env.tmp_filename_prefix + ".tex"):
    os.remove(env.tmp_filename_prefix + ".tex")
  if os.path.exists(env.thesis_filename):
    os.remove(env.thesis_filename)
 
  print(blue("++++++++++++++++++++++++++++++"))
  print(blue("+  Convert markdown -> latex +"))
  print(blue("++++++++++++++++++++++++++++++"))
  local("pandoc thesis.md -o " + env.thesis_filename)
 
  print(magenta("+++++++++++++++++++"))
  print(magenta("+ Open template ! +"))
  print(magenta("+++++++++++++++++++"))
  template = open(env.template, "r").read()
 
  print(cyan("+++++++++++++++++++"))
  print(cyan("+ Open thesis !!  +"))
  print(cyan("+++++++++++++++++++"))
  thesis = open(env.thesis_filename, "r").read()
  tex = open(env.tmp_filename_prefix + ".tex","w")
 
  print(blue("+++++++++++++++++++"))
  print(blue("+ Write thesis !! +"))
  print(blue("+++++++++++++++++++"))
  tex.write(template
    .replace("@THESIS",
        thesis
            .replace(r"\tightlist","")
            .replace(r"\begin{verbatim}", r"\begin{lstlisting}[basicstyle=\ttfamily\footnotesize, frame=single]")
            .replace(r"\end{verbatim}", r"\end{lstlisting}")
            .replace(r".png}", r".eps}")
            .replace(r"\includegraphics",r"\includegraphics[clip,keepaspectratio, width = 8.5cm]")
            .replace(r"\begin{figure}",r"\begin{figure}[h]")
    )
  )
 
  tex.close()
  run("rm ~/thesis/"+ env.tmp_filename_prefix + ".*", warn_only=True)
  run("rm ~/thesis/images/*", warn_only=True)
  put("images/*.png", "~/thesis/images/")
  put(env.tmp_filename_prefix + ".tex", "~/thesis/")
  with cd("~/thesis"):
    with cd("images"):
      run("convert pic1{.png,.eps}")
      run("convert arch{.png,.eps}")
    run("/usr/local/texlive/bin/latex "+ env.tmp_filename_prefix + ".tex")
    run("dvipdfmx "+ env.tmp_filename_prefix +".dvi")
    get(env.tmp_filename_prefix + ".pdf",".")

やってること

thesis.mdをPandocでTexにする
templateに埋め込む
一部の構文を置き換える
できたLatexFileを学内環境に送りCompile、手元に持ってくる
( git add & commit & push)

やってみて 

大体うまく行った。
URL、Table、Code類はReplaceを組み合わせる事でうまくいく