MathParser\Interpreting\LaTeXPrinter Class Reference

Create LaTeX code for prettyprinting a mathematical expression (for example via MathJax) More...

Inheritance diagram for MathParser\Interpreting\LaTeXPrinter:
MathParser\Interpreting\Visitors\Visitor

Public Member Functions

 __construct ()
 Constructor. More...
 
 visitExpressionNode (ExpressionNode $node)
 Generate LaTeX code for an ExpressionNode. More...
 
 visitNumberNode (NumberNode $node)
 Generate LaTeX code for a NumberNode. More...
 
 visitIntegerNode (IntegerNode $node)
 Interface function for visiting IntegerNodes. More...
 
 visitRationalNode (RationalNode $node)
 Interface function for visiting RationalNodes. More...
 
 visitVariableNode (VariableNode $node)
 Generate LaTeX code for a VariableNode. More...
 
 visitFunctionNode (FunctionNode $node)
 Generate LaTeX code for a FunctionNode. More...
 
 visitConstantNode (ConstantNode $node)
 Generate LaTeX code for a ConstantNode. More...
 
 parenthesize (Node $node, ExpressionNode $cutoff, $prepend='')
 Add parentheses to the LaTeX representation of $node if needed. More...
 
 bracesNeeded (Node $node)
 Add curly braces around the LaTex representation of $node if needed. More...
 

Private Attributes

 $lexer
 StdMathLexer $lexer.
 

Detailed Description

Create LaTeX code for prettyprinting a mathematical expression (for example via MathJax)

Implementation of a Visitor, transforming an AST into a string giving LaTeX code for the expression.

The class in general does not generate the best possible LaTeX code, and needs more work to be used in a production setting.

Example:

$parser = new StdMathParser();
$f = $parser->parse('exp(2x)+xy');
printer = new LaTeXPrinter();
result = $f->accept($printer); // Generates "e^{2x}+xy"

Note that surrounding $, $$ or \begin{equation}..\end{equation} has to be added manually.

Constructor & Destructor Documentation

MathParser\Interpreting\LaTeXPrinter::__construct ( )

Constructor.

Create a LaTeXPrinter.

Member Function Documentation

MathParser\Interpreting\LaTeXPrinter::bracesNeeded ( Node  $node)

Add curly braces around the LaTex representation of $node if needed.

Nodes representing a single ConstantNode, VariableNode or NumberNodes (0–9) are returned as-is. Other Nodes get curly braces around their LaTeX code.

Parameters
Node$nodeAST to parse
Return values
string
MathParser\Interpreting\LaTeXPrinter::parenthesize ( Node  $node,
ExpressionNode  $cutoff,
  $prepend = '' 
)

Add parentheses to the LaTeX representation of $node if needed.

Parameters
Node$nodeThe AST to typeset
ExpressionNode$cutoffA token representing the precedence of the parent node. Operands with a lower precedence have parentheses added.
bool$addSpaceFlag determining whether an additional space should be added at the beginning of the returned string.
Return values
string
MathParser\Interpreting\LaTeXPrinter::visitConstantNode ( ConstantNode  $node)

Generate LaTeX code for a ConstantNode.

Create a string giving LaTeX code for a ConstantNode. pi typesets as \pi and e simply as e.

Exceptions
UnknownConstantExceptionfor nodes representing other constants.
Parameters
ConstantNode$nodeAST to be typeset
Return values
string

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitExpressionNode ( ExpressionNode  $node)

Generate LaTeX code for an ExpressionNode.

Create a string giving LaTeX code for an ExpressionNode (x op y) where op is one of +, -, *, / or ^

Typesetting rules:

  • Adds parentheses around each operand, if needed. (I.e. if their precedence lower than that of the current Node.) For example, the AST (^ (+ 1 2) 3) generates (1+2)^3 but (+ (^ 1 2) 3) generates 1^2+3 as expected.
  • Multiplications are typeset implicitly (* x y) returns xy or using \cdot if the first factor is a FunctionNode or the (left operand) in the second factor is a NumberNode, so (* x 2) return x \cdot 2 and (* (sin x) x) return \sin x \cdot x (but (* x (sin x)) returns x\sin x)
  • Divisions are typeset using \frac
  • Exponentiation adds braces around the power when needed.
Parameters
ExpressionNode$nodeAST to be typeset
Return values
string

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitFunctionNode ( FunctionNode  $node)

Generate LaTeX code for a FunctionNode.

Create a string giving LaTeX code for a functionNode.

Typesetting rules:

  • sqrt(op) is typeset as \sqrt{op} -exp(op)is either typeset ase^{op}, ifopis a simple expression or as(op)` for more complicated operands.
Parameters
FunctionNode$nodeAST to be typeset
Return values
string

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitIntegerNode ( IntegerNode  $node)

Interface function for visiting IntegerNodes.

Parameters
IntegerNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitNumberNode ( NumberNode  $node)

Generate LaTeX code for a NumberNode.

Create a string giving LaTeX code for a NumberNode. Currently, there is no special formatting of numbers.

Parameters
NumberNode$nodeAST to be typeset
Return values
string

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitRationalNode ( RationalNode  $node)

Interface function for visiting RationalNodes.

Parameters
RationalNode$nodeNode to visit.

Implements MathParser\Interpreting\Visitors\Visitor.

MathParser\Interpreting\LaTeXPrinter::visitVariableNode ( VariableNode  $node)

Generate LaTeX code for a VariableNode.

Create a string giving LaTeX code for a VariableNode. Currently, there is no special formatting of variables.

Parameters
VariableNode$nodeAST to be typeset
Return values
string

Implements MathParser\Interpreting\Visitors\Visitor.


The documentation for this class was generated from the following file: