# SPDX-License-Identifier: MPL-2.0
# Copyright (C) 2021- The MultiPie developers
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
"""
This class manages PDF creation via LaTeX.
"""
import os
import shutil
import signal
import subprocess
import math
import numpy as np
_LATEX_TIMEOUT = 1800 # timeout for each LaTeX run in seconds.
# LaTeX special characters, and their replacements in text and math modes.
_TEX_SPECIAL = {
"\\": (r"\textbackslash{}", r"\backslash{}"),
"{": (r"\{", r"\{"),
"}": (r"\}", r"\}"),
"_": (r"\texttt{\symbol{95}}", r"\texttt{\symbol{95}}"), # underscore glyph (\_ is drawn as a rule).
"#": (r"\#", r"\#"),
"%": (r"\%", r"\%"),
"&": (r"\&", r"\&"),
"$": (r"\$", r"\$"),
"^": (r"\^{}", r"\wedge{}"),
"~": (r"\~{}", r"\sim{}"),
}
# ==================================================
[docs]
def tex_text(name, math=False):
"""
Escape LaTeX special characters in a name given by the user, e.g., model and site names.
Args:
name (str): name.
math (bool, optional): escape for math mode ? otherwise for text mode.
Returns:
- (str) -- escaped name.
"""
return "".join(_TEX_SPECIAL[c][math] if c in _TEX_SPECIAL else c for c in str(name))
# ==================================================
[docs]
class LaTeXError(Exception):
"""
Error in PDF creation via LaTeX.
"""
pass
# ==================================================
def _kill_tree(p):
"""
Kill process and all its child processes started by _run_tex.
Args:
p (subprocess.Popen): process.
"""
if os.name == "nt":
try:
subprocess.run(
["taskkill", "/F", "/T", "/PID", str(p.pid)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=30
)
except (OSError, subprocess.TimeoutExpired):
pass
if p.poll() is None:
p.kill()
else:
try:
os.killpg(p.pid, signal.SIGKILL)
except ProcessLookupError:
pass
try:
p.wait(timeout=30)
except subprocess.TimeoutExpired:
pass
# ==================================================
def _run_tex(cmd, timeout):
"""
Run TeX command, and kill all its child processes when it does not finish normally.
Args:
cmd (list): command.
timeout (float): timeout in seconds.
Returns:
- (int) -- return code.
Note:
- ptex2pdf runs TeX and dvipdfmx as child processes.
- the process tree is killed on timeout or interruption (e.g., Ctrl-C).
"""
if os.name == "nt":
opt = {"creationflags": subprocess.CREATE_NEW_PROCESS_GROUP}
else:
opt = {"start_new_session": True}
p = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **opt)
try:
return p.wait(timeout=timeout)
except BaseException:
_kill_tree(p)
raise
# ==================================================
[docs]
def wrap_string(a, left="", right=""):
"""
Wrap array elementwise with left and right strings.
Args:
a (array-like): array.
left (str, optional): left string to wrap.
right (str, optional): right string to wrap.
Returns:
- (array-like) -- wrapped array.
"""
return np.frompyfunc(lambda i: left + i + right, 1, 1)(np.array(a, dtype=str))
# ==================================================
[docs]
def regularize_table(lst2d, padding=None):
"""
Regularize an irregular table.
Args:
lst2d (list): 2d list with different number of columns.
padding (any, optional): padding value.
Returns:
- (list) -- regularized 2d list.
"""
if not isinstance(lst2d, list):
raise KeyError(f"non list type ({type(lst2d)}) is given.")
col = max(map(len, lst2d))
tbl = []
for r in lst2d:
p = [padding] * col
p[: len(r)] = r
tbl.append(p)
return tbl
# ==================================================
[docs]
def list_to_table(lst1d, col, p=None):
"""
Convert from list to table.
Args:
lst1d (list): 1d list.
col (int): number of columns.
p (any, optional): padding value (no padding for None).
Returns:
- (list) -- 2d list.
"""
if not isinstance(lst1d, list):
raise KeyError(f"non list type ({type(lst1d)}) is given.")
n = len(lst1d)
row = int(math.ceil(n / col))
tbl = []
for i in range(row):
tbl.append(lst1d[col * i : col * (i + 1)])
d = row * col - n
if p is not None and d != 0:
tbl[-1].extend([p] * d)
return tbl
# ==================================================
[docs]
class PDFviaLaTeX:
"""
PDF creation via LaTeX.
"""
# Attributes:
# __content (list): text in each line [str]
# __package (list): package [(name, option)]
# __twice (bool): compile twice ?
# __fname (str): file name without ".tex"
# __dir (str): output directory
# __opt (dict): option
# __replace (list): replace string at creation
# __pos (dict): position string
#
# ==================================================
def __init__(self, fname, package=[], style="normal", pt=10, no_page=False, landscape=False, english=False, dir=""):
"""
PDF creator class.
Args:
fname (str): file name without ".tex".
package (list, optional): package [(name,option)].
style (str, optional): margin style (narrowest/narrow/normal/wide/widest).
pt (int, optional): default point (8/9/10/11/12/14).
no_page (bool, optional): no page number ?
landscape (bool, optional): landscape ?
english (bool, optional): english mode ?
dir (str, optional): output directory.
"""
self.__content = []
self.__twice = False
self.__fname = fname
self.__dir = dir if dir else "."
self.__opt = {"style": style, "pt": pt, "no_page": no_page, "landscape": landscape, "english": english}
self.__package = [
("color", "usenames"),
("amsmath", ""),
("breqn", ""),
("graphicx", ""),
("amssymb", ""),
("bm", ""),
("enumitem", ""),
("braket", ""),
("multirow", ""),
("booktabs", ""),
("longtable", ""),
("caption", ""),
] #: LaTeX package (name, option)
self.__package += package
self.__replace = [
(r"\left[\begin{matrix}", r"\begin{pmatrix}"),
(r"\end{matrix}\right]", r"\end{pmatrix}"),
] #: replace string (target, replace)
self.__pos = {"c": "center", "l": "flushleft", "r": "flushright"}
# ==================================================
[docs]
def build(self):
"""
Create PDF.
"""
txt = self._create_source(content=self.__content, package=self.__package, **self.__opt)
for x in self.__replace:
txt = txt.replace(*x)
pdfdir = os.path.abspath(self.__dir)
cwd = os.getcwd()
os.chdir(pdfdir)
pdf = os.path.join(pdfdir, self.__fname + ".pdf")
done = False
try:
f = open(self.__fname + ".tex", mode="wt")
f.write(txt)
f.close()
ptex2pdf = shutil.which("ptex2pdf")
if ptex2pdf is None:
raise LaTeXError("ptex2pdf is not found.")
self._check_package()
# TeX options must be given as one argument of -ot. nonstopmode and closed stdin
# prevent TeX from waiting for input, e.g., when a package is missing.
cmd = [ptex2pdf, "-l", "-ot", "-synctex=0 -halt-on-error -interaction=nonstopmode", f"{self.__fname}.tex"]
if ptex2pdf.lower().endswith(".lua"): # script found via PATHEXT on Windows.
cmd = ["texlua"] + cmd
rm_file = [self.__fname + ext for ext in [".aux", ".log"]]
n_run = 2 if self.__twice else 1
for _ in range(n_run):
try:
rc = _run_tex(cmd, _LATEX_TIMEOUT)
except subprocess.TimeoutExpired:
raise LaTeXError(f"LaTeX did not finish in {_LATEX_TIMEOUT} sec. See, {self.__fname}.log")
except OSError as e:
raise LaTeXError(f"cannot run ptex2pdf: {e}")
if rc != 0:
raise LaTeXError(f"LaTeX compile error. See, {self.__fname}.log")
done = True
for rm in rm_file:
if os.path.exists(rm):
os.remove(rm)
finally:
os.chdir(cwd)
# when this run fails or is interrupted, remove the PDF, which is from an earlier run or incomplete.
if not done and os.path.exists(pdf):
try:
os.remove(pdf)
except OSError: # keep the original error.
pass
# ==================================================
def _check_package(self):
"""
Check if all LaTeX packages are installed (only when kpsewhich is available).
:meta private:
"""
if shutil.which("kpsewhich") is None:
return
# a package entry may contain several names, e.g., "amsmath,amssymb".
names = [i.strip() for name, _ in self.__package for i in name.split(",") if i.strip()]
names = list(dict.fromkeys(names))
missing = []
for name in names:
# search in the same way as platex, which is used by ptex2pdf -l.
try:
result = subprocess.run(
["kpsewhich", "-progname=platex", name + ".sty"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=60,
)
except (OSError, subprocess.TimeoutExpired):
return # cannot check, let LaTeX report errors.
if result.returncode != 0:
missing.append(name + ".sty")
if missing:
raise LaTeXError(f"LaTeX package(s) not found: {', '.join(missing)}.")
# ==================================================
def _create_source(self, content, package=[], style="normal", pt=10, no_page=False, landscape=False, english=False):
"""
Create tex source.
Args:
content (str or list): content of document.
package (list, optional): package [(name,option)].
style (str, optional): margin style (narrowest/narrow/normal/wide/widest).
pt (int, optional): default point size (8/9/10/11/12/14).
no_page (bool, optional): no page number ?
landscape (bool, optional): landscape ?
english (bool, optional): english mode ? (use US letter size).
Returns:
- (str) -- tex source.
"""
px, py = (210, 297) # A4
st = "jsarticle"
foot = 7
layout_dict = {
"narrowest": (-15, -15),
"narrow": (-5, -5),
"normal": (0, 0),
"wide": (5, 5),
"widest": (10, 10),
} # margin (width,height)
scale_dict = {8: 9.6, 9: 9.82, 10: 10.0, 11: 10.0, 12: 10.0, 14: 9.7}
mx, my = layout_dict[style]
if landscape:
px, py = py, px
px *= scale_dict[pt] / pt
py *= scale_dict[pt] / pt
tx, ty = (px - 25.4 * 2 - 2 * mx, py - 25.4 * 2 - 2 * my - foot)
package_txt = []
if package:
package_txt += ["\n%%% package\n"]
for p, o in package:
txt = r"\usepackage"
if o:
txt += "[" + o + "]"
txt += "{" + p + "}"
package_txt.append(txt)
ls = ",landscape" if landscape else ""
head = [
r"\documentclass[fleqn," + str(pt) + "pt" + ls + "]{" + st + "}",
]
if landscape:
head.append(r"\special{papersize=\the\paperwidth,\the\paperheight}")
preamble = [
"\n%%% layout setting\n",
f"\\paperwidth={px}truemm",
f"\\paperheight={py}truemm",
f"\\textwidth={tx}truemm",
f"\\textheight={ty}truemm",
f"\\oddsidemargin={mx}truemm",
f"\\topmargin={my}truemm",
f"\\footskip={foot}truemm",
"\n%%% fixed\n",
r"\hoffset=0truemm",
r"\voffset=0truemm",
r"\headheight=0truemm",
r"\headsep=0truemm",
r"\marginparsep=0truemm",
r"\marginparwidth=0truemm",
"\n%%% main text\n",
r"\begin{document}" + "\n",
r"\setcounter{MaxMatrixCols}{32}" + "\n",
]
if english:
preamble += [
r"\renewcommand{\tablename}{Table~}",
r"\renewcommand{\figurename}{Figure~}",
]
if no_page:
preamble.append(r"\pagestyle{empty}" + "\n")
postamble = ["\n" + r"\end{document}" + "\n"]
txt = "\n".join(head + package_txt + preamble + content + postamble)
return txt
# ==================================================
def _create_table(self, tbl, cols=None, math=False):
"""
Create table content.
Args:
tbl (list): content.
cols (int, optional): number of columns.
math (bool, optional): math mode ?
Returns:
- (list) -- 2d table with given cols [[str]].
- (list) -- number of divisions in each rows [int].
"""
if not isinstance(tbl, list):
raise KeyError("non-list type is given for table.")
if len(tbl) == 0:
raise KeyError("empty list is given for table.")
if not isinstance(tbl[0], list): # in case of 1d list
tbl = [tbl]
nmax = max(map(len, tbl))
if cols is None or nmax <= cols:
n = [1] * len(tbl)
t = tbl
else:
n = []
t = []
for i in tbl:
t1 = list_to_table(i, cols, "")
t.extend(t1)
n.append(len(t1))
t = regularize_table(t, "")
if math:
t = wrap_string(t, "$ ", " $")
return t, n
# ==================================================
[docs]
def title(self, title, size="LARGE", pos="c"):
"""
Add title.
Args:
title (str): title.
size (str, optional): font size (large/Large/LARGE/Huge).
pos (str, optional): position (c/l/r) (center/left/right).
"""
pos = self.__pos[pos]
txt = [r"\begin{" + pos + "}", f"\\{size}", title, r"\end{" + pos + "}"]
self.text(txt)
# ==================================================
[docs]
def text(self, content=""):
"""
Add text.
Args:
content (str or list, optional): text.
"""
if isinstance(content, list):
self.__content += content
else:
self.__content += [content]
# ==================================================
[docs]
def equation(self, eqs, num=False, long=False):
"""
Add equation.
Args:
eqs (str or list): equation or list of eqs.
num (bool, optional): with eq. number ?
long (bool, optional): long eq. ? (single eq. only).
"""
n = "" if num else "*"
if isinstance(eqs, list):
if long:
raise KeyError("long equation is unsupported.")
if not eqs:
return
txt = (
[r"\begin{align" + n + "}"] + ["& " + i + r" \\" for i in eqs[:-1]] + ["& " + eqs[-1]] + [r"\end{align" + n + "}"]
)
else:
env = "dmath" if long else "align"
txt = [r"\begin{" + env + n + "}", eqs, r"\end{" + env + n + "}"]
self.text(txt)
# ==================================================
# ==================================================
[docs]
def simple_table(self, tbl, cols=None, cpos=None, tmath=False):
"""
Add simple table.
Args:
tbl (list): content of table (1d or 2d).
cols (int, optional): number of columns.
cpos (str, optional): position of each cols. default is center.
tmath (bool, optional): math mode ?
"""
t, _ = self._create_table(tbl, cols, tmath)
if cpos is None:
cpos = "c" * len(t[0])
txt = [r"\begin{tabular}{" + cpos + "}"]
for i in t[:-1]:
txt.append(" & ".join(i) + r" \\")
txt.append(" & ".join(t[-1]))
txt.append(r"\end{tabular}")
self.text(txt)
# ==================================================
def _table(
self,
tbl,
row,
col,
rc="",
hl=[],
caption="",
stretch=1.0,
cpos=None,
rcmath=False,
rmath=False,
cmath=False,
tmath=False,
center=True,
):
"""
Add table (reshaped by using number of col).
Args:
same as self.table() except "long".
"""
cr = r" \\"
hr = r" \hline"
if rcmath and rc != "":
rc = "$ " + rc + " $"
if rmath:
row = wrap_string(row, "$ ", " $")
if cmath:
col = wrap_string(col, "$ ", " $")
txt = []
if caption:
txt.append(r"\begin{table}[ht!]")
if center:
txt.append(r"\begin{center}")
else:
txt.append(r"\captionsetup{justification=raggedright, singlelinecheck=off}")
txt.append(r"\caption{" + caption + "}")
txt.append(r"\renewcommand{\arraystretch}{" + str(stretch) + "}")
# tabular
t, n = self._create_table(tbl, len(col), tmath)
if cpos is None:
cpos = "c" * (len(col) + 1)
head = rc + " & " + " & ".join(col) + cr + hr
txt.append(r"\begin{tabular}{" + cpos + "}" + hr + hr)
txt.append(head)
ti = 0
for i in range(len(row)):
last = hr if i in hl else ""
if n[i] == 1:
txt.append(str(row[i]) + " & " + " & ".join(t[ti]) + cr + last)
else:
txt.append(str(row[i]) + " & " + " & ".join(t[ti]) + cr)
for j in range(1, n[i] - 1):
txt.append("& " + " & ".join(t[ti + j]) + cr)
txt.append("& " + " & ".join(t[ti + n[i] - 1]) + cr + last)
ti += n[i]
txt.append(hr + hr)
txt.append(r"\end{tabular}")
# end tabular
if caption:
if center:
txt.append(r"\end{center}")
txt.append(r"\end{table}")
self.text(txt)
# ==================================================
def _long_table(
self,
tbl,
row,
col,
rc="",
hl=[],
caption="",
stretch=1.0,
cpos=None,
rcmath=False,
rmath=False,
cmath=False,
tmath=False,
center=True,
):
"""
Add table (reshaped by using number of col).
Args:
same as self.table() except "long".
"""
cr = r" \\"
hr = r" \hline"
if rcmath and rc != "":
rc = "$ " + rc + " $"
if rmath:
row = wrap_string(row, "$ ", " $")
if cmath:
col = wrap_string(col, "$ ", " $")
t, n = self._create_table(tbl, len(col), tmath)
if cpos is None:
cpos = "c" * (len(col) + 1)
mc = r"\multicolumn{" + str(len(col)) + "}"
head = rc + " & " + " & ".join(col) + cr + hr
txt = []
cnt = "" if center else "[l]"
txt.append(r"\renewcommand{\arraystretch}{" + str(stretch) + "}")
txt.append(r"\begin{longtable}" + cnt + "{" + cpos + "}")
if caption:
if not center:
txt.append(r"\captionsetup{justification=raggedright, singlelinecheck=off}")
txt.append(r"\caption{" + caption + "}")
txt.append(cr)
txt.append(hr + hr)
txt.append(head + r" \endfirsthead" + "\n")
txt.append(mc + r"{l}{\tablename\ \thetable{}}" + cr)
txt.append(hr + hr)
txt.append(head + r" \endhead" + "\n")
txt.append(hr + hr)
txt.append(mc + r"{r}{\footnotesize\it continued ...}" + cr + r" \endfoot" + "\n")
txt.append(hr + hr)
txt.append(mc + "{r}{}" + cr + r" \endlastfoot" + "\n")
ti = 0
for i in range(len(row)):
last = hr if i in hl else ""
if n[i] == 1:
txt.append(str(row[i]) + " & " + " & ".join(t[ti]) + cr + last)
else:
txt.append(str(row[i]) + " & " + " & ".join(t[ti]) + cr)
for j in range(1, n[i] - 1):
txt.append("& " + " & ".join(t[ti + j]) + cr)
txt.append("& " + " & ".join(t[ti + n[i] - 1]) + cr + last)
ti += n[i]
txt.append(r"\end{longtable}")
self.text(txt)
self.__twice = True
# ==================================================
[docs]
def table(
self,
tbl,
row,
col,
rc="",
hl=[],
caption="",
stretch=1.0,
cpos=None,
rcmath=False,
rmath=False,
cmath=False,
tmath=False,
long=False,
center=True,
):
"""
Add table (reshaped by using number of col).
Args:
tbl (list): content of table [[str]].
row (list): row name [str].
col (list): column name [str].
rc (str, optional): row-col name.
hl (list, optional): position with horizontal line [int]. if True, use horizontal lines for all lines.
caption (str, optional): table caption.
stretch (float, optional): baseline stretch (ratio).
cpos (str, optional): column position. default is center.
rcmath (bool, optional): math mode for row-col name ?
rmath (bool, optional): math mode for row name ?
cmath (bool, optional): math mode for column name ?
tmath (bool, optional): math mode for table ?
long (bool, optional): long table ?
center (bool, optional): centering ?
"""
if hl is True:
hl = list(range(len(row) - 1))
if long:
self._long_table(tbl, row, col, rc, hl, caption, stretch, cpos, rcmath, rmath, cmath, tmath, center)
else:
self._table(tbl, row, col, rc, hl, caption, stretch, cpos, rcmath, rmath, cmath, tmath, center)