The Equation Editor API (Application Programming Interface) provides a mechanism to create custom equation editor and to seamlessly integrated the editor within any HTML web page. This page show the essential steps to creating an editor tool bar, customising the panels, and interfacing with content on your page.
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>
Creates an instance of TextArea for LaTeX input and associates this with an HTML element. To provide some introductory text use the `placeholder`` tag as illustrated in the example below. Any introductory text will be removed once the user starts typing.
<div id="input" placeholder="Write LaTeX here"></div>
<script>
var ta = new EqEditor.TextArea('input', true);
</script>
Id of the target div to be used for editing.
Activates cursor within the text area.
Adds history, with undo and redo, to the TextArea. This requires a history menu to provide the necessary control
The history menu object
Connects an Output component to the current TextArea.
The Output component
Connects a Toolbar component to the current Textarea.
The Toolbar component.
Optional
activate: boolean = trueInforms the toolbar to send commands to this textArea
NEW!
Provides a convienent method of executing any Output class function for all Ouput areas associated with the TextArea. This is particular useful when you have multiple output and you need them all to change.
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>
Clears the input box (TextArea)
Call if you are dynamically displaying the editor and select to 'close' a TextArea. This function will clean up any extra DOM elements.
Returns the current TextArea object.
Typically you will then want to retrieve the textContent
to 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)" />
Insert commands into the active HTML element at the current cursor position. 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>
New text to insert
Optional
newCaretPos: number = nullWhere to place cursor following the insertion of text. 0 is the start.
Optional
selectedInsertPos: number = nullWhere to insert existing selected text within the newly inserted text.
Returns true if the current TextArea is empty, false otherwise.
Generates the LaTeX for an array with the specified dimensions and inserted this into the active TextArea.
Sets the type of array, e.g. pmatrix
Start token
End token
Generates the LaTeX for a Matrix and inserts this into the active TextArea.
Type of matrix to generate e.g. pmatrix
If true indicates the equation is numbered.
If true indicates the equation is conditional.
NEW!
Notifies all connected History Menus of changes.
NEW! Notifies all connected Outputs of changes.
Pushes any text from the current TextArea to the History stack. Which allows further changes to be undone.
Reverts an undone change.
NEW! Restricts keyboard input, with three current modes: 0 - allows all text an key operations 1 - allows delete and space key. Other inputs must come from the toolbar 2 - All inputs must come from the toolbar
NEW! Assigned allowable commands that can be used within the formula. Any variables breaking this conditions are assigned the class "CCrestricted" which by default causes them to be shown in red and underlined. Override the CSS for "CCrestricted" to achieve alternative effects.
Array of allowable words. RegEx expressions can also be used.
NEW!
Assigned allowable variables that can be used within the formula. Any variables breaking this conditions are assigned the class "CCrestricted" which typically causes them to be red and undelined.
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'])
Array of allowable words. RegEx expressions can also be used.
Undoes the last action. Repeated called will unwind changes to the input area.
Changes the dimensions of an existing selected matrix
The selected text
NEW! Check the equation to ensire it only contians permitted variables and functions. Return true or false accordingly.
Static
linkStatic constructor.
Returns a new instance of TextArea linked to the specified HTML element. The advantage of this over the use of constructor with 'new' is that it allows you chain additional requirments, as shown in the example:
<div id="input"></div>
<script>
EqEditor.TextArea.link('input', true)
.addToolbar(new EqEditor.Toolbar('toolbar'), true)
</script>
Id of the target div to be used for editing.
Activates cursor within the text area
This class is responsible to generate the History buttons.
Creates an instance of History.
The div's id that history menu will be rendered.
Connects the History buttons with the TextArea provided as parameter.
The TextArea object to connect with
The EqEditor Output class provides a means of extracting content from the editable TextArea's in a variety of forms. Initially this is used to provide an example of the fully rendered equation as a graphical image. Output can also format the LaTeX for inclusion in third-party blogs, forums.
One or more Outputs is attached to a TextArea. The Output can then be configured with output formats, fonts, fontsize, dpi, etc.
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 - Word Press. phpBB - php Bulletin Board. tw - Tiny Wiki. url - Unique web address of the rendered image. urlencoded - Same as url by with additional encoding of special characters. pre - HTML pre element with xml language assigned to latex. Used by some forums. doxygen - DOxygen format. html - HTML image element. latex - Raw latex.
The Div Id that the Output class will render.
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Connects an export area with the current Output and returns this Output Object.
The Div Id of the Export Area.
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Links Output to a TextArea so it will display the formatted equation for that text.
The TextArea Id
Downloads the current equation.
Optional
filename: string = ...Interface function for exporting the equation, returning a text string in the appropriate type.
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Returns the equation in a compact form, removing all redundant spaces.
Returns the equation as a text string.
If true, downloads rendered equation, otherwise returns image
Returns the supported export options.
Returns the current inline status.
Returns the supported output colours.
Returns the supported output DPI.
Returns the supported output font sizes.
Returns the supported output fonts.
Returns the supported output formats.
Sets the Background Color of the Output.
EqEditor.Output.link('output', 'url')
.setBackground('red');
One of ['', 'white', 'black', 'red', 'green', 'blue']
Sets the Dpi of the Output.
EqEditor.Output.link('output', 'url')
.setDpi('110');
One of ['50', '80', '100', '110', '120', '150', '200', '300']
Sets the Format of the Output.
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
Sets the font of the Output. Current the rendering service 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');
One of ['','jvn','cmb','phv','phn','tx','px','cs']
Sets the font size of the Output.
EqEditor.Output.link('output', 'url')
.setFontSize('LARGE');
One of ['tiny', 'small', '', 'large', 'LARGE', 'huge']
Sets the graphical Format of the Output, but changing the url of the request to the equation rendering service. For example 'gif' uses 'http://latex.codecogs.com/gif.latex?', where as 'svg' uses 'http://latex.codecogs.com/svg.latex?'
EqEditor.Output.link('output', 'url')
.setFormat('pdf');
One of ['gif', 'pdf', 'png', 'svg', 'emf']
Sets the Inline mode of the Output.
EqEditor.Output.link('output', 'url')
.setInline(true);
Value to set inline mode
Sets the URL which will render the active equation. The default is to use 'https://latex.codecogs.com', and you would typically change this to use custom installation of the rendering service.
URL formatted for CodeCogs equation renderer
<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>
Updates the Output to reflect new changes.
Static
linkStatic 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>
The Div Id that the Output class will render
One of [wp, phpBB, tw, url, urlencoded, pre, doxygen, html, latex]
The EqEditor Toolbar class is the core to CodeCogs Equation Editor and linked to one more more editable TextArea's. The toolbar provide shortcuts to common LaTeX commands and can be customised to show a range of panels and layouts.
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 a range of standard toolbars. Currently these include: 'full', 'mini', 'chemistry'. e.g.
var tb = new EqEditor.Toolbar('toolbar','mini');
Alternative you can select the panels required in any order, e.g.
var tb = new EqEditor.Toolbar('toolbar', '[["operators_core","greeklower","chemistry_arrows",
"mhchem","mhchem_arrows","colors"]]');
Id of the the layer into which the toolbar is placed.
The toolbar design or JSON array with the panels to be used.
Optional scaling of toolbar buttons. Default=1.
Connects a TextArea with the current Toolbar. For example:
<div id="editor"></div>
<div id="input"></div>
<script>
EqEditor.Toolbar.link('editor', true)
.addTextArea(mew EqEditor.TextArea('input'), true)
</script>
Returns the current Toolbar object.
The TextArea object to associate with the Toolbar.
If true it also adds the Toolbar to the TextArea.
NEW!
Appends a new panel to the end of an existing toolbar with a custom set of buttons.
The function accepts either a single string, that contains the name of a in build toolbar name (e.g. "matrix"). Alternatively you can provide a object with the following parameters:
A button is defines with the following:
By default all text buttons are assigned the class CCButton, while any with images are assigned a 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"],
}
]
});
The id of the toolbar. Toolbar numbers start from 0.
The name of a standard panel or JSON object with details of the button.
Returns the HTMLDivElement for a equation editor toolbar panel using its name. This is typically required when dynamically moving a panel from an inactive panel onto the main toolbar.
The name of the panel to retrieve
Moves the editor from one location to another. This is useful if you want to provide dynamic position of the toolbar with multiple inputs. 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(mew EqEditor.TextArea('input1'), true)
.addTextArea(mew EqEditor.TextArea('input2'), true);
</script>
return itself in support of the chaining commands together.
Id of the the layer into which the toolbar is to be moved
Make the specified TextArea active, such that it recieves input from the toolbar and keyboard.
Returns this (the current Toolbar object) in support of chained commands.
The TextArea to make active.
Static
linkA static constructor that places the Equation Edittor toolbar on the page and returns new Toolbar object. This is a convenient alternative to using 'new EqEditor.Toolbar' as it allows you to chain associsated 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 a range of standard toolbars. Currently these include: 'full', 'mini', 'chemistry'. e.g.
EqEditor.Toolbar('toolbar','chemistry');
Alternative you can select the panels required in any order, e.g.
EqEditor.Toolbar('toolbar', [["operators_core","greeklower","chemistry_arrows",
"mhchem","mhchem_arrows","colors"]]);
Id of the the layer into which the toolbar is placed.
The toolbar design or JSON array with the panels to be used.
Optional scaling of toolbar buttons. Default=1.
The equation editor uses some specific parameter values to modify the behaviour of certain functions. Below are the
Used throughout the Output Class to determine which format to render the equations in.
latex | Raw LaTeX markup | 1+sin(x) |
encoded | Encoded LaTeX markup | 1+%20sin(x) - Uses the javascript escape function and converts '+' rto + |
wp | Wordpress markup | [latex]1+sin(x)[/latex] |
phpBB | phpBB markup | [tex]1+sin(x)[/tex] |
tw | Tiddly Wiki | [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)" /> |
Stands for Dots Per Inch and is used throughout the Output Class to determine the resolution to render equations in. Note that a larger DPI often increases the size of the rendered equation as well.
50 | Lowest Resolution |
80 | |
100 | |
110 | |
120 | |
150 | |
200 | |
300 | Highest Resolution |
The TextArea class is used to manage connection between the equation editor toolbar and editable areas within which the mathematical LaTeX markup is shown and can be edited. These editable ares are formed from HTML 'div' elments.
The key features the TextArea manages includes
The TextArea is usually associated with a equation editor Toolbar, to provide visual assistance. Multiple TextArea's can be attached to single Toolbar. Conversely you may also attached multiple Toolbars to a single TextArea.
The TextArea can be linked to Output regions that present rendered versions of the code, or provide the LaTeX in a form to be exported into another system (e.g. a forum, Google docs).