PHPackages                             akram-zaitout/laravel-katex - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. akram-zaitout/laravel-katex

ActiveLibrary[Templating &amp; Views](/categories/templating)

akram-zaitout/laravel-katex
===========================

A comprehensive Laravel package for rendering beautiful mathematical expressions using KaTeX

1.3.3(2mo ago)17[1 issues](https://github.com/akramzaitout/laravel-katex/issues)MITPHPPHP ^7.2|^8.0CI passing

Since Jan 29Pushed 2mo agoCompare

[ Source](https://github.com/akramzaitout/laravel-katex)[ Packagist](https://packagist.org/packages/akram-zaitout/laravel-katex)[ Fund](https://www.buymeacoffee.com/akramzaitout)[ GitHub Sponsors](https://github.com/akramzaitout)[ RSS](/packages/akram-zaitout-laravel-katex/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (18)Used By (0)

Laravel KaTeX
=============

[](#laravel-katex)

 [![Laravel KaTeX Package Logo](./assets/logo.png)](./assets/logo.png)

 [ ![Latest Version on Packagist](https://camo.githubusercontent.com/88cbdd620342bc21831fb0cd05a7b3d2d19066481c7ea512e76244f6eea3838c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616b72616d2d7a6169746f75742f6c61726176656c2d6b617465783f7374796c653d666c61742d737175617265) ](https://packagist.org/packages/akram-zaitout/laravel-katex) [ ![Total Downloads](https://camo.githubusercontent.com/b52c060a99573d3a3c1b82cceb8a94377e405bb231b4641e2217589a7d064b4e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616b72616d2d7a6169746f75742f6c61726176656c2d6b617465783f7374796c653d666c61742d737175617265) ](https://packagist.org/packages/akram-zaitout/laravel-katex) [ ![License](https://camo.githubusercontent.com/f3725eac761f5c47388028b7bfab629188812cf7227e4cc7d3fb9d2736118c03/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f616b72616d2d7a6169746f75742f6c61726176656c2d6b617465783f7374796c653d666c61742d737175617265) ](https://packagist.org/packages/akram-zaitout/laravel-katex)

A comprehensive, production-ready Laravel package for rendering beautiful mathematical expressions using [KaTeX](https://katex.org/). Native PHP implementation with dependency injection, following Laravel best practices.

✨ Features
----------

[](#-features)

- � **Flexible &amp; Intuitive** - Use it your way with Blade directives, components, or our Facade—whatever fits your coding style.
- 🔒 **Secure by Design** - We handle XSS protection and validation automatically so you can render user content with peace of mind.
- ⚙️ **Fully Customizable** - Tweak delimiters, error handling, and macros globally or on the fly to match your specific needs.
- 🚀 **Blazing Fast** - Optimized rendering ensures your mathematical expressions load instantly without slowing down your app.
- 🧪 **Production Ready** - Built with a comprehensive test suite and strict typing to ensure reliability in mission-critical applications.

📋 Requirements
--------------

[](#-requirements)

- PHP 7.2 or higher
- Laravel 6.0 or higher
- ext-json

📦 Installation
--------------

[](#-installation)

Install the package via Composer:

```
composer require akram-zaitout/laravel-katex
```

### Publish Configuration (Optional)

[](#publish-configuration-optional)

```
php artisan vendor:publish --tag=katex-config
```

This creates `config/katex.php` where you can customize all aspects of the package.

### Publish Views (Optional)

[](#publish-views-optional)

```
php artisan vendor:publish --tag=katex-views
```

🚀 Quick Start
-------------

[](#-quick-start)

### Basic Setup

[](#basic-setup)

In your Blade layout file (e.g., `resources/views/layouts/app.blade.php`):

```

    My Application

    @katexStyles

    @yield('content')

    @katexScripts

```

### Using Blade Directives

[](#using-blade-directives)

```
{{-- Inline math --}}
The quadratic formula is @katex('x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}')

{{-- Display (block) math --}}
@katexBlock('\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}')

{{-- With variables --}}
@php
    $equation = 'E = mc^2';
@endphp
Einstein's equation: @katex($equation)
```

📖 Usage
-------

[](#-usage)

### 1. Blade Directives

[](#1-blade-directives)

#### @katexStyles

[](#katexstyles)

Renders the KaTeX CSS stylesheet:

```
@katexStyles
```

#### @katexScripts

[](#katexscripts)

Renders KaTeX JavaScript with optional configuration:

```
{{-- Default options --}}
@katexScripts

{{-- Custom options --}}
@katexScripts([
    'delimiters' => [
        ['left' => '$', 'right' => '$', 'display' => false],
        ['left' => '$$', 'right' => '$$', 'display' => true],
    ],
    'throwOnError' => false,
    'macros' => [
        '\\RR' => '\\mathbb{R}',
    ]
])
```

#### @katex

[](#katex)

Renders inline mathematical expressions:

```
@katex('a^2 + b^2 = c^2')
```

#### @katexBlock

[](#katexblock)

Renders display mode (block) mathematical expressions:

```
@katexBlock('\sum_{i=1}^{n} i = \frac{n(n+1)}{2}')
```

### 2. Facade

[](#2-facade)

Use the `Katex` facade for programmatic access:

```
use AkramZaitout\LaravelKatex\Facades\Katex;

// Generate stylesheet
$styles = Katex::generateStylesheet();

// Generate scripts
$scripts = Katex::generateScripts(['throwOnError' => false]);

// Wrap expressions
$inline = Katex::wrapInline('x^2');
$display = Katex::wrapDisplay('\int_0^\infty');

// Get configuration
$version = Katex::getConfig('version');
```

### 3. Helper Functions

[](#3-helper-functions)

```
// Get renderer instance
$renderer = katex();

// Render inline math
$inline = katex_inline('a^2 + b^2 = c^2');

// Render display math
$display = katex_display('\sum_{i=1}^{n} i');

// Generate assets
$styles = katex_styles();
$scripts = katex_scripts(['throwOnError' => false]);
```

### 4. Blade Component

[](#4-blade-component)

```

```

### 5. Service Injection

[](#5-service-injection)

```
use AkramZaitout\LaravelKatex\Services\KatexRenderer;

class MathController extends Controller
{
    public function __construct(
        protected KatexRenderer $katex
    ) {}

    public function show()
    {
        $expression = $this->katex->wrapInline('E=mc^2');

        return view('math.show', compact('expression'));
    }
}
```

⚙️ Configuration
----------------

[](#️-configuration)

### Environment Variables

[](#environment-variables)

Set these in your `.env` file:

```
KATEX_VERSION=0.16.28
KATEX_CDN=https://cdn.jsdelivr.net/npm/katex
KATEX_CSS_INTEGRITY=sha384-Wsr4Nh3yrvMf2KCebJchRJoVo1gTU6kcP05uRSh5NV3sj9+a8IomuJoQzf3sMq4T
KATEX_JS_INTEGRITY=sha384-+W9OcrYK2/bD7BmUAk+xeFAyKp0QjyRQUCxeU31dfyTt/FrPsUgaBTLLkVf33qWt
KATEX_AUTO_RENDER_INTEGRITY=sha384-hCXGrW6PitJEwbkoStFjeJxv+fSOOQKOPbJxSfM6G5sWZjAyWhXiTIIAmQqnlLlh
```

### Configuration File

[](#configuration-file)

Publish and edit `config/katex.php`:

```
return [
    'version' => '0.16.28',
    'cdn' => 'https://cdn.jsdelivr.net/npm/katex',

    'options' => [
        'delimiters' => [
            ['left' => '$$', 'right' => '$$', 'display' => true],
            ['left' => '\\(', 'right' => '\\)', 'display' => false],
        ],
        'throwOnError' => false,
        'errorColor' => '#cc0000',
        'macros' => [
            '\\RR' => '\\mathbb{R}',
            '\\NN' => '\\mathbb{N}',
        ],
    ],
];
```

🌍 Local Asset Management (Offline Support)
------------------------------------------

[](#-local-asset-management-offline-support)

This package supports downloading KaTeX assets to your local application, allowing you to run without external CDN dependencies. This is perfect for offline environments, intranets, or strict privacy compliance/GDPR.

### 1. Download Assets

[](#1-download-assets)

Run the artisan command to download CSS, JS, and font files to `public/vendor/katex`:

```
php artisan katex:download
```

You can also specify a specific version:

```
php artisan katex:download --version=0.16.0
```

### 2. Enable Local Assets

[](#2-enable-local-assets)

Update your `.env` file to tell the package to use the local files:

```
KATEX_USE_LOCAL_ASSETS=true
```

Or configure it in `config/katex.php`:

```
'use_local_assets' => true,
```

🎓 Examples
----------

[](#-examples)

```

    Calculus Fundamentals

    The derivative of @katex('x^n') is @katex('nx^{n-1}').

    Example
    @katexBlock('\frac{d}{dx}(x^3) = 3x^2')

    The Power Rule
    @katexBlock('\frac{d}{dx}(x^n) = nx^{n-1}')

```

🔒 Security
----------

[](#-security)

### XSS Protection

[](#xss-protection)

All user input is automatically escaped using Laravel's `e()` helper:

```
{{-- Safe: HTML is escaped --}}
@katex($userInput)
```

### Subresource Integrity

[](#subresource-integrity)

SRI hashes ensure CDN files haven't been tampered with:

```
'css_integrity' => 'sha384-Wsr4Nh3yrvMf2KCebJchRJoVo1gTU6kcP05uRSh5NV3sj9+a8IomuJoQzf3sMq4T',
```

### Trust Settings

[](#trust-settings)

By default, `\url` and `\href` commands are disabled. Enable only if needed:

```
'options' => [
    'trust' => false, // Keep disabled for user-generated content
],
```

📝 License
---------

[](#-license)

This package is open-sourced software licensed under the [MIT license](LICENSE.md).

---

Made with ❤️ by [Akram Zaitout](https://github.com/akramzaitout)

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance67

Regular maintenance activity

Popularity7

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 78.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Recently: every ~9 days

Total

17

Last Release

64d ago

Major Versions

0.0.1 → 1.0.32026-01-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/ab57a9e64763e5f3e5a4b1ffebce3b382d9bd76be3e1034eaf482bfef7c3bb31?d=identicon)[akramzaitout](/maintainers/akramzaitout)

---

Top Contributors

[![akramzaitout](https://avatars.githubusercontent.com/u/23154385?v=4)](https://github.com/akramzaitout "akramzaitout (62 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (17 commits)")

---

Tags

bladeequationskatexlaravellatexmathmathematicsrenderinglaravelblademathematicsmathlatexRenderingequationskatex

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/akram-zaitout-laravel-katex/health.svg)

```
[![Health](https://phpackages.com/badges/akram-zaitout-laravel-katex/health.svg)](https://phpackages.com/packages/akram-zaitout-laravel-katex)
```

###  Alternatives

[robsontenorio/mary

Gorgeous UI components for Livewire powered by daisyUI and Tailwind

1.5k454.7k15](/packages/robsontenorio-mary)[radic/blade-extensions

Laravel package providing additional Blade extensions: foreach (with $loop data like twig), break, continue, set,array (multiline), etc

271321.7k5](/packages/radic-blade-extensions)[stijnvanouplines/blade-country-flags

A package to easily make use of country flags in your Laravel Blade views.

26307.2k6](/packages/stijnvanouplines-blade-country-flags)[saade/blade-iconsax

A package to easily make use of Iconsax in your Laravel Blade views.

21138.5k](/packages/saade-blade-iconsax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
