Skip to content

bjoern-hempel/php-latex-2-png

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Latex2Png library

With this library you can directly convert latex formulas into images. Feel free to use it directly on github.com or in your web projects:

\begin{array}{c}\\A\,\end{array}{\underbrace{Latex^2_{Png}}_{library}}

1. Installation

It is possible to use this repository with Composer. Write a composer.json file to install this project:

user$ vi composer.json
{
    "name": "My Project",
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/bjoern-hempel/php-latex-2-png.git"
        }
    ],
    "require": {
        "ixno/php-latex-2-png": "dev-master"
    }
}
user$ composer install
user$ composer dumpautoload -o

To include all needed libraries to your project you can use vendor/autoload.php.

2. Requirements

The project requires at least all subsequent libraries:

  • latex
  • imagemagick

2.1 Debian / Ubuntu

Easy to install on Debian or Ubuntu:

user$ sudo apt install texlive-base biblatex texlive-latex-extra texlive-fonts-recommended
user$ sudo apt install ImageMagick

And of course also on other systems. ;) See the documentation of the corresponding system.

3. Usage

3.1 Browser

If you need the pictures directly embed in your web projects:

user$ vi index.php
<?php

include "vendor/autoload.php";

use Ixno\Latex2Png\Builder;

/* Some configs */
$cacheFolder = dirname(__FILE__).'/cache';
$useCache    = true;
$debug       = false;
$resolution  = 600;
$padding     = '1pt';
$formula     = '\sum_{i = 0}^{n} i = \frac{n(n + 1)}{2}';

if (array_key_exists('c', $_REQUEST)) {
    $useCache = $_REQUEST['c'] === '1' ? true : false;
}

if (array_key_exists('d', $_REQUEST)) {
    $debug = $_REQUEST['d'] === '1' ? true : false;
}

if (array_key_exists('r', $_REQUEST)) {
    $resolution = $_REQUEST['r'];
}

if (array_key_exists('p', $_REQUEST)) {
    $padding = $_REQUEST['p'];
}

if (array_key_exists('f', $_REQUEST)) {
    $formula = $_REQUEST['f'];
}

$builder = new Builder($cacheFolder, $formula, $useCache, $debug);

$builder->sendPNGToBrowser($resolution, $padding);

An image can now be created by requesting it in the browser:

https://latex.ixno.de/?r=300&f=E%3Dmc^2

3.2 Command Line

If you need the pictures directly within the command line:

TODO.. (add a cli example)

4. Examples

4.1 Mass–energy equivalence

4.1.1 Latex

E = m \cdot c^2

4.1.2 Result

E = m \cdot c^2

4.2 Summation

4.2.1 Latex

\sum_{i = 0}^{n} i = \frac{n(n + 1)}{2}

4.2.2 Result

\sum_{i = 0}^{n} i = \frac{n(n + 1)}{2}

4.3 Damped oscillator

4.3.1 Latex

\ddot x(t) +2 \delta \cdot \dot x(t) + \omega_0^2 \cdot x(t) = 0 \hspace{0.5cm} \Bigg\vert
\hspace{0.2cm} \delta=\frac{d}{2m} \hspace{0.2cm} and \hspace{0.2cm} \omega_0=\sqrt{\frac{k}{m}}
\hspace{0.2cm} and \hspace{0.2cm} \ddot x(t) = \frac{\text{d}^2}{\text{d}t^2}x(t) \hspace{0.2cm}
and \hspace{0.2cm} \dot x(t) = \frac{\text{d}}{\text{d}t}x(t)

4.3.2 Result

\ddot x(t) +2 \delta \cdot \dot x(t) + \omega_0^2 \cdot x(t) = 0 \hspace{0.5cm} \Bigg\vert \hspace{0.2cm} \delta=\frac{d}{2m} \hspace{0.2cm} and \hspace{0.2cm} \omega_0=\sqrt{\frac{k}{m}} \hspace{0.2cm} and \hspace{0.2cm} \ddot x(t) = \frac{\text{d}^2}{\text{d}t^2}x(t) \hspace{0.2cm} and \hspace{0.2cm} \dot x(t) = \frac{\text{d}}{\text{d}t}x(t)

4.4 Bellman equation

4.4.1 Latex

V^*(s) = \substack{\textbf{max}\\ {\tiny a}}\sum_{s'}^{} T(s, a, s')[R(s, a, s') +
\gamma \cdot V^*(s')] \quad \forall s

4.4.2 Result

V^*(s) = \substack{\textbf{max}\\ {\tiny a}}\sum_{s'}^{} T(s, a, s')[R(s, a, s') + \gamma \cdot V^*(s')] \quad \forall s

4.5 Schrödinger equation

4.5.1 Latex

\mathrm{i} \hbar \partial_t \ket{\psi_{(\textbf{x}, t)}} = \hat{\mathcal{H}}_{(\hat{\textbf{p}},
\hat{\textbf{x}})} \ket{\psi_{(\textbf{x}, t)}} \hspace{0.5cm} \Bigg\vert \hspace{0.2cm}
\hbar = \frac{h}{2\pi} \hspace{0.2cm} and \hspace{0.2cm} \hat{\mathcal{H}}_{(\hat{\textbf{p}},
\hat{\textbf{x}})} = -\frac{\hat{\textbf{p}}^2}{2m} + V_{(\hat{\textbf{x}})}

4.5.2 Result

\mathrm{i} \hbar \partial_t \ket{\psi_{(\textbf{x}, t)}} = \hat{\mathcal{H}}_{(\hat{\textbf{p}}, \hat{\textbf{x}})} \ket{\psi_{(\textbf{x}, t)}} \hspace{0.5cm} \Bigg\vert \hspace{0.2cm} \hbar = \frac{h}{2\pi} \hspace{0.2cm} and \hspace{0.2cm} \hat{\mathcal{H}}_{(\hat{\textbf{p}}, \hat{\textbf{x}})} = -\frac{\hat{\textbf{p}}^2}{2m} + V_{(\hat{\textbf{x}})}

4.6 Derivation of the time-dependent Schrödinger equation from wave mechanics

4.6.1 Latex

See below

4.6.2 Result

See below

4.6.3 Wave equation

See Wave equation (Wikipedia)

(\partial_x^2-\frac{1}{c^2}\partial_t^2)\psi=0

(\partial_x^2-\frac{1}{c^2}\partial_t^2)\psi=0

4.6.4 Relativistic law of conservation of energy

Particles are considered to be massless (m = 0). For relativistic law of conservation of energy see Wikipedia (german)

E^2=c^2p^2+m^2c^4 \,\, \xrightarrow{\text{m=0}} \,\, -(p^2 - \frac{1}{c^2}E^2)=0

E^2=c^2p^2+m^2c^4 \,\, \xrightarrow{\text{m=0}} \,\, -(p^2 - \frac{1}{c^2}E^2)=0

The transformation of the equation is now somewhat similar to the wave function and shows the relationship between the classical mechanics and the relativistic mechanics (correspondence principle).

4.6.5 Correspondence principle

Derivation from 4.6.3 and 4.6.4. For correspondence principle see Correspondence principle (Wikipedia)

\begin{aligned} p {}\longrightarrow{} & \frac{1}{\mathrm{i}} \hbar
\partial_x \\ E {}\longrightarrow{} & \mathrm{i} \hbar \partial_t \\
\mathrm{i}^2 {}\longrightarrow{} & -1 \end{aligned}

\begin{aligned} p {}\longrightarrow{} & \frac{1}{\mathrm{i}} \hbar \partial_x \\ E {}\longrightarrow{} & \mathrm{i} \hbar \partial_t \\ \mathrm{i}^2 {}\longrightarrow{} & -1 \end{aligned}

Test: If we put this relationship into the relativistic energy equation, we get the classical wave equation.

4.6.6 Total energy

The total amount of energy is the sum of the kinetic energy and the potential energy:

\frac{p^2}{2m} + V = E

\frac{p^2}{2m} + V = E

4.6.7 Use correspondence principle

-\frac{\hbar^2 \partial_x^2}{2m}+V = \mathrm{i} \hbar \partial_t \hspace{0.5cm}
\Bigg\vert \hspace{0.2cm} \cdot \psi_{(x,t)}

-\frac{\hbar^2 \partial_x^2}{2m}+V = \mathrm{i} \hbar \partial_t \hspace{0.5cm} \Bigg\vert \hspace{0.2cm} \cdot \psi_{(x,t)}

-\frac{\hbar^2 \partial_x^2}{2m} \psi_{(x,t)}+V \psi_{(x,t)} = \mathrm{i}
\hbar \partial_t \psi_{(x,t)}

-\frac{\hbar^2 \partial_x^2}{2m} \psi_{(x,t)}+V \psi_{(x,t)} = \mathrm{i} \hbar \partial_t \psi_{(x,t)}

Now we have the time-dependent Schrödinger equation.

4.7 Gaussian error integral

4.7.1 Latex

\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}

4.7.2 Result

\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}

4.8 Eulerian identity

4.8.1 Latex

e^{\mathrm{i}\cdot\pi} + 1 = 0 \hspace{0.5cm} \Bigg\vert \hspace{0.2cm}
e^{\mathrm{i}\cdot\pi} = {\underbrace{cos(\pi)}_{=-1}} + {\underbrace{\mathrm{i} \cdot sin(\pi)}_{=0}}

4.8.2 Result

e^{\mathrm{i}\cdot\pi} + 1 = 0 \hspace{0.5cm} \Bigg\vert \hspace{0.2cm} e^{\mathrm{i}\cdot\pi} = {\underbrace{cos(\pi)}_{=-1}} + {\underbrace{\mathrm{i} \cdot sin(\pi)}_{=0}}

4.9 Euler product

4.9.1 Latex

{\underbrace{\sum_{n=1}^\infty {1 \over n^s}}_{Riemannsche \hspace{0.1cm} Zeta-Funktion}}
= {\underbrace{\prod_p {1 \over {1 - {1 \over {p^s}}}}}_{p \in \mathbb{P} \hspace{0.2cm}
\wedge \hspace{0.2cm} s \hspace{0.1cm} > \hspace{0.1cm} 1}}

4.9.2 Result

{\underbrace{\sum_{n=1}^\infty {1 \over n^s}}_{Riemannsche \hspace{0.1cm} Zeta-Funktion}} = {\underbrace{\prod_p {1 \over {1 - {1 \over {p^s}}}}}_{p \in \mathbb{P} \hspace{0.2cm} \wedge \hspace{0.2cm} s \hspace{0.1cm} > \hspace{0.1cm} 1}}

5. Check for errors

If there is an error in the formula, you will get the following picture:

Error\,(see\,error.log)

Check the web server error files to locate the error (example):

...
[Mon Aug 20 21:59:43.406729 2018] [:error] [pid 174] [client 172.17.0.1:33807] ) (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsa.fd)
[Mon Aug 20 21:59:43.406734 2018] [:error] [pid 174] [client 172.17.0.1:33807] (/usr/share/texlive/texmf-dist/tex/latex/amsfonts/umsb.fd)
[Mon Aug 20 21:59:43.406738 2018] [:error] [pid 174] [client 172.17.0.1:33807]
[Mon Aug 20 21:59:43.406742 2018] [:error] [pid 174] [client 172.17.0.1:33807] LaTeX Warning: Command \\" invalid in math mode on input line 16.
[Mon Aug 20 21:59:43.406747 2018] [:error] [pid 174] [client 172.17.0.1:33807]
[Mon Aug 20 21:59:43.406751 2018] [:error] [pid 174] [client 172.17.0.1:33807] ! Please use \\mathaccent for accents in math mode.
[Mon Aug 20 21:59:43.406756 2018] [:error] [pid 174] [client 172.17.0.1:33807] \\add@accent ...@spacefactor \\spacefactor }\\accent
[Mon Aug 20 21:59:43.406761 2018] [:error] [pid 174] [client 172.17.0.1:33807]                                                   #1 #2\\egroup \\spacefactor ...
[Mon Aug 20 21:59:43.406766 2018] [:error] [pid 174] [client 172.17.0.1:33807] l.16 E=m\xc3\xb6
[Mon Aug 20 21:59:43.406771 2018] [:error] [pid 174] [client 172.17.0.1:33807]           c^2
[Mon Aug 20 21:59:43.406776 2018] [:error] [pid 174] [client 172.17.0.1:33807] !  ==> Fatal error occurred, no output PDF file produced!
[Mon Aug 20 21:59:43.406781 2018] [:error] [pid 174] [client 172.17.0.1:33807] Transcript written on /var/www/de/ixno/latex/php-latex-2-png/examples/cache/09f
[Mon Aug 20 21:59:43.406786 2018] [:error] [pid 174] [client 172.17.0.1:33807] 87da0b0e7a03f691d5e4e2d2165b0.log.

Or use the debug parameter (d=1) to see the log directly on screen:

https://latex.ixno.de/?c=0&r=300&f=E%3Dm%C3%B6c^2&d=1

6. The way it works (the technique)

If you don't like PHP and you want to build your own framework or you are just interested in how it works, you will find the following command line commands for creating images from tex files. This is the background of this library:

user$ vi latex.tex
\documentclass[border={1pt 1pt 1pt 1pt}]{standalone} % <- The padding arround the formula.
\nofiles
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\usepackage{color}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pst-plot}
\usepackage{physics}
\begin{document}
\pagestyle{empty}
$\displaystyle
E = m \cdot c^2 % <- The formula here at this point.
$
\end{document}

Create a pdf document latex.pdf (intermediate step):

user$ pdflatex latex.tex

Create the png image latex.png from latex.pdf:

user$ convert -density 300 latex.pdf -quality 100 latex.png

That's it. Enjoy!

7. Tools

A. Authors

B. Licence

This tutorial is licensed under the MIT License - see the LICENSE.md file for details

C. Closing words

Have fun! :)

About

Create png files from given latex formulas.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages