1#!/usr/bin/env python2"""Markdown -> LaTeX fragment via pandoc, with Unicode math symbols mapped to LaTeX (for appendix inclusion)."""3import subprocess, sys4src, dst = sys.argv[1], sys.argv[2]5# markdown pipe tables -> bullet lists (longtable output of pandoc clashes with the caption package)6lines, out, in_tab, header = open(src).read().splitlines(), [], False, None7for ln in lines:8 if ln.startswith("|"):9 cells = [c.strip() for c in ln.strip().strip("|").split("|")]10 if all(set(c) <= set("-: ") for c in cells):11 continue12 if not in_tab:13 in_tab, header = True, cells14 continue15 out.append("- " + "; ".join(f"**{h}**: {c}" for h, c in zip(header, cells) if c and c != "—"))16 else:17 in_tab = False18 out.append(ln)19md = "\n".join(out)20tex = subprocess.run(["pandoc", "-f", "markdown", "-t", "latex", "--wrap=none"], input=md, capture_output=True, text=True).stdout21rep = {"≤": r"$\leq$", "≥": r"$\geq$", "→": r"$\rightarrow$", "←": r"$\leftarrow$", "×": r"$\times$", "−": "--", "≈": r"$\approx$", "±": r"$\pm$",22 "σ": r"$\sigma$", "λ": r"$\lambda$", "κ": r"$\kappa$", "η": r"$\eta$", "Δ": r"$\Delta$", "δ": r"$\delta$", "ε": r"$\varepsilon$", "ĝ": r"$\hat g$", "ĉ": r"$\hat c$", "σ̂": r"$\hat\sigma$",23 "⇒": r"$\Rightarrow$", "∈": r"$\in$", "≠": r"$\neq$", "·": r"$\cdot$", "²": r"$^2$", "…": r"\ldots{}", "—": "---", "–": "--", "’": "'", "“": "``", "”": "''", "ρ": r"$\rho$", "α": r"$\alpha$", "β": r"$\beta$", "γ": r"$\gamma$", "µ": r"$\mu$", "μ": r"$\mu$", "∅": r"$\varnothing$"}24for k, v in rep.items():25 tex = tex.replace(k, v)26tex = tex.replace(r"\section{", r"\subsection*{").replace(r"\subsection{", r"\subsubsection*{")27open(dst, "w").write(tex)28