├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── example ├── simple.js ├── simple.png └── texerrors.js ├── mathmode.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* 16 | .DS_Store -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* 16 | .DS_Store 17 | example/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2013 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | mathmode 2 | ======== 3 | This node.js module turns LaTeX math mode expressions into images. 4 | 5 | Example 6 | ======= 7 | The following one-liner will write a png image of the famous Euler identity to stdout: 8 | 9 | require("mathmode")("e^{i \\pi} = -1").pipe(process.stdout) 10 | 11 | Here is what the result should look like: 12 | 13 | 14 | 15 | Installation 16 | ============ 17 | 18 | This code uses the `preview` LaTeX environment and GraphicsMagick, which you will need to install. To do this on Debian, use the following command: 19 | 20 | sudo apt-get install texlive texlive-latex-extra imagemagick 21 | 22 | On OS X, you can do this using MacPorts: 23 | 24 | sudo port install texlive texlive-latex-extra imagemagick 25 | 26 | I have no idea how to do any of this on Windows or if this package will even work at all in that environment. If you do know how to use Windows, please open an issue/pull request with a fix. 27 | 28 | Once all that is done, you can then install the npm package directly: 29 | 30 | npm install mathmode 31 | 32 | There is only one function in the module, which is the following: 33 | 34 | 35 | `require("mathmode")(expr[, options])` 36 | -------------------------------------- 37 | 38 | Turns the LaTeX expression `expr` into an image encoded as a stream. You can tweak this behavior a bit by specifying extra parameters via the `options` struct: 39 | 40 | * `dpi`: Dots-per-inch determines the resolution of resulting image. Default: 300 41 | * `format`: Resulting image format. Default `png` 42 | * `packages`: A list of extra LaTeX packages to include. Default `["amsmath"]` 43 | * `macros`: A list of LaTeX preprocessor macros. Default `""` 44 | * `pdflatex_path`: A path the `pdflatex` executable. Default: `pdflatex` 45 | * `imagemagick_path`: A path to Graphics Magic. Default `convert` 46 | 47 | The result is a stream object for the resulting image. 48 | 49 | 50 | Acknowledgements 51 | ================ 52 | (c) 2013 Mikola Lysenko. MIT License 53 | -------------------------------------------------------------------------------- /example/simple.js: -------------------------------------------------------------------------------- 1 | require("../mathmode.js")("e^{i \\pi} = -1").pipe(process.stdout) -------------------------------------------------------------------------------- /example/simple.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mikolalysenko/mathmode/b832a71aa39c341567b2326ce4b65d8bb93fb07f/example/simple.png -------------------------------------------------------------------------------- /example/texerrors.js: -------------------------------------------------------------------------------- 1 | require("../mathmode.js")("\\thiswillcra%$$16\\]sh").on("error", function(err) { 2 | console.log("Got error: ", err.toString()); 3 | }); -------------------------------------------------------------------------------- /mathmode.js: -------------------------------------------------------------------------------- 1 | var latex = require("latex"); 2 | var spawn = require("child_process").spawn; 3 | 4 | function sanitize(expr) { 5 | var sanitized = expr.replace(/([^\\](\\\\)*)(\$)/g, "$1"); 6 | if(sanitized.charAt(0) === "$") { 7 | return sanitized.substr(1) 8 | } 9 | return sanitized 10 | } 11 | 12 | module.exports = function(expr, options) { 13 | if(!options) { 14 | options = {}; 15 | } 16 | var format = options.format || "png"; 17 | var size = options.dpi || 300; 18 | 19 | var package_list = options.packages || [ "amsmath" ]; 20 | var packages = []; 21 | for(var i=0; i