6.2.0 • Published 6 months ago

latex-utensils v6.2.0

Weekly downloads
600
License
MIT
Repository
github
Last release
6 months ago

latex-utensils

latex-utensils CI Tests

A LaTeX parser and utilities.

The parser is based on the following libraries:

Usage

You can see AST as follows. Without the option -i, you can obtain the output as JSON format.

$ cat sample/t.tex
\documentclass{article}
\usepackage{amsmath}
\begin{document}
  abc $x+y$ d
  \begin{align}
    a + b
  \end{align}
\end{document}

$ luparse --color -i sample/t.tex
{ kind: 'ast.root',
  content:
   [ { kind: 'command',
       name: 'documentclass',
       args:
        [ { kind: 'arg.group',
            content: [ { kind: 'text.string', content: 'article' } ] } ] },
     { kind: 'command',
       name: 'usepackage',
       args:
        [ { kind: 'arg.group',
            content: [ { kind: 'text.string', content: 'amsmath' } ] } ] },
     { kind: 'env',
       name: 'document',
       args: [],
       content:
        [ { kind: 'text.string', content: 'abc' },
          { kind: 'inlineMath',
            content:
             [ { kind: 'math.character', content: 'x' },
               { kind: 'math.character', content: '+' },
               { kind: 'math.character', content: 'y' } ] },
          { kind: 'text.string', content: 'd' },
          { kind: 'env.math.align',
            name: 'align',
            args: [],
            content:
             [ { kind: 'math.character', content: 'a' },
               { kind: 'math.character', content: '+' },
               { kind: 'math.character', content: 'b' } ] },
          { kind: 'parbreak' } ] } ],
  comment: undefined }

$ luparse --help
Usage: luparse [options]

Options:
  -i, --inspect            use util.inspect to output AST
  --color                  turn on the color option of util.inspect
  -l, --location           enable location
  -c, --comment            enable comment
  -s, --start-rule [rule]  set start rule. default is "Root".
  -h, --help               output usage information

API

import {latexParser} from 'latex-utensils';
const texString = '\\begin{document}abc\\end{document}';
const ast = latexParser.parse(texString);
console.log(JSON.stringify(ast, undefined, '  '));

latexParser.parse(texString, options?): AstRoot | AstPreamble

Parameters

  • texString: string
  • options?: { startRule?: 'Root' | 'Preamble'; enableComment?: boolean; }startRule specifies a rule with which the parser begins. If 'Root' is set, the whole document is parsed. If 'Preamble' is set, only the preamble is parsed. The default is 'Root'. If enableComment is true, all the comments in the texString will be extracted into a returned AST also. The default is false.

Returns

If the startRule is Root, an AstRoot object is returned.

type AstRoot = {
    kind: 'ast.root';
    content: Node[];
    comment?: Comment[];
}

If the startRule is Preamble, an AstPreamble object is returned.

type AstPreamble = {
    kind: 'ast.preamble';
    content: Node[];
    comment?: Comment[];
    rest: string;
}

For the details of Node and Comment, please see src/latex/latex_parser_types.ts.

latexParser.parsePreamble(texString): AstPreamble

Equivalent to latexParser.parse(texString, {startRule: 'Preamble'}).

latexParser.stringify(node: Node | Node[], options = { lineBreak: '' }): string

Convert AST to a string.

latexParser.isSyntaxError(e: any): boolean

A Type Guard for SyntaxError thrown by latexParser.parse.

Development

To lint changes, run

npm run lint

To build, run

npm run build

To test, run

npm run test
6.2.1-b.4

6 months ago

6.2.1-b.3

6 months ago

6.2.1-b.1

6 months ago

6.2.0

8 months ago

6.1.0

11 months ago

6.0.0

11 months ago

5.0.0

12 months ago

4.4.0

2 years ago

4.6.0

2 years ago

4.5.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.3.0

2 years ago

4.1.1

2 years ago

4.1.0

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

4.0.0-beta.5

3 years ago

4.0.0-beta.4

3 years ago

4.0.0-beta.3

3 years ago

4.0.0-beta.2

3 years ago

4.0.0-beta.1

3 years ago

4.0.0-beta.0

3 years ago

3.0.0

3 years ago

3.0.0-beta.5

3 years ago

3.0.0-beta.4

3 years ago

3.0.0-beta.3

3 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.0

3 years ago

2.3.0

3 years ago

2.2.0-beta.0

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.1.0-beta.2

3 years ago

2.1.0-beta.1

4 years ago

2.1.0-beta.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

2.0.0-beta.5

4 years ago

2.0.0-beta.4

4 years ago

2.0.0-beta.3

4 years ago

2.0.0-beta.2

4 years ago

2.0.0-beta.1

4 years ago

2.0.0-beta.0

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.10

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago