From 08d8f67dda8ef018ad66e096d2cec38e031b4eae Mon Sep 17 00:00:00 2001 From: Jean-Benoist Leger Date: Sat, 31 Dec 2016 10:15:34 +0100 Subject: [PATCH] doc newcommand --- __init__.py | 11 ++++++++++- conv.py | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index 224ea6b..8d41f8d 100644 --- a/__init__.py +++ b/__init__.py @@ -29,7 +29,7 @@ LaTeX math to Unicode text converter flatlatex is a basic converter from LaTeX math to human readable text math using unicode characters. -Example: +Basic example: >>> import flatlatex >>> c = flatlatex.converter() @@ -37,6 +37,15 @@ Example: ... r'\\forall \\eta>0\\, \\exists n\\in\\mathbb{N}\\, \\forall i>n\\, |u_i-n|<\\eta') '∀η>0 ∃n∊ℕ ∀i>n |uᵢ-n|<η' +Commands can be added with LaTeX syntax: + + >>> import flatlatex + >>> c = flatlatex.converter() + >>> c.add_newcommand(r'\\newcommand\\prob{\\mathbb{P}}') + >>> c.add_newcommand(r'\\newcommand\\binom[2]{\\frac{#2!}{#1!(#2-#1)!}}') + >>> c.convert(r'\\prob(X=k)\\,=\\,\\binom{k}{n}\\times p^k(1-p)^{n-k}') + 'ℙ(X=k) = (n!)/(k!(n-k)!)×pᵏ(1-p)ⁿ⁻ᵏ' + The behavior can be change: >>> import flatlatex diff --git a/conv.py b/conv.py index 4769e9a..e0dfb66 100644 --- a/conv.py +++ b/conv.py @@ -215,6 +215,15 @@ class converter: return comb[1] + '(' + expr + ')' def add_newcommand(self, one_newcommand): + """Add a command definiton using LaTeX syntax. + + :param expr: a valid \\newcommand (or \\renewcommand or \\def) + definition. + Examples: + - r'\\newcommand\\prob{\\mathbb{P}}' + - r'\\newcommand\\binom[2]{\\frac{#2!}{#1!(#2-#1)!}}' + """ + parsed = parser.parse(one_newcommand) if not (len(parsed) in (3,6)): raise LatexSyntaxError -- GitLab