Create LaTeX code for prettyprinting a mathematical expression (for example via MathJax) More...
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. | |
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.
Note that surrounding $
, $$
or \begin{equation}..\end{equation}
has to be added manually.
MathParser\Interpreting\LaTeXPrinter::__construct | ( | ) |
Constructor.
Create a LaTeXPrinter.
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.
Node | $node | AST to parse |
string |
MathParser\Interpreting\LaTeXPrinter::parenthesize | ( | Node | $node, |
ExpressionNode | $cutoff, | ||
$prepend = '' |
|||
) |
Add parentheses to the LaTeX representation of $node if needed.
Node | $node | The AST to typeset |
ExpressionNode | $cutoff | A token representing the precedence of the parent node. Operands with a lower precedence have parentheses added. |
bool | $addSpace | Flag determining whether an additional space should be added at the beginning of the returned string. |
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
.
UnknownConstantException | for nodes representing other constants. |
ConstantNode | $node | AST to be typeset |
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 ^
(^ (+ 1 2) 3)
generates (1+2)^3
but (+ (^ 1 2) 3)
generates 1^2+3
as expected.(* 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
)\frac
ExpressionNode | $node | AST to be typeset |
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.
sqrt(op)
is typeset as \sqrt{op} -
exp(op)is either typeset as
e^{op}, if
opis a simple expression or as
(op)` for more complicated operands.FunctionNode | $node | AST to be typeset |
string |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\LaTeXPrinter::visitIntegerNode | ( | IntegerNode | $node | ) |
Interface function for visiting IntegerNodes.
IntegerNode | $node | Node 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.
NumberNode | $node | AST to be typeset |
string |
Implements MathParser\Interpreting\Visitors\Visitor.
MathParser\Interpreting\LaTeXPrinter::visitRationalNode | ( | RationalNode | $node | ) |
Interface function for visiting RationalNodes.
RationalNode | $node | Node 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.
VariableNode | $node | AST to be typeset |
string |
Implements MathParser\Interpreting\Visitors\Visitor.