Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bjoern-hempel/php-latex-2-png

Create png files from given latex formulas.
https://github.com/bjoern-hempel/php-latex-2-png

latex mit-license php

Last synced: 20 days ago
JSON representation

Create png files from given latex formulas.

Awesome Lists containing this project

README

        

# 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](https://getcomposer.org/download/). Write a `composer.json` file to install this project:

```shell
user$ vi composer.json
```

```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"
}
}
```

```shell
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](https://de.wikipedia.org/wiki/Debian) or [Ubuntu](https://de.wikipedia.org/wiki/Ubuntu):

```shell
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:

```shell
user$ vi index.php
```

```php
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

```latex
E = m \cdot c^2
```

#### 4.1.2 Result

E = m \cdot c^2

### 4.2 Summation

#### 4.2.1 Latex

```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

```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

```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

```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)](https://en.wikipedia.org/wiki/Wave_equation)

```latex
(\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)](https://de.wikipedia.org/wiki/Energieerhaltungssatz#Energieerhaltungssatz_in_der_Relativit%C3%A4tstheorie)

```latex
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)](https://en.wikipedia.org/wiki/Correspondence_principle)

```latex
\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:

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

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

#### 4.6.7 Use correspondence principle

```latex
-\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)}

```latex
-\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

```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

```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

```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:

```shell
user$ vi latex.tex
```

```latex
\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):

```shell
user$ pdflatex latex.tex
```

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

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

That's it. Enjoy!

## 7. Tools

* [Image-Template-Builder](https://latex.ixno.de/build.php)
* [Online-Formula-Editor](http://www.hostmath.com/)
* [TeX Mathmode](http://www.lsv.fr/~markey/LaTeX/doc/Mathmode.pdf)
* [List of LaTeX symbols](http://latex.wikia.com/wiki/List_of_LaTeX_symbols)

## A. Authors

* Björn Hempel - _Initial work_ - [https://github.com/bjoern-hempel](https://github.com/bjoern-hempel)

## B. Licence

This tutorial is licensed under the MIT License - see the [LICENSE.md](/LICENSE.md) file for details

## C. Closing words

Have fun! :)