Copy Link
Add to Bookmark
Report

How to render formulas

asbness's profile picture
Published in 
apache
 · 11 Nov 2023

This is a small example of an HTML page rendering Latex equations with Katex on browser side.

<!DOCTYPE html> 
<html lang="en">
<head>
<title>Equations</title>
<link rel="stylesheet" href="./katex.css">
<script src="./katex.js"></script>
<script defer src="./auto-render.js"
onload="renderMathInElement(document.body);"></script>

<style>
/* optional - uncomment and customise next line to change maths font size
.katex { font-size: 1em !important; }
*/

</style>
</head>

<body>


Find $$\{ x, y, z \} \in \N$$ where:
$$x^2 + y^2 = z^2$$


\begin{equation}
\begin{split}
(a - b)^2 &= (a - b)(a - b) \\
&= a(a - b) - b(a - b) \\
&= a^2 -ab -ba + b^2 \\
&= a^2 + 2ab + b^2 \nonumber
\end{split}
\end{equation}


</body>
</html>

This is the node.js version

<!DOCTYPE html> 
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Equations</title>
<link rel="stylesheet" href="./katex.css">
<script src="./katex.js"></script>
<style>
/* */
</style>
</head>

<body>


<?php
$mathExpression = "x^2 + y^2 = z^2";
?>


<!-- Use KaTeX to render dynamic content -->
<div id="renderedContent"></div>
<script>
// Get the dynamic content from PHP variable
var mathExpression = <?php echo json_encode($mathExpression); ?>;

// Render the content using KaTeX
katex.render(mathExpression, document.getElementById('renderedContent'));
</script>


</body>
</html>

← previous
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT