Skip to content

Commit

Permalink
toTex: Escape special characters in strings
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Jan 31, 2018
1 parent 94ab164 commit 9126130
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
3 changes: 2 additions & 1 deletion lib/expression/node/ConstantNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var getType = require('../../utils/types').type;
var stringify = require('../../utils/string').stringify;
var escape = require('../../utils/string').escape;
var escapeLatex = require('../../utils/latex').escape;

function factory (type, config, load, typed) {
var register = load(require('./compile')).register;
Expand Down Expand Up @@ -226,7 +227,7 @@ function factory (type, config, load, typed) {
index;
switch (this.valueType) {
case 'string':
return '\\mathtt{' + stringify(value) + '}';
return '\\mathtt{' + escapeLatex(stringify(value)) + '}';

case 'number':
index = value.toLowerCase().indexOf('e');
Expand Down
18 changes: 10 additions & 8 deletions lib/utils/latex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var escape_latex = require('escape-latex')

exports.symbols = {
// GREEK LETTERS
Alpha: 'A', alpha: '\\alpha',
Expand Down Expand Up @@ -80,6 +82,10 @@ var units = {
deg: '^\\circ'
};

exports.escape = function (string) {
return escape_latex(string, {'preserveFormatting': true});
}

//@param {string} name
//@param {boolean} isUnit
exports.toSymbol = function (name, isUnit) {
Expand All @@ -88,17 +94,13 @@ exports.toSymbol = function (name, isUnit) {
if (units.hasOwnProperty(name)) {
return units[name];
}
return '\\mathrm{' + name + '}';

return '\\mathrm{' + exports.escape(name) + '}';
}

if (exports.symbols.hasOwnProperty(name)) {
return exports.symbols[name];
}
else if (name.indexOf('_') !== -1) {
//symbol with index (eg. alpha_1)
var index = name.indexOf('_');
return exports.toSymbol(name.substring(0, index)) + '_{'
+ exports.toSymbol(name.substring(index + 1)) + '}';
}
return name;

return exports.escape(name);
};
6 changes: 6 additions & 0 deletions test/expression/node/ConstantNode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,10 @@ describe('ConstantNode', function() {
assert.equal(n.toTex({handler: customFunction}), 'const\\left(1, number\\right)');
});

it('should escape strings in toTex', function () {
var n = new ConstantNode('space tab\tunderscore_bla$/', 'string');

assert.equal(n.toTex(), '\\mathtt{"space~tab\\qquad{}underscore\\_bla\\$/"}');
});

});
16 changes: 13 additions & 3 deletions test/utils/latex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@ var assert = require('assert'),
latex = require('../../lib/utils/latex');

describe('util.latex', function() {
it('should convert symbols with indices', function () {
assert.equal(latex.toSymbol('alpha_1'), '\\alpha_{1}');
it('should convert symbols with underscores', function () {
assert.equal(latex.toSymbol('alpha_1'), 'alpha\\_1');
});

it('should convert units', function () {
it('should convert special units', function () {
assert.equal(latex.toSymbol('deg', true), '^\\circ');
});

it('should convert normal units', function () {
assert.equal(latex.toSymbol('cm', true), '\\mathrm{cm}');
});

it('should escape strings', function () {
var string = 'space tab\tunderscore_bla$/';

assert.equal(latex.toSymbol(string), 'space~tab\\qquad{}underscore\\_bla\\$/');
});
});

0 comments on commit 9126130

Please sign in to comment.