aboutsummaryrefslogtreecommitdiff
path: root/scripts/tex.py
blob: 2fd51d89ba93bbbd7d49731caaf762dd2990a424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# utility function for converting latex code

def group(*args):
  return "".join("{" + arg + "}" for arg in args)

def string(content):
  return r"\string" + content

def cmd(*args):
  name = args[0]
  args = args[1:]
  if len(args) == 0: args = [""]
  return f"\\{name}" + group(*args)

def csdef(*args):
  return r"\def" + cmd(*args)

def auxout(content):
  return r"\write\@auxout" + group(content)

def scmd(*args):
  return string(cmd(*args))

def env(name, *args):
  content = args[-1]
  args = args[0:-1]
  out = f"\\begin{{{name}}}"
  if len(args) > 0:
    out += group(*args)
  out += content
  out += f"\\end{{{name}}}"
  return out

def esc(plain):
  plain = plain.replace("\\", "\\string\\")
  plain = plain.replace("#", "\\#")
  plain = plain.replace("$", "\\$")
  plain = plain.replace("%", "\\%")
  return plain

def tabrule(*cells):
  return "&".join(cells) + "\\\\"

def label2ref(*labels):
  return ",".join(["req:" + label.replace('.', ':') for label in labels])