This package provides LaTeX language support for the CodeMirror 6 code editor, based on the Overleaf LaTeX grammar.
- LaTeX syntax highlighting
- Auto-indentation for environments and groups
- Code folding for environments and document sections
- Bracket matching
- Autocompletion for common LaTeX commands and environments
- Snippets for common LaTeX structures
- Auto-closing of environments
- Hover tooltips with command and environment documentation
- LaTeX-specific linting (missing document environment, unmatched environments, etc.)
npm install codemirror-lang-latex
import { EditorState, EditorView } from '@codemirror/basic-setup';
import { latex } from 'codemirror-lang-latex';
let editor = new EditorView({
state: EditorState.create({
doc: '\\documentclass{article}\n\\begin{document}\nHello, world!\n\\end{document}',
extensions: [
// ... other extensions
latex()
]
}),
parent: document.querySelector('#editor')
});
You can configure the LaTeX language support by passing options:
import { latex } from 'codemirror-lang-latex';
// With all options explicitly set (these are the defaults)
const extensions = [
// ... other extensions
latex({
autoCloseTags: true, // Auto-close environments
enableLinting: true, // Enable linting
enableTooltips: true // Enable hover tooltips
})
];
The main function that creates a LanguageSupport
instance for LaTeX.
import { latex } from 'codemirror-lang-latex';
// Include LaTeX support in your editor with default options
const extensions = [
// ... other extensions
latex()
];
// Or with specific options
const extensions = [
// ... other extensions
latex({
autoCloseTags: true, // Enable auto-close environments
enableLinting: true, // Enable linting
enableTooltips: true // Enable tooltips on hover
})
];
The LaTeX language definition. Usually you'll want to use the latex()
function instead, but you can access this directly if you need to.
import { latexLanguage } from 'codemirror-lang-latex';
An extension that provides automatic closing of LaTeX environments.
import { autoCloseTags } from 'codemirror-lang-latex';
const extensions = [
// ... other extensions
autoCloseTags
];
An extension that shows helpful tooltips when hovering over LaTeX commands and environments.
import { latexHoverTooltip } from 'codemirror-lang-latex';
const extensions = [
// ... other extensions
latexHoverTooltip
];
A linter function that checks for common LaTeX errors and issues.
import { latexLinter } from 'codemirror-lang-latex';
import { linter } from '@codemirror/lint';
const extensions = [
// ... other extensions
linter(latexLinter({
checkMissingDocumentEnv: true, // Check for missing document environment
checkUnmatchedEnvironments: true, // Check for unmatched begin/end environments
checkMissingReferences: true // Check for references to undefined labels
}))
];
A collection of LaTeX-related snippets for common structures.
import { snippets } from 'codemirror-lang-latex';
The package includes CSS styles for syntax highlighting in dist/latex.css
. Import these styles to get the default LaTeX syntax highlighting:
import 'codemirror-lang-latex/dist/latex.css';
You can also customize styles in your own CSS.
You can compose your own editor with exactly the LaTeX features you need:
import { EditorState, EditorView } from '@codemirror/basic-setup';
import { keymap } from '@codemirror/view';
import { linter } from '@codemirror/lint';
import {
latexLanguage,
latexCompletionSource,
latexBracketMatching,
latexLinter,
latexHoverTooltip
} from 'codemirror-lang-latex';
// Create an editor with only specific LaTeX features
const editor = new EditorView({
state: EditorState.create({
doc: "\\documentclass{article}\n\\begin{document}\nHello world\n\\end{document}",
extensions: [
// Basic CodeMirror setup
basicSetup,
// Add just the LaTeX language with completion
new LanguageSupport(latexLanguage, [
latexLanguage.data.of({
autocomplete: latexCompletionSource
})
]),
// Add only the extensions you want
latexBracketMatching,
linter(latexLinter()),
latexHoverTooltip,
// Line wrapping is useful for LaTeX editing
EditorView.lineWrapping
]
}),
parent: document.querySelector('#editor')
});
The package also provides several utility functions for working with the LaTeX syntax tree:
import {
findEnvironmentName,
getIndentationLevel,
findMatchingEnvironment,
findSectionBoundaries
} from 'codemirror-lang-latex';
These can be useful for implementing custom extensions that interact with LaTeX document structure.
git clone https://github.com/texlyre/codemirror-lang-latex.git
cd codemirror-lang-latex
npm install
npm run build
To run the webpack-bundled example locally, clone the repository and run:
npm install
npm run setup-example
npm run example
Then open http://localhost:3000
in your browser.
To run the GitHub Pages example locally, which is also deployed to the demo site:
npm install
npm run setup-pages-example
npm run pages-example
This will also run on http://localhost:3000
in your browser.