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:
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
.
The project requires at least all subsequent libraries:
- latex
- imagemagick
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.
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
If you need the pictures directly within the command line:
TODO.. (add a cli example)
E = m \cdot c^2
\sum_{i = 0}^{n} i = \frac{n(n + 1)}{2}
\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)
V^*(s) = \substack{\textbf{max}\\ {\tiny a}}\sum_{s'}^{} T(s, a, s')[R(s, a, s') +
\gamma \cdot V^*(s')] \quad \forall s
\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}})}
See below
See below
(\partial_x^2-\frac{1}{c^2}\partial_t^2)\psi=0
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
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).
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}
Test: If we put this relationship into the relativistic energy equation, we get the classical wave equation.
The total amount of energy is the sum of the kinetic energy and the potential energy:
\frac{p^2}{2m} + V = E
-\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)}
Now we have the time-dependent Schrödinger equation.
\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}
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}}
{\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}}
If there is an error in the formula, you will get the following picture:
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
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!
- Björn Hempel bjoern@hempel.li - Initial work - https://github.com/bjoern-hempel
This tutorial is licensed under the MIT License - see the LICENSE.md file for details
Have fun! :)