FxEditor
User guideQuickstartDeploymentAPIFxTeX
APIExamplesAccessibilityFxTeXServer
DeploymentAPILaTeXRender
DeploymentAPIPlugins
CK Editor v5Tiny MCE v6BloggerAngularDeveloper GuideLicensing
How licensing worksfxEditor API
The Equation Editor API (Application Programming Interface) provides a mechanism to build a custom equation editor and to integrate it seamlessly within any HTML web page. This page shows the essential steps to creating an editor toolbar, customising its panels, and interfacing with the content on your page.
Prerequisites
Add to <head> :
<script src="https://editor.codecogs.com/eqneditor.api.min.js"
crossorigin="anonymous"></script>
Add to <body> :
<div id="toolbar"></div>
<div id="latexInput"></div>
<img id="output"/>
<script type="text/javascript">
textarea = new EqEditor.TextArea('latexInput')
.addToolbar(new EqEditor.Toolbar('toolbar'), true)
.addOutput(new EqEditor.Output('output'));
</script>
Toolbar
constructor
-
new Toolbar(toolbarElementId: string, design?: any, scale?: number): Toolbar
Places the Equation Editor toolbar on the page, e.g.
<div id="toolbar"></div>
<div id="input"></div>
<script>
var tb = new EqEditor.Toolbar('toolbar');
tb.addTextArea(new EqEditor.TextArea('input'), true);
</script>You can use any of the standard toolbar designs: 'default', 'full', 'mini', 'chemistry', 'sidebar' and 'inactive'. e.g.
var tb = new EqEditor.Toolbar('toolbar','mini');Alternatively you can select the panels you require, in any order. Each inner array is one row of the toolbar, so the example below produces a single row of six panels:
var tb = new EqEditor.Toolbar('toolbar', '[["operators_core","greeklower","chemistry_arrows",
"mhchem","mhchem_arrows","colors"]]');The available panel names are: 'binary', 'accents', 'accents_ext', 'arrows', 'brackets', 'chemistry_arrows', 'chemistry_letters', 'colors', 'foreign', 'greeklower', 'greekupper', 'matrix', 'mhchem', 'mhchem_arrows', 'operators', 'operators_core', 'relations', 'spaces', 'style', 'subsupset', 'symbols'
Parameters
- toolbarElementId: string
Id of the layer into which the toolbar is placed.
Optionaldesign: any = 'default'The name of a standard toolbar design, or a JSON array of rows listing the panels to be used.
Optionalscale: number = 1Optional scaling of the toolbar buttons.
Returns Toolbar
- toolbarElementId: string
add Math View
-
addMathView(mathView: MathView, reverseLink?: boolean): Toolbar
Connects a MathView with the current Toolbar, the same way addTextArea() does for a TextArea - so the toolbar's buttons insert into the MathView whenever it has focus. Returns the current Toolbar object.
Parameters
- mathView: MathView
The MathView object to associate with the Toolbar.
OptionalreverseLink: boolean = trueIf true it also adds the Toolbar to the MathView.
Returns Toolbar
This Toolbar, in support of chaining commands together.
- mathView: MathView
add Text Area
-
addTextArea(textArea: TextArea, reverseLink?: boolean): Toolbar
Connects a TextArea with the current Toolbar. For example:
<div id="editor"></div>
<div id="input"></div>
<script>
EqEditor.Toolbar.link('editor', true)
.addTextArea(new EqEditor.TextArea('input'), true)
</script>Returns the current Toolbar object.
Parameters
- textArea: TextArea
The TextArea object to associate with the Toolbar.
OptionalreverseLink: boolean = trueIf true it also adds the Toolbar to the TextArea.
Returns Toolbar
This Toolbar, in support of chaining commands together.
- textArea: TextArea
append Panel
-
appendPanel(id: number, panel: string | object): Toolbar
Appends a new panel, with a custom set of buttons, to the end of an existing toolbar row.
The function accepts either a string naming one of the built-in panels (e.g. "matrix"), or an object describing a panel of your own, with the following properties:
- name {string} - A unique name for the panel.
- class {string} - [optional] CSS class to assign to each button.
- width {number} - Width of the panel in pixels.
- buttons {array} - List of button objects (see below).
A button is defined with the following properties:
- latex {string} - What the button displays. This can be plain text, or a single supported LaTeX string, which is rendered as MathML on the button face.
- width {number} - Width of the button in pixels.
- operation {string} - Command to be executed, typically "insert", but it may be any method of the active TextArea or MathView.
- args {array} - Arguments passed to the 'operation' function. For "insert" the first argument is the LaTeX to be added to the TextArea; for other operations it depends on the method being called.
By default all text buttons are given the class CCButton, while rendered ones are given the class CCImage.
Key examples:
EqEditor.Toolbar.link('toolbar','[["operators"]]')
.addTextArea(textarea)
.appendPanel(0,"matrix")
.appendPanel(0,{
"name":"textvariables",
"width":40,
"buttons":[
{"latex":"bill"},
{"latex":"jill"},
{"latex":"jack"}
]
})
.appendPanel(0,{
"name":"any_unique_name",
"class":"green",
"width":50,
"buttons":[
{
"latex": "\\mp",
"width": 35,
"operation": "insert",
"args": ["\\mp"],
},
{
"latex": "alpha",
"width": 35,
"operation": "insert",
"args": ["\\alpha"],
}
]
});Parameters
- id: number
The row of the toolbar to append to. Rows are numbered from 0.
- panel: string | object
The name of a standard panel, or an object describing a panel of your own (see above).
Returns Toolbar
This Toolbar, in support of chaining commands together.
get Panel
-
getPanel(panelName: string): HTMLDivElement
Returns the HTMLDivElement for an equation editor toolbar panel, given its name. This is typically required when dynamically moving a panel from the 'inactive' toolbar onto the main toolbar.
Parameters
- panelName: string
The name of the panel to retrieve.
Returns HTMLDivElement
- panelName: string
move To
-
moveTo(toolbarElementId: string): Toolbar
Moves the toolbar from one location on the page to another. This is useful when a single toolbar is shared between several inputs and should appear beside whichever one the user is currently editing. For example:
<div id="editor1"></div>
<div id="input1" onClick="tb.moveTo('editor1')"></div>
<div id="editor2"></div>
<div id="input2" onClick="tb.moveTo('editor2')"></div>
<script>
tb = EqEditor.Toolbar.link('editor1', true)
.addTextArea(new EqEditor.TextArea('input1'), true)
.addTextArea(new EqEditor.TextArea('input2'), true);
</script>Parameters
- toolbarElementId: string
Id of the layer into which the toolbar is to be moved.
Returns Toolbar
This Toolbar, in support of chaining commands together.
- toolbarElementId: string
set Active Text Area
-
setActiveTextArea(target: ToolbarTarget): Toolbar
Makes the specified TextArea (or MathView) active, so that it receives input from the toolbar and the keyboard.
Parameters
- target: ToolbarTarget
The TextArea or MathView to make active.
Returns Toolbar
Returns this (the current Toolbar object) in support of chained commands.
- target: ToolbarTarget
Staticlink
-
link(toolbarElementId: string, design?: any, scale?: number): Toolbar
A static constructor that places the Equation Editor toolbar on the page and returns the new Toolbar object. This is a convenient alternative to 'new EqEditor.Toolbar', as it allows you to chain associated Toolbar commands, e.g.
<div id="toolbar"></div>
<div id="input"></div>
<script>
EqEditor.Toolbar.link('toolbar')
.addTextArea(new EqEditor.TextArea('input'), true);
</script>You can use any of the standard toolbar designs: 'default', 'full', 'mini', 'chemistry', 'sidebar' and 'inactive'. e.g.
EqEditor.Toolbar.link('toolbar','chemistry');Alternatively you can select the panels you require, in any order, one inner array per row:
EqEditor.Toolbar.link('toolbar', [["operators_core","greeklower","chemistry_arrows",
"mhchem","mhchem_arrows","colors"]]);The available panel names are: 'binary', 'accents', 'accents_ext', 'arrows', 'brackets', 'chemistry_arrows', 'chemistry_letters', 'colors', 'foreign', 'greeklower', 'greekupper', 'matrix', 'mhchem', 'mhchem_arrows', 'operators', 'operators_core', 'relations', 'spaces', 'style', 'subsupset', 'symbols'
Parameters
- toolbarElementId: string
Id of the layer into which the toolbar is placed.
Optionaldesign: any = 'default'The name of a standard toolbar design, or a JSON array of rows listing the panels to be used.
Optionalscale: number = 1Optional scaling of the toolbar buttons.
Returns Toolbar
The new Toolbar, ready for further chained calls.
- toolbarElementId: string
TextArea
The TextArea class manages the connection between the equation editor Toolbar and the editable
areas in which the LaTeX markup is shown and edited. An editable area is an ordinary HTML
div element, which the TextArea turns into a contenteditable region when it is linked.
The key features a TextArea provides are:
- syntax highlighting of the equation as it is typed;
- auto-suggestion of LaTeX commands, with a rendered preview of each candidate;
- undo/redo history, when a History menu is attached;
- optional restriction of the variables and commands a user is allowed to enter.
A TextArea is usually paired with a Toolbar, which provides visual assistance. Several TextAreas may be attached to a single Toolbar, and conversely several Toolbars may be attached to a single TextArea.
A TextArea can also be linked to one or more Output regions, which present a rendered version of the equation, or supply the LaTeX in a form suitable for pasting into another system (for example a forum, a wiki, or Google Docs). A mathview.MathView may be linked in the same way, giving a WYSIWYG view of the same equation that stays in step with the markup in both directions.
<div id="toolbar"></div>
<div id="input" placeholder="Write LaTeX here"></div>
<img id="output"/>
<script>
EqEditor.TextArea.link('input', true)
.addToolbar(new EqEditor.Toolbar('toolbar'))
.addOutput(new EqEditor.Output('output', 'url'));
</script>
TextArea
constructor
-
new TextArea(textAreaId: string, focus?: boolean, wrap?: boolean): TextArea
Creates an instance of TextArea for LaTeX input and associates it with an HTML element. To provide some introductory text, use the
placeholderattribute as illustrated in the example below. Any introductory text is removed as soon as the user starts typing.<div id="input" placeholder="Write LaTeX here"></div>
<script>
var ta = new EqEditor.TextArea('input', true);
</script>Parameters
- textAreaId: string
Id of the target div to be used for editing.
Optionalfocus: boolean = falseActivates the cursor within the text area.
Optionalwrap: boolean = trueWraps long equations onto multiple lines and makes the area vertically resizable. Pass false to keep the equation on a single scrolling line.
Returns TextArea
- textAreaId: string
add History Menu
-
addHistoryMenu(history: History): TextArea
Adds history, with undo and redo, to the TextArea. This requires a History menu to provide the necessary controls.
Parameters
- history: History
The history menu object.
Returns TextArea
This TextArea, in support of chaining commands together.
- history: History
add Output
-
addOutput(out: TextAreaOutput): TextArea
Connects an Output component to the current TextArea. Every connected Output is refreshed whenever the equation changes. A mathview.MathView registers through this same call, so a WYSIWYG view can be kept in step with the markup.
Parameters
- out: TextAreaOutput
The Output (or MathView) to attach.
Returns TextArea
This TextArea, in support of chaining commands together.
- out: TextAreaOutput
add Restricted Commands
-
addRestrictedCommands(words: string[]): TextArea
Parameters
- words: string[]
Returns TextArea
add Restricted Variables
-
addRestrictedVariables(words: string[]): TextArea
Parameters
- words: string[]
Returns TextArea
add Toolbar
-
addToolbar(toolbar: Toolbar, activate?: boolean): TextArea
Connects a Toolbar component to the current Textarea.
Parameters
- toolbar: Toolbar
The Toolbar component.
Optionalactivate: boolean = trueInforms the toolbar to send commands to this TextArea.
Returns TextArea
This TextArea, in support of chaining commands together.
- toolbar: Toolbar
call Outputs
-
callOutputs(func: string, arg: any): void
Provides a convenient method of executing any Output class function across all of the Output areas associated with the TextArea. This is particularly useful when you have several outputs and need them all to change together.
For example, say you want to change the format for all the outputs:
<select id="format" onchange="textarea.callOutputs('setFormat', this.value);">
<option value="svg">svg</option>
<option value="gif">gif</option>
<option value="png">png</option>
<option value="pdf">pdf</option>
<option value="emf">emf</option>
</select>Parameters
- func: string
- arg: any
Returns void
clear
-
clear(): void
Clears the input box (TextArea), removing the equation and resetting the cursor.
Returns void
close
-
close(): void
Call if you are dynamically displaying the editor and select to 'close' a TextArea. This function will clean up any extra DOM elements.
This used to hide the suggestion list and stop there, leaving five listeners on the input element and, if the editor was closed mid-drag, one on
window. Each of those closes over the TextArea, so nothing it held - the token array, the history, the Output and History menus it notifies - could be collected, and an editor opened and closed repeatedly (a dialog, a per-cell editor in a table) accumulated a copy of all of it every time. Closing twice is harmless: removeEventListener on a listener that isn't attached does nothing.Returns void
focus
-
focus(): void
Moves keyboard focus into this TextArea. Part of the ToolbarTarget contract (see src/toolbar/toolbar.ts) so the Toolbar can refocus whichever target - a TextArea or a MathView - a keyboard-driven button activation should return focus to.
Returns void
get Text Area
-
getTextArea(): HTMLElement
Returns the HTML element backing this TextArea. Typically you will then read its
textContentto retrieve the LaTeX formula.The following example illustrates how a button onclick action can retrieve the content of a TextArea.
<div id="input"></div>
<script>
textarea = new EqEditor.TextArea('input');
</script>
...
<button onclick="alert(textarea.getTextArea().textContent)" />Returns HTMLElement
insert
-
insert(txt: string, newCaretPos?: number, selectedInsertPos?: number): void
Insert commands into the active HTML element at the current cursor position. This function is usually called in response to a toolbar button press. The following example insert text for a fraction and places the cursor between the first curly braces:
<div id="input"></div>
<script>
var a = EqEditor.TextArea.link('input', true);
a.insert('\\frac{}{}', 6);
</script>Parameters
- txt: string
New text to insert. Remember that a backslash must be escaped in JavaScript string literals, i.e. '\frac{}{}' rather than '\frac{}{}'.
OptionalnewCaretPos: number = nullWhere to place the cursor, counted from the start of the inserted text. 0 is the start; null leaves the cursor at the end of the insertion.
OptionalselectedInsertPos: number = nullWhere any currently selected text should be placed within the newly inserted text, so that (for example) selecting "x" and pressing the square-root button yields "\sqrt{x}".
Returns void
- txt: string
is Text Area Empty
-
isTextAreaEmpty(): boolean
Returns true if the current TextArea is empty, false otherwise.
Returns boolean
make Array Matrix
-
makeArrayMatrix(type: string, start: string, end: string): void
Generates the LaTeX for an array of the currently selected dimensions (see TextArea.curMatrixDims) and inserts it into the active TextArea.
Parameters
- type: string
Sets the type of array, e.g. 'array'.
- start: string
Start token, e.g. '\left('.
- end: string
End token, e.g. '\right)'.
Returns void
- type: string
make Equations Matrix
-
makeEquationsMatrix(
type: string,
firstLine?: string,
middleLine?: string,
lastLine?: string,
): voidGenerates the LaTeX for a matrix of the currently selected dimensions (see TextArea.curMatrixDims) and inserts it into the active TextArea.
Parameters
- type: string
Type of matrix to generate, e.g. 'pmatrix'.
- firstLine: string = ""
Format of the first line.
- middleLine: string = null
Format of the middle lines.
- lastLine: string = null
Format of the last line.
Returns void
- type: string
notify History Menus
-
notifyHistoryMenus(): void
Notifies all connected History Menus of changes.
Returns void
notify Outputs
-
notifyOutputs(): void
Notifies all connected Outputs of changes.
Returns void
push To History
-
pushToHistory(): void
Pushes any text from the current TextArea to the History stack. Which allows further changes to be undone.
Returns void
redo History
-
redoHistory(): void
Reverts an undone change, i.e. redoes the most recently undone action.
Returns void
replace All
-
replaceAll(text: string, caret: number): void
Replaces the whole equation in one step, recording a single undo entry for it.
The maths view edits its own copy of the LaTeX and then pushes the result here. Doing that as a bare clear() + insert() was visible in the history: two entries per keystroke, the first of them empty. This is the same rewrite with the history told about it once.
Parameters
- text: string
The equation to put in the input area.
- caret: number
Where to leave the cursor, as an offset into
text.
Returns void
- text: string
set Autocomplete
-
setAutocomplete(enabled: boolean): TextArea
Enables or disables auto-insertion of matching close brackets when an open bracket is typed.
Parameters
- enabled: boolean
Pass true to enable auto-bracket completion, false to disable it.
Returns TextArea
This TextArea, in support of chaining commands together.
- enabled: boolean
set Restricted Commands
-
setRestrictedCommands(words: string[]): TextArea
Assigns the LaTeX commands that may be used within the formula. Any command breaking this condition is given the CSS class "CCrestricted", which by default shows it in red with a double underline. Override the CSS for "CCrestricted" to achieve alternative effects.
Parameters
- words: string[]
Array of allowable words. RegEx expressions can also be used.
Returns TextArea
- words: string[]
set Restricted Variables
-
setRestrictedVariables(words: string[]): TextArea
Assigns the variables that may be used within the formula. Any variable breaking this condition is given the CSS class "CCrestricted", which by default shows it in red with a double underline. Use TextArea.validate to test the equation as a whole.
The follow example allows variable 'bill' and any that start with 'j' and also end with 's':
textarea = EqEditor.TextArea.link('latexInput',true)
.addOutput(new EqEditor.Output('output'))
.setRestrictedVariables(['bill','j\\w+s'])Parameters
- words: string[]
Array of allowable words. RegEx expressions can also be used.
Returns TextArea
- words: string[]
set Restrict Mode
-
setRestrictMode(mode: number): TextArea
Restricts keyboard input. This is useful in assessment settings, where the equation must be built from a controlled set of toolbar buttons rather than typed freely.
There are three modes:
- 0 - allows all text and key operations (the default);
- 1 - allows the cursor, delete and space keys; all other input must come from a toolbar;
- 2 - all input must come from a toolbar.
Parameters
- mode: number
One of 0, 1 or 2.
Returns TextArea
This TextArea, in support of chaining commands together.
undo History
-
undoHistory(): void
Undoes the last action. Repeated calls unwind further changes to the input area.
Returns void
update Matrix Dims
-
updateMatrixDims(text: string): void
Changes the dimensions of an existing selected matrix
Parameters
- text: string
The selected text
Returns void
- text: string
validate
-
validate(): boolean
Checks the equation to ensure it contains only the variables and commands permitted by TextArea.setRestrictedVariables and TextArea.setRestrictedCommands.
Returns boolean
True if every token is permitted, false otherwise.
Staticlink
-
link(textAreaId: string, focus?: boolean, wrap?: boolean): TextArea
Static constructor.
Returns a new instance of TextArea linked to the specified HTML element. The advantage of this over the constructor with 'new' is that it allows you to chain additional requirements together, as shown in the example:
<div id="input"></div>
<script>
EqEditor.TextArea.link('input', true)
.addToolbar(new EqEditor.Toolbar('toolbar'), true)
</script>Parameters
- textAreaId: string
Id of the target div to be used for editing.
Optionalfocus: boolean = falseActivates the cursor within the text area.
Optionalwrap: boolean = trueWraps long equations onto multiple lines.
Returns TextArea
The new TextArea, ready for further chained calls.
- textAreaId: string
MathView
A MathView is a WYSIWYG view of an equation: the maths is drawn as live MathML and edited in place, with a caret you can click to position and arrow keys you can navigate with, rather than typed as markup.
A MathView plugs into a Toolbar exactly as a TextArea does, so every toolbar button inserts into whichever of the two currently has focus. It also registers as one of a TextArea's Outputs, so a page can show the markup and the rendered equation side by side and have an edit in either one appear immediately in the other.
This means a page can offer three arrangements from the same objects:
- MathView only - a WYSIWYG editor, with the LaTeX hidden from the user entirely;
- TextArea only - the classic markup editor with syntax highlighting;
- both - the two linked together, each reflecting edits made in the other.
<div id="toolbar"></div>
<div id="mathview"></div>
<div id="input"></div>
<script>
var ta = EqEditor.TextArea.link('input');
EqEditor.MathView.link('mathview', true)
.addTextArea(ta)
.addToolbar(new EqEditor.Toolbar('toolbar'));
</script>
Editing is by whole construct rather than by character: pressing Backspace just after
\frac{x}{2} removes the fraction entirely, while pressing it inside the fraction removes only
the x. Empty arguments are drawn as grey boxes, so an inserted \frac{}{} shows you the two
blanks that are waiting to be filled in.
MathView
constructor
-
new MathView(mathViewElementId: string, focus?: boolean): MathView
Creates a WYSIWYG MathML view inside the given container element, e.g.
<div id="mathview"></div>
<script>
var mv = new EqEditor.MathView('mathview');
mv.addTextArea(new EqEditor.TextArea('input'));
</script>Parameters
- mathViewElementId: string
Id of the container div the view is placed into.
Optionalfocus: boolean = falseActivates the cursor within the view.
Returns MathView
- mathViewElementId: string
add Text Area
-
addTextArea(textArea: TextArea, reverseLink?: boolean): MathView
Links this view to a TextArea that holds its LaTeX. An edit here refreshes that TextArea's content (see pushToLinkedTextArea); an edit there refreshes this view (see updateOutput, wired up automatically via TextArea.addOutput()).
Parameters
- textArea: TextArea
The TextArea to hold this view's LaTeX.
OptionalreverseLink: boolean = trueIf true, also registers this view as one of the TextArea's Outputs, so it is notified whenever that TextArea changes.
Returns MathView
This MathView, in support of chaining commands together.
- textArea: TextArea
add Toolbar
-
addToolbar(toolbar: Toolbar, activate?: boolean): MathView
Connects a Toolbar to this view, the same way TextArea.addToolbar() does - so the Toolbar's buttons insert into this view whenever it has focus.
Parameters
- toolbar: Toolbar
The Toolbar component.
Optionalactivate: boolean = trueImmediately makes this view the Toolbar's active target.
Returns MathView
This MathView, in support of chaining commands together.
- toolbar: Toolbar
clear
-
clear(): void
Empties the view, removing the equation and resetting the cursor to the start.
Returns void
connect Text Area
-
connectTextArea(_textAreaId: string): void
Part of the Output-style contract TextArea.addOutput() expects; the actual TextArea object reference is supplied directly via addTextArea() instead, so there's nothing further to do with the id itself.
Parameters
- _textAreaId: string
Returns void
focus
-
focus(): void
Moves keyboard focus into this view. Part of the ToolbarTarget contract (see src/toolbar/toolbar.ts) so the Toolbar can refocus whichever target a keyboard-driven button activation should return focus to.
Returns void
get Cursor Offset
-
getCursorOffset(): number
Where the cursor is, as a character offset into the LaTeX getLatex() returns.
The same number the linked TextArea reports, because both panes count into the same string - so anything following the editor can ask either one. Provided for a view used on its own, with no markup pane to ask instead.
Returns number
The cursor's offset into the equation.
get Latex
-
getLatex(): string
Returns the equation's current LaTeX. This is the value to read when submitting a form, or when handing the equation to a rendering service.
Returns string
The LaTeX of the equation currently shown.
insert
-
insert(txt: string, newCaretPos?: number, selectedInsertPos?: number): void
Splices
txtinto the equation at the cursor - what every "insert" toolbar button funnels through (see icons.json), whether that's a single command (\alpha), a full structure (\frac{}{},\int_{}^{}{}), or plain text. Since the document is the LaTeX, this is a string splice and needs no parsing of the snippet at all; the cursor lands inside the snippet's first empty{}pair if it has one, matching the "ready to type" placement the same button gives in a TextArea.Parameters
- txt: string
- newCaretPos: number = null
- selectedInsertPos: number = null
Returns void
insert Frac
-
insertFrac(): void
Inserts an empty fraction,
\frac{}{}, leaving the cursor in the numerator.Returns void
insert Int
-
insertInt(): void
Inserts an integral with empty limits,
\int_{}^{}.Returns void
insert Paren
-
insertParen(): void
Inserts an auto-sized bracket pair,
\left({}\right).Returns void
insert Script
-
insertScript(): void
Inserts an empty superscript,
^{}, leaving the cursor inside it.Returns void
insert Sqrt
-
insertSqrt(): void
Inserts an empty square root,
\sqrt{}.Returns void
make Array Matrix
-
makeArrayMatrix(...args: any[]): void
Parameters
- ...args: any[]
Returns void
make Equations Matrix
-
makeEquationsMatrix(...args: any[]): void
Delegates to the linked TextArea's own matrix-building logic rather than reimplementing it. curMatrixDims is relayed across since the TextArea's own method reads it from itself, not from whichever object happened to be the Toolbar's active target when it was set (see Toolbar's matrix grid UI, which writes directly to the active target's curMatrixDims). Unlike the previous version of this view, the resulting matrix now renders here as a real table rather than showing up as literal LaTeX - the converter has always understood matrices; it was this view that did not.
Parameters
- ...args: any[]
Returns void
notify Outputs
-
notifyOutputs(): void
Delegates so any Output/History already linked to the TextArea still refreshes after a toolbar-driven edit here, same as if the user had typed directly into the TextArea.
Returns void
set Inline
-
setInline(flag?: boolean): MathView
Shows the equation as an inline one or as a display one, the same distinction output.Output.setInline makes for the exported formats.
The visible difference is
display="block"on the<math>root: a display equation gets its own centred line, an inline one sits where it is put, on the baseline of whatever text surrounds it. Call it alongside Output.setInline() so what is on screen and what gets exported agree.Display style is left on either way - full-size fraction numerators, and a large operator's limits above and below rather than beside. That is deliberate and it is about editing rather than presentation: in text style MathML drops an
<mfrac>'s numerator and denominator a script level, so a fraction inside a fraction is drawn smaller than the one containing it, and the inner one is exactly what somebody is usually trying to click into.mathview.setInline(true);Parameters
Optionalflag: boolean = trueTrue for an inline equation, false for a display one.
Returns MathView
This MathView, in support of chaining commands together.
set Latex
-
setLatex(latex: string): void
Replaces the equation wholesale and puts the cursor at the end. Use this to pre-load the view with an existing equation.
Parameters
- latex: string
The LaTeX to display. Pass an empty string to clear the view.
Returns void
- latex: string
update Output
-
updateOutput(): void
Re-renders this view from the linked TextArea's current LaTeX, and puts this view's cursor where that TextArea's is. Both are plain character offsets into the same string, so the "nearest matching position" translation an AST-based view needed here is now just an assignment. Skips an edit that's actually this view's own echo (see pushToLinkedTextArea / lastPushedLatex) rather than pointlessly re-rendering text already fully in sync.
Returns void
Staticlink
-
link(mathViewElementId: string, focus?: boolean): MathView
Static constructor. Returns a new instance of MathView linked to the specified element, allowing chained setup, e.g.
<div id="mathview"></div>
<div id="input"></div>
<script>
EqEditor.MathView.link('mathview')
.addTextArea(EqEditor.TextArea.link('input'));
</script>Parameters
- mathViewElementId: string
Id of the container div the view is placed into.
Optionalfocus: boolean = falseActivates the cursor within the view.
Returns MathView
The new MathView, ready for further chained calls.
- mathViewElementId: string
Output
The Output class extracts the content of an editable api.TextArea in a variety of forms. Its most common use is to show the fully rendered equation as an image, but it will equally format the LaTeX for pasting into a third-party blog, forum, wiki or document.
One or more Outputs are attached to a TextArea with api.TextArea.addOutput. Each Output can then be configured independently with its own image format, font, font size, DPI, background colour and inline setting, so a single equation can drive several previews at once.
<div id="entry"></div>
<img id="output"/>
<script>
EqEditor.TextArea.link('entry')
.addOutput(EqEditor.Output.link('output', 'url')
.setFormat('svg')
.setFont('phv')
.setFontSize('LARGE'));
</script>
Output
constructor
-
new Output(divOutputId?: string, outputFormat?: string): Output
Creates an instance of the Output class, which links output from the Equation editor to an HTML element on the page. This is used to export LaTeX content from an associated TextArea in various forms for display or inclusion in other web tools. e.g.:
<div id="entry"></div>
<img id="output"/>
<script>
EqEditor.TextArea.link('entry')
.addOutput(new EqEditor.Output('output', 'url'));
</script>The outputFormat sent to the exported area can be one of:
- wp - WordPress.
- phpBB - phpBB bulletin board.
- tw - TiddlyWiki.
- url - Unique web address of the rendered image.
- urlencoded - As url, but with additional encoding of special characters.
- pre - HTML
preelement with its xml language set to latex. Used by some forums. - doxygen - Doxygen format.
- html - HTML
imgelement. - latex - Raw LaTeX.
Parameters
- divOutputId: string = null
The id of the element the Output will render into. An
imgelement receives the rendered equation; any other element receives the formatted text. - outputFormat: string = null
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Returns Output
add Export Area
-
addExportArea(divElementId: string, exportType: string): Output
Connects an export area to this Output. The named element is filled with the equation in the requested format, and refreshed whenever the equation changes - which is how a page offers a "copy this into your forum post" box beside the preview.
Parameters
- divElementId: string
The id of the export area element.
- exportType: string
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Returns Output
This Output, in support of chaining commands together.
- divElementId: string
connect Text Area
-
connectTextArea(textAreaId: string): void
Links this Output to a TextArea, so it displays the formatted equation for that text. You will not usually call this directly - api.TextArea.addOutput does it for you.
Parameters
- textAreaId: string
The id of the TextArea's element.
Returns void
- textAreaId: string
download
-
download(filename?: string): void
Downloads the current equation as a file in the Output's current image format.
Parameters
Optionalfilename: string = ...Name to save the file under. Defaults to "equation." followed by the current format, e.g. "equation.svg". Browsers ignore a requested filename when the rendering service is on a different origin to the page, in which case the name comes from the service's own Content-Disposition header.
Returns void
export As
-
exportAs(exportType: string): string
Exports the current equation, returning a text string in the requested format.
Parameters
- exportType: string
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex, mathml, mathmlspeech]
Returns string
The equation, formatted for the requested target.
- exportType: string
get Equation
-
getEquation(): string
Returns the equation in a compact form, removing all redundant spaces.
Returns string
get Equation URL
-
getEquationURL(download?: boolean): string
Returns the URL that renders the current equation.
Parameters
Optionaldownload: boolean = falseIf true, returns a URL that downloads the rendered equation as a file; otherwise returns a URL that displays it.
Returns string
get Export Options
-
getExportOptions(): string[]
Returns the supported export options, i.e. the formats accepted by Output.setFormat and Output.addExportArea.
Returns string[]
get Inline
-
getInline(): boolean
Returns true if the equation is being rendered inline (vertically aligned with the surrounding text) rather than as a display equation.
Returns boolean
get Supported Colors
-
getSupportedColors(): any[]
Returns the supported background colours, as an array of
{value, desc}pairs.Returns any[]
get Supported Dpi
-
getSupportedDpi(): string[]
Returns the supported output DPI values.
Returns string[]
get Supported Export Formats
-
getSupportedExportFormats(): any[]
Returns the supported export formats, as an array of
{value, desc, group}entries suitable for populating aselectelement - the counterpart of Output.getSupportedFonts.groupnames theoptgroupan entry belongs under ("Formats" for markup you keep, "Platforms" for markup only one application understands). Entries arrive in the order they should be offered, with each group's members already adjacent, so a caller can build the menu in one pass without sorting.Every entry is a valid argument to Output.exportAs and Output.setExportFormat. Building a menu from this rather than hard-coding one means a page picks up new formats when the API gains them, instead of silently offering an out-of-date list.
Returns any[]
get Supported Fonts
-
getSupportedFonts(): any[]
Returns the supported output fonts, as an array of
{value, desc}pairs suitable for populating aselectelement.Returns any[]
get Supported Font Sizes
-
getSupportedFontSizes(): any[]
Returns the supported font sizes, as an array of
{value, desc}pairs.Returns any[]
get Supported Formats
-
getSupportedFormats(): string[]
Returns the supported image formats, i.e. ['gif', 'pdf', 'png', 'svg', 'emf'].
Returns string[]
set Background
-
setBackground(backgroundColor: string): Output
Sets the background colour of the rendered equation. Leave it empty for a transparent background, which is usually what you want when the equation sits on a coloured page.
EqEditor.Output.link('output', 'url')
.setBackground('red');Parameters
- backgroundColor: string
One of ['', 'white', 'black', 'red', 'green', 'blue'].
Returns Output
This Output, in support of chaining commands together.
- backgroundColor: string
set Dpi
-
setDpi(dpi: string): Output
Sets the resolution of the rendered equation, in dots per inch. This is ignored for 'svg', which is resolution independent.
EqEditor.Output.link('output', 'url')
.setDpi('110');Parameters
- dpi: string
One of ['50', '80', '100', '110', '120', '150', '200', '300'].
Returns Output
This Output, in support of chaining commands together.
- dpi: string
set Export Format
-
setExportFormat(format: string): Output
Sets the export format of the Output, i.e. the way the LaTeX is written out for pasting into another system.
Parameters
- format: string
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Returns Output
This Output, in support of chaining commands together.
- format: string
set Font
-
setFont(font: string): Output
Sets the font used to render the equation. The rendering service currently supports: (default) - Latin Modern jvn - Verdana cmb - Computer Modern phv - Helvetica phn - Helvetica Neue tx - tx Sans Serif px - px Sans Serif cs - Comic Sans
EqEditor.Output.link('output', 'url')
.setFont('tx');Parameters
- font: string
One of ['','jvn','cmb','phv','phn','tx','px','cs'].
Returns Output
This Output, in support of chaining commands together.
- font: string
set Font Size
-
setFontSize(fontSize: string): Output
Sets the font size of the Output.
EqEditor.Output.link('output', 'url')
.setFontSize('LARGE');Parameters
- fontSize: string
One of ['tiny', 'small', '', 'large', 'LARGE', 'huge'].
Returns Output
This Output, in support of chaining commands together.
- fontSize: string
set Format
-
setFormat(format: string): Output
Sets the image format of the Output, by changing the URL used to request the rendered equation. For example 'gif' requests 'https://latex.codecogs.com/gif.image?', whereas 'svg' requests 'https://latex.codecogs.com/svg.image?'.
EqEditor.Output.link('output', 'url')
.setFormat('pdf');Parameters
- format: string
One of ['gif', 'pdf', 'png', 'svg', 'emf'].
Returns Output
This Output, in support of chaining commands together.
- format: string
set Inline
-
setInline(flag?: boolean): Output
Sets the inline mode of the Output. An inline equation is vertically aligned to sit on the baseline of the surrounding text, rather than being rendered as a standalone display equation.
EqEditor.Output.link('output', 'url')
.setInline(true);Parameters
- flag: boolean = true
True to render inline, false for a display equation.
Returns Output
This Output, in support of chaining commands together.
- flag: boolean = true
set Render URL
-
setRenderURL(renderURL: string): Output
Sets the URL of the rendering service used to create the preview of the active equation. The default is 'https://latex.codecogs.com'; you would typically change this to point at your own installation of the rendering service.
<div id="entry"></div>
<img id="output"/>
<script>
EqEditor.TextArea.link('entry')
.addOutput(EqEditor.Output.link('output', 'url')
.setRenderURL('https://latex.codecogs.com')
.setFont('phv');
);
</script>Parameters
- renderURL: string
Base URL of a CodeCogs equation rendering service.
Returns Output
This Output, in support of chaining commands together.
- renderURL: string
update Output
-
updateOutput(): void
Updates the Output to reflect new changes.
Returns void
Staticlatex To Math ML
-
latexToMathML(latex: string, withSpeech?: boolean, inline?: boolean): string
Converts a LaTeX string straight to presentation MathML, without needing an Output bound to a TextArea first.
Public and static because the editor is not the only thing that needs this: a host integration - a CKEditor or TinyMCE plugin storing the source LaTeX and regenerating its markup on the way out - has a string in hand and no editor in sight. Sharing this rather than each caller writing its own means every one of them produces the same markup.
Parameters
- latex: string
The equation source.
OptionalwithSpeech: boolean = falseWrap it with the generated spoken text, so a screen reader reads the mathematics rather than spelling out the source.
Optionalinline: boolean = falseProduce an inline equation - no display="block" on the root, limits beside a large operator rather than above and below it - for MathML that has to sit in a line of running text rather than on a line of its own.
Returns string
The MathML, or an empty string for an empty equation.
- latex: string
Staticlink
-
link(divOutputId?: string, outputFormat?: string): Output
Static constructor. Returns a new instance of Output linked to the specified HTML element. This approach is useful for chaining various output requirements together, e.g.
<div id="entry"></div>
<img id="output"/>
<script>
EqEditor.TextArea.link('entry')
.addOutput(EqEditor.Output.link('output', 'url')
.setFont('phv')
.setFontSize('LARGE')
);
</script>Parameters
- divOutputId: string = null
The id of the element the Output will render into.
- outputFormat: string = null
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Returns Output
The new Output, ready for further chained calls.
- divOutputId: string = null
History
The History class draws the undo, redo and clear buttons that accompany a TextArea, together with the toggle that enables or disables automatic bracket completion.
Attach it with TextArea.addHistoryMenu; the buttons enable and disable themselves as the undo stack changes.
<div id="history"></div>
<div id="input"></div>
<script>
EqEditor.TextArea.link('input', true)
.addHistoryMenu(new EqEditor.History('history'));
</script>
History
constructor
-
new History(historyMenuId: string): History
Creates an instance of History and renders its buttons into the given element.
Parameters
- historyMenuId: string
The id of the div the history menu is rendered into.
Returns History
- historyMenuId: string
set Active Text Area
-
setActiveTextArea(textArea: TextArea): void
Connects the History buttons to the given TextArea. You will not usually call this directly - TextArea.addHistoryMenu does it for you.
Parameters
- textArea: TextArea
The TextArea object to connect with.
Returns void
- textArea: TextArea
Parameter Details
The equation editor uses some specific parameter values to modify the behaviour of certain functions. These are listed below.
exportType
Used throughout the Output class to determine which format the equation is rendered in.
| latex | Raw LaTeX markup | 1+sin(x) |
| encoded | Encoded LaTeX markup | 1+%20sin(x) - Uses the JavaScript escape function and converts '+' to + |
| wp | WordPress markup | [latex]1+sin(x)[/latex] |
| phpBB | phpBB markup | [tex]1+sin(x)[/tex] |
| tw | TiddlyWiki | [img[http://latex.codecogs.com/gif.latex?1+sin(x)]] |
| url | URL link to equation | http://latex.codecogs.com/gif.latex?1+sin(x) |
| urlencoded | Encoded URL link to equation | http://latex.codecogs.com/gif.latex?1+sin%28x%29 |
| pre | HTML code using pre-tags | <pre xml:lang="latex">1+sin(x)</pre> |
| doxygen | Doxygen markup | \f[1+sin(x)\f] |
| html | HTML code for use on a webpage | <img src="latex.codecogs.com?gif.latex=1+sin(x)" /> |
| mathml | Presentation MathML | <math xmlns="http://www.w3.org/1998/Math/MathML">…</math> - the equation itself rather than a picture of it, so it scales, reflows and can be selected. |
| mathmlspeech | MathML with spoken text | <span role="math" aria-label="1 plus sin open paren x close paren"><math…></span> - the same markup wrapped so a screen reader reads the mathematics rather than spelling out the source. The label above is for 1+\sin(x); written as 1+sin(x) the letters are three separate variables, and are read as such, exactly as LaTeX would typeset them. |
The two MathML formats generate from the LaTeX rather than lifting the markup off the page, so what you get carries none of the editor’s own position annotations. Both are also offered by getSupportedExportFormats(), which returns every format with a label attached — build a menu from that and it picks up new formats as the API gains them rather than going quietly out of date.
dpi
Stands for Dots Per Inch, and is used throughout the Output class to determine the resolution an equation is rendered at. Note that a larger DPI also increases the size of the rendered image.
| 50 | Lowest Resolution |
| 80 | |
| 100 | |
| 110 | |
| 120 | |
| 150 | |
| 200 | |
| 300 | Highest Resolution |
More
Deployment — these options, arranged into working editors.
Design — which toolbar panels to include, and in what order.
User guide — what the finished editor looks like to the person using it.
fxTeX — displaying equations your users have already written.
CodeCogs®
The EqEditor Toolbar class is the heart of the CodeCogs Equation Editor. A Toolbar is linked to one or more editable TextAreas (or to a MathView), and provides shortcut buttons for common LaTeX commands. It can be customised to show any selection of panels, in any order, across one or more rows.
Every button face is rendered as live MathML by the editor's own LaTeX-to-MathML converter, so a button shows the shape of what it inserts - including the grey boxes that mark the empty arguments you are expected to fill in.
The toolbar is fully keyboard accessible: holding Alt reveals a shortcut badge on each panel, Alt plus that key opens the panel, Tab and Shift+Tab move between open panels, the arrow keys move around a panel's buttons, Enter or Space activates one, and Escape closes the panel.
Toolbar