KaTeX Shortcode

Below, you’ll find a detailed guide on how to add the KaTeX Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the KaTeX Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the KaTeX Plugin and the shortcodes it provides:

Plugin Icon

"KaTeX plugin is an outstanding tool for WordPress that allows seamless integration of high-speed, beautiful math in your posts. Display complex equations easily with KaTeX!"

★★★★★ (14) Active Installs: 3000+ Tested with: 6.1.4 PHP Version: 5.3
Included Shortcodes:
  • [katex]

KaTeX [katex] Shortcode

The ‘katex’ shortcode is part of the KaTeX plugin in WordPress. It renders mathematical expressions in your posts. The shortcode function ‘katex_display_inline_render’ takes attributes and content as parameters. The ‘display’ attribute is set to false by default. If ‘display’ is true, the equation will be displayed in a block format. The function replaces any break tags in the content with newline characters. It then encodes the content to prevent HTML injection. Finally, it returns a span element with the class ‘katex-eq’, the ‘data-katex-display’ attribute set to the display value, and the encoded content as its inner HTML.

Shortcode: [katex]


Here is a list of all possible katex shortcode parameters and attributes:

  • display – determines if the equation will be displayed inline or block.

Examples and Usage

Basic example – The shortcode is used to display a mathematical equation using the KaTeX plugin. The equation is written inside the shortcode tags.

[katex]E = mc^2[/katex]

Advanced examples

Displaying a mathematical equation in ‘display’ mode. The ‘display’ attribute is set to true, which means the equation will be displayed in a standalone block, centered and on its own line.

[katex display=true]a^2 + b^2 = c^2[/katex]

Displaying a mathematical equation in ‘inline’ mode. The ‘display’ attribute is set to false, which means the equation will be displayed inline with the surrounding text.

[katex display=false]F = ma[/katex]

It’s important to note that the default value of the ‘display’ attribute is false. So, if the ‘display’ attribute is not specified, the equation will be displayed inline by default.

PHP Function Code

In case you have difficulties debugging what causing issues with [katex] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('katex', 'katex_display_inline_render');

Shortcode PHP function:

                    function katex_display_inline_render($attributes, $content = null) {
    global $katex_resources_required;
    $katex_resources_required = true;

    $attributes = shortcode_atts(
            'display' => false

    // Truthy -> 'true', falsy -> 'false'
    $display = $attributes['display'] ? 'true' : 'false';

    $content = preg_replace("|<br\s*/?>|", "\n", $content);
    $encoded = htmlspecialchars(html_entity_decode($content));
    return sprintf('<span class="katex-eq" data-katex-display="%s">%s</span>', $display, $encoded);

Code file location:



Now that you’ve learned how to embed the KaTeX Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.


Leave a Reply

Your email address will not be published. Required fields are marked *