xemlock / php-latex
LaTeX parser and renderer
Installs: 28 929
Dependents: 1
Suggesters: 0
Security: 0
Stars: 41
Watchers: 5
Forks: 6
Open Issues: 2
Requires
- php: >=5.3.0
- ext-dom: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: >=5.7 <10.0
This package is auto-updated.
Last update: 2024-11-14 22:33:45 UTC
README
The main purpose of this library is to provide a valid LaTeX output from, not always valid, user input. You can also render LaTeX code to HTML, with one limitation though - rendering to HTML is done only for the text mode, the math mode needs to be handled by a JavaScript library - in the browser. For this I recommend using MathJax.
Bear in mind that not every LaTeX command is recognized or implemented. If you happen to need a command that's not supported you can either define it manually (see description below), or file a feature request.
Installation
To use php-latex, you install it just as any other php package - with Composer.
composer require xemlock/php-latex:dev-master
Usage
Basic usage is as follows:
Parsing LaTeX source code
$parser = new PhpLatex_Parser(); $parsedTree = $parser->parse($input); // $parsedTree contains object representation of the LaTeX document
Render parsed LaTeX source
Once you have a parsed source code, you can render it to HTML (or to LaTeX) - please mind that math-mode code is rendered as-is.
// render parsed LaTeX code to HTML $htmlRenderer = new PhpLatex_Renderer_Html(); $html = $htmlRenderer->render($parsedTree); // render parsed LaTeX code to sanitized LaTeX code $latex = PhpLatex_Renderer_Abstract::toLatex($parsedTree);
Customization
You can add custom (or not yet implemented) commands to the parser:
$parser = new PhpLatex_Parser(); $parser->addCommand( '\placeholder', array( // number of arguments 'numArgs' => 1, // number of optional arguments, default 0 'numOptArgs' => 1, // mode this command is valid in, can be: 'both', 'math', 'text' 'mode' => 'both', // whether command arguments should be parsed, or handled as-is 'parseArgs' => false, // whether command allows a starred variant 'starred' => false, ) );
pdflatex
Additionally, this library provides a wrapper for pdflatex to make rendering and compiling .tex
files
from PHP scripts easier.
$pdflatex = new PhpLatex_PdfLatex(); // to generate a PDF from .tex file $pathToGeneratedPdf = $pdflatex->compile('/path/to/document.tex', array(/* optional paths to files included by .tex file (images) */]) );
You can access the build log of the last compile
call via:
echo $pdflatex->getLog();
You can even compile on the fly a LaTeX string:
$pathToGeneratedPdf = $pdflatex->compileString(' \documentclass{article} \begin{document} Hello from \LaTeX! \end{document} ');
By default, a system temp dir is used for generating PDF from string. You can however customize it:
$pdflatex->setBuildDir('/path/to/temp');
License
The MIT License (MIT). See the LICENSE file.