2017 © Pedro Peláez
 

library php-latex

LaTeX parser and renderer

image

xemlock/php-latex

LaTeX parser and renderer

  • Thursday, June 23, 2016
  • by xemlock
  • Repository
  • 2 Watchers
  • 8 Stars
  • 133 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

php-latex

Build status License, (*1)

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., (*2)

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., (*3)

Installation

To use php-latex, you install it just as any other php package - with Composer., (*4)

composer require xemlock/php-latex:dev-master

Usage

Basic usage is as follows:, (*5)

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., (*6)

// 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:, (*7)

$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., (*8)

$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:, (*9)

echo $pdflatex->getLog();

You can even compile on the fly a LaTeX string:, (*10)

$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:, (*11)

$pdflatex->setBuildDir('/path/to/temp'); 

License

The MIT License (MIT). See the LICENSE file., (*12)

The Versions

23/06 2016

dev-master

9999999-dev

LaTeX parser and renderer

  Sources   Download

MIT

The Development Requires

by Avatar xemlock