Commit cfe30efd authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

newcommands

parent c11566f0
......@@ -72,6 +72,10 @@ class converter:
# others
self.__cmds[r'\frac'] = latexfuntypes.latexfun(self.__latexfun_frac,2)
# newcommands
for nc in data.newcommands:
self.add_newcommand(nc)
# config section
self.allow_zw = True
self.allow_combinings = True
......@@ -210,6 +214,39 @@ class converter:
return expr + comb[0]
return comb[1] + '(' + expr + ')'
def add_newcommand(self, one_newcommand):
parsed = parser.parse(one_newcommand)
if not (len(parsed) in (3,6)):
raise LatexSyntaxError
ok=False
if parsed[0][0] == 'cmd':
if parsed[0][1] in (
r'\newcommand',
r'\renewcommand',
r'\def'):
ok=True
if not ok:
raise LatexSyntaxError
nargs = 0
if len(parsed)==6:
if parsed[2] == ('char','[') and parsed[4] == ('char',']'):
nargs = int(parsed[3][1])
else:
raise LatexSyntaxError
cmdname = parsed[1][1]
cmdexpr = parsed[-1][1]
def thefun(args):
expr = cmdexpr
for i in range(len(args)):
expr = regex.sub('#%i'%(i+1),args[i],expr)
return self.convert(expr)
self.__cmds[cmdname] = latexfuntypes.latexfun(
lambda x: thefun(x),
nargs)
return None
def __latexfun_frac(self,inputs):
a = inputs[0]
b = inputs[1]
......
......@@ -985,3 +985,12 @@ combinings = {
r'\vec': ('\u20d7','vec'),
r'\underline': ('\u0332','underline'),
}
newcommands = (
r'\newcommand\mathcal[1]{\cal{#1}}',
r'\newcommand\mathit[1]{\it{#1}}',
r'\newcommand\mathbf[1]{\bf{#1}}',
r'\newcommand\mathbb[1]{\bb{#1}}',
r'\newcommand\mathmono[1]{\mono{#1}}',
r'\newcommand\mathfrak[1]{\frak{#1}}',
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment