From 266b1e3ae64934b4ddf3ea49f961f386cd5f8b59 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 18 Jun 2018 15:09:13 +0200 Subject: [PATCH] m.math: ability to color equation parts using m.css color classes. --- pelican-plugins/latex2svgextra.py | 42 ++- pelican-plugins/m/plots.py | 4 +- .../m/test/math/page-code-fallback.html | 2 + pelican-plugins/m/test/math/page.html | 243 +++++++++++------- pelican-plugins/m/test/math/page.rst | 6 + 5 files changed, 191 insertions(+), 106 deletions(-) diff --git a/pelican-plugins/latex2svgextra.py b/pelican-plugins/latex2svgextra.py index c442e1ac..a93be7af 100644 --- a/pelican-plugins/latex2svgextra.py +++ b/pelican-plugins/latex2svgextra.py @@ -43,11 +43,31 @@ params.update({ \usepackage{amssymb} \usepackage{gensymb} \usepackage{newtxtext} + +\usepackage{xcolor} +\definecolor{m-default}{HTML}{cafe03} +\definecolor{m-primary}{HTML}{cafe04} +\definecolor{m-success}{HTML}{cafe05} +\definecolor{m-warning}{HTML}{cafe06} +\definecolor{m-danger}{HTML}{cafe07} +\definecolor{m-info}{HTML}{cafe08} +\definecolor{m-dim}{HTML}{cafe09} """, # Zoom the letters a bit to match page font size 'dvisvgm_cmd': 'dvisvgm --no-fonts -Z 1.25', }) +# Mapping from color codes to CSS classes. Keep in sync with m.plots. +_class_mapping = { + ('fill=\'#cafe03\'', 'class=\'m-default\''), + ('fill=\'#cafe04\'', 'class=\'m-primary\''), + ('fill=\'#cafe05\'', 'class=\'m-success\''), + ('fill=\'#cafe06\'', 'class=\'m-warning\''), + ('fill=\'#cafe07\'', 'class=\'m-danger\''), + ('fill=\'#cafe08\'', 'class=\'m-info\''), + ('fill=\'#cafe09\'', 'class=\'m-dim\'') +} + _patch_src = re.compile(r"""<\?xml version='1.0' encoding='UTF-8'\?> @@ -126,17 +146,12 @@ def pickle_cache(file): pickle.dump(cache_to_save, f) # Patches the output from dvisvgm -# - w/o the XML preamble and needless xmlns attributes -# - unique element IDs (see `counter`) -# - adjusts vertical align if depth is not none -# - adds additional `attribs` to the element def patch(formula, svg, depth, attribs): - global counter - counter += 1 - + # patch away XML preamble and needless attributes, convert `pt` to `em`, + # add vertical align for inline formulas and add additional attribs like + # CSS classes to the element if depth is None: style = '' else: style = ' vertical-align: -{:.3f}em;'.format(depth*1.25) - def repl(match): return _patch_dst.format( width=pt2em*float(match.group('width')), @@ -145,5 +160,14 @@ def patch(formula, svg, depth, attribs): viewBox=match.group('viewBox'), attribs=attribs, formula=html.escape(formula)) + svg = _patch_src.sub(repl, svg) + + # Make element IDs unique + global counter + counter += 1 + svg = _unique_src.sub(_unique_dst.format(counter=counter), svg) + + # Replace color codes with CSS classes + for src, dst in _class_mapping: svg = svg.replace(src, dst) - return _unique_src.sub(_unique_dst.format(counter=counter), _patch_src.sub(repl, svg)) + return svg diff --git a/pelican-plugins/m/plots.py b/pelican-plugins/m/plots.py index 1d09cf97..42f95dc0 100644 --- a/pelican-plugins/m/plots.py +++ b/pelican-plugins/m/plots.py @@ -62,7 +62,7 @@ mpl.rcParams['figure.autolayout'] = True # so it relayouts everything to fit # Gets increased for every graph on a page to (hopefully) ensure unique SVG IDs mpl.rcParams['svg.hashsalt'] = 0 -# Color codes for bars +# Color codes for bars. Keep in sync with latex2svgextra. style_mapping = { 'default': '#cafe03', 'primary': '#cafe04', @@ -108,7 +108,7 @@ _class_mapping = [ # Title text ('style="fill:#cafe02;font-family:{font};font-size:13px;font-style:normal;font-weight:normal;', 'class="m-title" style="'), - # Bar colors + # Bar colors. Keep in sync with latex2svgextra. ('style="fill:#cafe03;"', 'class="m-bar m-default"'), ('style="fill:#cafe04;"', 'class="m-bar m-primary"'), ('style="fill:#cafe05;"', 'class="m-bar m-success"'), diff --git a/pelican-plugins/m/test/math/page-code-fallback.html b/pelican-plugins/m/test/math/page-code-fallback.html index 73e55724..9c1869d1 100644 --- a/pelican-plugins/m/test/math/page-code-fallback.html +++ b/pelican-plugins/m/test/math/page-code-fallback.html @@ -25,6 +25,8 @@

Inline colored math a^2 and colored math block:

a^2 + b^2 = c^2
+

Colored parts of inline b^2 - \color{m-info}{4ac} and block formulas:

+
\frac{-b \pm \color{m-success} \sqrt{D}}{2a}

Properly align huge formulas vertically on a line: \hat q^{-1} = \frac{\hat q^*}{|\hat q|^2} and make sure there's enough space for all the complex W things between diff --git a/pelican-plugins/m/test/math/page.html b/pelican-plugins/m/test/math/page.html index bffc2b62..8a2a3c81 100644 --- a/pelican-plugins/m/test/math/page.html +++ b/pelican-plugins/m/test/math/page.html @@ -60,38 +60,91 @@ a^2 + b^2 = c^2 +

Colored parts of inline + +b^2 - \color{m-info}{4ac} + + + + + + + + + + + + + + + + + + + + and block formulas:

+
+ + +\frac{-b \pm \color{m-success} \sqrt{D}}{2a} + + + + + + + + + + + + + + + + + + + + + + + + + +

Properly align huge formulas vertically on a line: \hat q^{-1} = \frac{\hat q^*}{|\hat q|^2} - - - - - - - - - - + + + + + + + + + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + and make sure there's enough space for all the complex @@ -99,10 +152,10 @@ and make sure there's enough space for all the complex - + - - + + things between the lines @@ -110,30 +163,30 @@ the lines @@ -141,24 +194,24 @@ W = \sum_{i=0}^{n} \frac{w_i}{h_i} Y = \sum_{i=0}^{n} B - - - - - - - - + + + + + + + + - - - - - - - - - + + + + + + + + +

The \cfrac thing doesn't align well: @@ -166,29 +219,29 @@ Y = \sum_{i=0}^{n} B W = \sum_{i=0}^{n} \cfrac{w_i}{h_i} - - - - - - - - - + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - - + +

Properly escape the formula source:

@@ -200,14 +253,14 @@ W = \sum_{i=0}^{n} \cfrac{w_i}{h_i} \end{array} - - - + + + - - - - + + + + @@ -217,4 +270,4 @@ W = \sum_{i=0}^{n} \cfrac{w_i}{h_i} - + \ No newline at end of file diff --git a/pelican-plugins/m/test/math/page.rst b/pelican-plugins/m/test/math/page.rst index c1867088..7356b017 100644 --- a/pelican-plugins/m/test/math/page.rst +++ b/pelican-plugins/m/test/math/page.rst @@ -13,6 +13,12 @@ Inline colored math :math-primary:`a^2` and colored math block: a^2 + b^2 = c^2 +Colored parts of inline :math:`b^2 - \color{m-info}{4ac}` and block formulas: + +.. math:: + + \frac{-b \pm \color{m-success} \sqrt{D}}{2a} + Properly align *huge* formulas vertically on a line: :math:`\hat q^{-1} = \frac{\hat q^*}{|\hat q|^2}` and make sure there's enough space for all the complex :math:`W` things between -- 2.30.2