marapper / gulp-mathmode

Gulp plugin for converting math LaTeX expressions to images
MIT License
2 stars 0 forks source link

grunt-mathmode

Turn LaTeX math expressions into images.

Based on mathmode.

Install

Install with npm.

npm install --save-dev gulp-mathmode

LaTeX math expressions

List of possible math

Usage Examples

Turns list of tex-files into png-images with the same names.

var srcPath = 'src/**/*.tex';
var buildPath = 'build/**/*.tex';
var format = png;

gulp.task('clean', function () {
    var clean = require('gulp-clean');
    return gulp.src(buildPath + '/*.png', {read: false})
        .pipe(clean());
});

gulp.task('math', ['clean'], function () {
    var math = require('gulp-mathmode'),
        rename = require("gulp-rename");
    gulp.src(srcPath)
        .pipe(math({
            format: format,
            dpi: 300
        }))
        .pipe(rename({extname: '.' + format}))
        .pipe(gulp.dest(buildPath));
});

Or you can turn formulas from single file like this

    #some_file.yml
    formula1: \varepsilon = \sum E
    formula2: R = \frac{1000}{L^{\frac32}+1000}

to a few images

var format = png;

gulp.task('math', ['clean'], function () {
    var through = require("through2");
    var yml = require("gulp-yml");
    var gutil = require("gulp-util");
    var math = require("gulp-mathmode");
    var rename = require("gulp-rename");

    gulp.src('some_file.yml')
        .pipe(yml().on( "error", gutil.log ))
        .pipe((function () {

            return through.obj(function (file, enc, cb) {
                "use strict";

                var json = JSON.parse(String(file.contents));
                var me = this;

                Object.keys(json).forEach(function (key) {
                    var newFile = new gutil.File();
                    newFile.filename = key + '.' + format;

                    newFile.base = file.base;
                    newFile.path = file.base + key + '.' + format;

                    newFile.contents = new Buffer(json[key]);
                    me.push(newFile);
                });
            });
        })())
        .pipe(math({
            format: format,
            dpi: 200
        }))
        .pipe(rename({extname: '.' + format}))
        .pipe(gulp.dest('build/folder'));

});

Configuration

Look at mathmode configs.

require('gulp-mathmode')({
    format: 'png',
    dpi: 300,
    packages: ["amsmath"],
    macros: "",
    pdflatex_path: "pdflatex",
    imagemagick_path: "convert"
}

Кириллица и русский текст

Установить дополнительно

sudo apt-get install texlive-latex-extra texlive-math-extra texlive-lang-cyrillic texlive-fonts-extra 

И использовать с дополнительными параметром

require('gulp-mathmode')({
    packages: [
        "amsmath", "mathtext", 
        ['fontenc', 'T1,T2A'], ['inputenc', 'utf8x'], ['babel', 'english,russian']
    ],
    macros: "",
    format: format,
    dpi: 200
})