#!/usr/bin/env python """Markdown -> LaTeX fragment via pandoc, with Unicode math symbols mapped to LaTeX (for appendix inclusion).""" import subprocess, sys src, dst = sys.argv[1], sys.argv[2] # markdown pipe tables -> bullet lists (longtable output of pandoc clashes with the caption package) lines, out, in_tab, header = open(src).read().splitlines(), [], False, None for ln in lines: if ln.startswith("|"): cells = [c.strip() for c in ln.strip().strip("|").split("|")] if all(set(c) <= set("-: ") for c in cells): continue if not in_tab: in_tab, header = True, cells continue out.append("- " + "; ".join(f"**{h}**: {c}" for h, c in zip(header, cells) if c and c != "—")) else: in_tab = False out.append(ln) md = "\n".join(out) tex = subprocess.run(["pandoc", "-f", "markdown", "-t", "latex", "--wrap=none"], input=md, capture_output=True, text=True).stdout rep = {"≤": r"$\leq$", "≥": r"$\geq$", "→": r"$\rightarrow$", "←": r"$\leftarrow$", "×": r"$\times$", "−": "--", "≈": r"$\approx$", "±": r"$\pm$", "σ": r"$\sigma$", "λ": r"$\lambda$", "κ": r"$\kappa$", "η": r"$\eta$", "Δ": r"$\Delta$", "δ": r"$\delta$", "ε": r"$\varepsilon$", "ĝ": r"$\hat g$", "ĉ": r"$\hat c$", "σ̂": r"$\hat\sigma$", "⇒": 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$"} for k, v in rep.items(): tex = tex.replace(k, v) tex = tex.replace(r"\section{", r"\subsection*{").replace(r"\subsection{", r"\subsubsection*{") open(dst, "w").write(tex)