README.md (2158B)
1 # escape-latex 2 3 [](https://greenkeeper.io/) 4 5 [](https://travis-ci.org/dangmai/escape-latex) 6 7 Escape LaTeX special characters with Javascript in NodeJS (>= 4.x) environment. 8 9 ## Usage 10 11 ```javascript 12 npm install escape-latex 13 var lescape = require('escape-latex'); 14 lescape("String to be escaped here #yolo"); 15 ``` 16 17 ## API 18 19 ```javascript 20 lescape((input: String), { 21 preserveFormatting: Boolean, 22 escapeMapFn: Function, 23 }); 24 ``` 25 26 By default, 27 `escape-latex` only escapes characters that would result in malformed LaTeX. 28 These characters include `# $ % & \ ^ _ { }`. 29 30 This means that the final LaTeX output might not look the same as your input Javascript string. 31 For example, multiple spaces are kept as-is, which may be truncated to 1 space by your LaTeX software. 32 33 If you want the final output string to be as similar to your input Javascript string as possible, 34 you can set the `preserveFormatting` param to `true`, like so: 35 36 ```javascript 37 lescape("Hello World", { preserveFormatting: true }); 38 // Hello~~~World 39 ``` 40 41 Which will be converted to three non-breaking spaces by your LaTeX software. 42 43 The list of format characters that are escaped include `space, \t (tab), – (en-dash), — (em-dash)`. 44 45 There is also the param `escapeMapFn` to modify the mapping of escaped characters, 46 so you can add/modify/remove your own escapes if necessary. 47 48 It accepts a callback function that takes in the default character escapes and the formatting escapes as parameters, and returns a complete escape mapping. Here's an example: 49 50 ```javascript 51 lescape("Hello World", { 52 preseveFormatting: true, 53 escapeMapFn: function(defaultEscapes, formattingEscapes) { 54 formattingEscapes[" "] = "\\\\"; 55 return Object.assign({}, defaultEscapes, formattingEscapes); 56 }, 57 }); 58 // Hello\\\\\\world 59 ``` 60 61 ## Testing 62 63 ``` 64 npm test 65 ``` 66 67 ## Notes 68 69 * If you are updating from `escape-latex < 1.0.0`, 70 the `en-dash` and `em-dash` are no longer escaped by default. 71 Please use `preserveFormatting` to turn them on if necessary. 72 73 ## License 74 75 MIT