Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
4e04b48
new file: README.md
Oct 11, 2020
4812569
modified: displaymath2svg README.md
Oct 11, 2020
d51a163
modified: README.md
Oct 11, 2020
2d30334
modified: README.md
Oct 11, 2020
ac2e714
Privacy code auditing
Oct 11, 2020
45b55e3
modified: displaymath2svg README.md
Oct 11, 2020
a160c06
modified: README.md
Oct 11, 2020
797a596
displaymath2svg.lua test added
Oct 11, 2020
f6d72c9
displaymath2svg
Oct 11, 2020
82fa204
modified: README.md
Oct 11, 2020
8db789e
MIT License
Oct 11, 2020
113413f
MathML comment
Oct 11, 2020
3102fb5
math2svg.lua
Oct 12, 2020
1f96b0d
README.md
Oct 12, 2020
6848665
refactoring of math2svg.lua
Oct 12, 2020
2121998
pandoc.Image when output format is not HTML.
Oct 12, 2020
277c007
License
Oct 12, 2020
74a4445
install instructions
Oct 12, 2020
afdfe46
MathML newcommands
Oct 12, 2020
1516ed5
improved inline SVG
Oct 12, 2020
fbdf9ce
argumentlist & reduced description
Oct 13, 2020
a38c802
macros removed in favour of header-includes
Oct 13, 2020
dd19ae1
sample.yaml
Oct 13, 2020
c8975ea
configuration over --metadata key values
Oct 14, 2020
0d9e3e3
README.md math2svg.lua
Oct 15, 2020
e40396e
Documentation completed (#1)
stroobandt Oct 15, 2020
6c1410d
README.md
Oct 15, 2020
b52cf39
README.md
Oct 16, 2020
15ff17c
README.md
Oct 16, 2020
a0a1d58
README.md
Oct 16, 2020
db42ee6
120 column lines & math2svg_tex2svg key value
Oct 16, 2020
0d3876c
README.md privacy statement
Oct 16, 2020
237ce0c
Makefile
Oct 16, 2020
0691b00
<span class="math display">
Oct 16, 2020
02354d0
description
Oct 17, 2020
473c360
.travis.yml
Oct 17, 2020
59fd787
README.md
Oct 26, 2020
6811a49
README.md 80 columns
Oct 26, 2020
0ccfcc1
README.md 80 columns
Oct 26, 2020
4dd39ad
math2svg.lua 80 columns
Oct 26, 2020
4719dfb
Typo in README.md
stroobandt Oct 26, 2020
bb6c273
.travis.yml
Oct 26, 2020
c94c9c0
.travis.yml
Oct 26, 2020
58935cb
.travis.yml
Oct 27, 2020
55cfd17
.travis.yml
stroobandt Oct 27, 2020
4601caa
Dockerfile
stroobandt Jan 14, 2021
267b99a
Dockerfile
stroobandt Jan 14, 2021
803c7a2
year 2021
stroobandt Jan 14, 2021
f70e9c5
README.md
stroobandt Jan 16, 2021
9f2f99d
README.md
stroobandt Jan 16, 2021
1b7efba
Merge branch 'master' into master
tarleb Jan 16, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .tools/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ RUN apt-get -q --no-allow-insecure-repositories update \
lmodern \
luarocks \
make \
nodejs \
npm \
python-is-python3 \
python3 \
python3-matplotlib \
Expand All @@ -38,6 +40,9 @@ ARG plantuml_jar=plantuml.1.2018.9.jar
RUN wget https://sourceforge.net/projects/plantuml/files/${plantuml_jar} \
--quiet --output-document=/root/${plantuml_jar}
ENV PLANTUML=/root/$plantuml_jar

RUN npm install --global mathjax-node-cli

ENV DIFF="diff -u"
ENV LC_ALL="C.UTF-8"
ENV LANG="C.UTF-8"
Expand Down
17 changes: 17 additions & 0 deletions math2svg/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
DIFF ?= diff --strip-trailing-cr -u

test: sample.md math2svg.lua
@pandoc \
--mathml --lua-filter=math2svg.lua \
-M math2svg_font='Gyre-Pagella' \
--to=html sample.yaml $< \
| $(DIFF) expected.html -

expected: sample.md math2svg.lua
pandoc \
--mathml --lua-filter=math2svg.lua \
-M math2svg_tex2svg='/usr/local/bin/tex2svg' \
-M math2svg_font='Gyre-Pagella' \
--output=expected.html sample.yaml $<

.PHONY: test
258 changes: 258 additions & 0 deletions math2svg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
# math2svg


## Description

This [Lua filter][pandoc.lua-filters] for [Pandoc][pandoc] converts
[LaTeX math][latex.math] to [MathJax][mathjax] generated
[scalable vector graphics (SVG)][svg] for insertion into the output document
in a standalone manner.
SVG output is in any of the [available MathJax fonts][mathjax.fonts].

This is useful when a CSS paged media engine (such as [Prince XML][prince])
cannot process complex JavaScript as required by MathJax.
See: <https://www.print-css.rocks> for information about CSS paged media,
a [W3C standard][w3c].

No Internet connection is required when generating or viewing SVG formulas,
resulting in both absolute privacy and offline, standalone robustness.


## Requirements

First, use the package manager of your operating system to install `pandoc`,
`nodejs` and `npm`. `brew` and `choco` are recommended package managers for
respectively macOS and Windows. See: <https://pandoc.org/installing.html>

```bash
$ sudo apt install pandoc nodejs npm
$ sudo dnf install pandoc nodejs npm
$ sudo yum install pandoc nodejs npm
$ brew install pandoc nodejs npm
> choco install pandoc nodejs npm
```

Then, by means of node's package manager `npm`, install the `mathjax-node-cli`
package. This package comes with the `tex2svg` executable.
Leave out the `sudo` on Windows.

```bash
$ sudo npm install --global mathjax-node-cli
> npm install --global mathjax-node-cli
```


## Usage

To be used as a [Pandoc Lua filter][pandoc.lua-filters].
[MathML][mathml] should be set as a fallback with
the `--mathml` argument.

```bash
pandoc --mathml --filter='math2svg.lua'
```

The math2svg filter is entirely configurable over
[`--metadata` key value pairs](pandoc.metadata).
Nine configuration keys are available with sensible default values.
Hence, depending on your system and intentions, not all keys are necessarily
required.

|key|default value|
|:--|:-----------:|
|`math2svg_tex2svg`|`''`|
|`math2svg_display2svg`|`true`|
|`math2svg_inline2svg`|`false`|
|`math2svg_speech`|`false`|
|`math2svg_linebreaks`|`true`|
|`math2svg_font`|`'TeX'`|
|`math2svg_ex`|`6`|
|`math2svg_width`|`100`|
|`math2svg_extensions`|`''`|


### Key value `math2svg_tex2svg`
This string key value is only required when, on your system, the path to the
`tex2svg` executable of the `mathjax-node-cli` package is not present in the
`$PATH` environment variable.

The full path to `tex2svg` can be found with the following command on \*nix,
respectively Windows:

```bash
$ which -a tex2svg
> where tex2svg
```

### Key values `math2svg_display2svg` and `math2svg_inline2svg`
These boolean key values specify whether display math, respectively inline math,
should be converted to [SVG][svg] by the filter.
The defaults convert display math to SVG, whereas inline math falls back to
[MathML][mathml] when `--mathml` was specified at `pandoc` evocation.
These defaults offer the following benefits:

- MathML output gets generated much faster than SVG output.
- Moreover, MathML is well suited for inline math as line heights are kept
small.


### Key value `math2svg_speech`
This boolean key value controls whether textual annotations for speech
generation are added to SVG formula. The default is `false`.

### Key value `math2svg_linebreaks`
This boolean key value automatic switches automatic line breaking.
The default is `true`.


### Key value `math2svg_font`
This string key value allows to specify a [MathJax font][mathjax.fonts]
different from the default `'TeX'` font.
The string should correspond to the local directory name of the font in the
`mathjax-node-cli` installation directory.
For example, the key value string for the font in
`/usr/local/lib/node_modules/mathjax-node-cli/node_modules/mathjax/fonts/HTML-CSS/Gyre-Pagella/`
would simply be `Gyre-Pagella`.


### Key value `math2svg_ex`
This positive integer key value sets the `ex` unit in pixels.
The default value is `6` pixels.


### Key value `math2svg_width`
This positive integer key value sets the container width in `ex` units for line
breaking and tags. The default value is `100` ex.


### Key value `math2svg_extensions`
This string key value allows to load one or more comma separated
[MathJax extensions for TeX and LaTeX][mathjax.tex.ext] present on the system.
These MathJaX extensions reside in a subdirectory of the `mathjax-node-cli`
installation directory.

Take for example, the installation directory of the extensions is
`/usr/local/lib/node_modules/mathjax-node-cli/node_modules/mathjax/unpacked/extensions/`
It contains a subdirectory `TeX` with the extension file `AMSmath.js`.
This MathJaX extension can be loaded by specifying the string `'TeX/AMSmath'`
as the value of the `math2svg_extensions` key.


### Adding `header-includes`
It might turn out useful to systematically include LaTeX macros, for example as
shown below, a series of `\newcommand`.

```latex
---
header-includes: |
\newcommand{\j}{\text{j}}
\newcommand{\e}[1]{\,\text{e}^{#1}}
...
```

This may be achieved either by adding a [YAML][yaml] block with the
[`header-includes`][pandoc.header-includes] key value at the top of the input
document, or by having a separate YAML document loaded before the input
document. In the latter case, simply evoke `pandoc` as follows:

```bash
pandoc --mathml --filter='math2svg.lua' header-includes.yaml input.md
```


## Adding equation numbers with CSS

When automatic equation numbers are desired, these need to be added using
[cascading style sheets (CSS)](https://en.wikipedia.org/wiki/CSS),
as JavaScript is not available in CSS paged media.

Below example CSS code centers display math whilst adding equation numbers
to the right.

```css
span.math.display {
display: flex;
justify-content: flex-end;
align-items: center;
}

span.math.display svg {
margin: 0 auto;
max-width: 85%;
}

span.math.display:after {
counter-increment: equation;
content: '(' counter(equation) ')';
font-weight: bold;
}
```


## Privacy

No Internet connection is established when creating MathJax SVG code using
the `tex2svg` command of [`mathjax-node-cli`][mathjax.node.cli].
Nor will any Internet connection be established when viewing an SVG formula.

Hence, formulas in SVG can be created and viewed offline whilst remaining
private.

For code auditing, see also:

- <https://github.com/mathjax/MathJax-node>
- <https://github.com/pkra/mathjax-node-sre>
- <https://github.com/mathjax/mathjax-node-cli>


## License

Copyright (c) 2020-2021 Serge Y. Stroobandt

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


## Contact

```bash
$ echo c2VyZ2VAc3Ryb29iYW5kdC5jb20K |base64 -d
```


[latex.math]: https://en.wikibooks.org/wiki/LaTeX/Mathematics

[mathjax]: https://www.mathjax.org/
[mathjax.fonts]: https://docs.mathjax.org/en/latest/output/fonts.html
[mathjax.node.cli]: https://github.com/mathjax/mathjax-node-cli
[mathjax.tex.ext]: https://docs.mathjax.org/en/latest/input/tex/extensions.html
[mathml]: https://en.wikipedia.org/wiki/MathML

[pandoc]: https://pandoc.org/
[pandoc.header-includes]: https://pandoc.org/MANUAL.html#metadata-blocks
[pandoc.lua-filters]: https://pandoc.org/lua-filters.html
[pandoc.metadata]: https://pandoc.org/MANUAL.html#reader-options
[prince]: https://www.princexml.com

[svg]: https://en.wikipedia.org/wiki/Scalable_Vector_Graphics

[w3c]: https://www.w3.org/TR/css-page-3/

[yaml]: https://en.wikipedia.org/wiki/YAML
Loading