Example #1
0
 protected function _renderStyled(PhpLatex_Node $node)
 {
     $typestyle = null;
     if ($node->getType() === PhpLatex_Parser::TYPE_COMMAND) {
         switch ($node->value) {
             case '\\textbf':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->bold = true;
                 break;
             case '\\textup':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->style = PhpLatex_Renderer_Typestyle::STYLE_NORMAL;
                 break;
             case '\\textit':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->style = PhpLatex_Renderer_Typestyle::STYLE_ITALIC;
                 break;
             case '\\textsl':
                 // slanted (oblique)
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->style = PhpLatex_Renderer_Typestyle::STYLE_SLANTED;
                 break;
             case '\\emph':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->emphasis = true;
                 break;
             case '\\textrm':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->family = PhpLatex_Renderer_Typestyle::FAMILY_SERIF;
                 break;
             case '\\texttt':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->family = PhpLatex_Renderer_Typestyle::FAMILY_MONO;
                 break;
             case '\\textsf':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->family = PhpLatex_Renderer_Typestyle::FAMILY_SANS;
                 break;
             case '\\underline':
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->underline = true;
                 break;
             case '\\textsc':
                 // small caps
                 $typestyle = $this->_pushTypestyle();
                 $typestyle->smallcaps = true;
                 break;
         }
     }
     $render = null;
     foreach ($node->getChildren() as $arg) {
         $render .= $this->_renderNode($arg, self::FLAG_ARG);
     }
     // wrap in style difference wrt to parent typestyle
     if ($typestyle) {
         if (strlen($render)) {
             $diff = $typestyle->diff();
             if ($diff) {
                 $render = $this->_wrapStyle($render, $diff);
             }
         }
         $this->_typestyle = $typestyle->pop();
     }
     return (string) $render;
 }
Example #2
0
 /**
  * @param  string $stopAtToken
  * @param  string $state
  * @return array
  */
 protected function _parseExprList(PhpLatex_Node $parent, $stopAtToken, $state, $environ = null)
 {
     $tree = array();
     while (false !== ($token = $this->_peek())) {
         if ($token['value'] === $stopAtToken) {
             // consume terminating token
             $this->_next();
             break;
         }
         $node = $this->_parseExpr($state, $environ);
         if ($node) {
             $parent->appendChild($node);
         }
     }
     return $tree;
 }
Example #3
0
 /**
  * Creates LaTeX representation of the given document node.
  *
  * This method is useful when parts of the rendered document should be
  * presented as the LaTeX source for processing (validating and rendering)
  * by external tools, i.e. MathJaX, mathTeX or mimeTeX.
  *
  * @param PhpLatex_Node|array $node
  * @return string
  */
 public static function toLatex($node)
 {
     if ($node instanceof PhpLatex_Node) {
         switch ($node->getType()) {
             case PhpLatex_Parser::TYPE_SPECIAL:
                 if ($node->value === '_' || $node->value === '^') {
                     return $node->value . self::toLatex($node->getChildren());
                 }
                 return $node->value;
             case PhpLatex_Parser::TYPE_TEXT:
                 // make sure text is properly escaped
                 $source = PhpLatex_Utils::escape($node->value);
                 return $source;
             case PhpLatex_Parser::TYPE_GROUP:
                 $source = $node->optional ? '[{' : '{';
                 $source .= self::toLatex($node->getChildren());
                 $source .= $node->optional ? '}]' : '}';
                 return $source;
             case PhpLatex_Parser::TYPE_VERBATIM:
                 return $node->value;
             case PhpLatex_Parser::TYPE_MATH:
                 $source = self::toLatex($node->getChildren());
                 if ($node->inline) {
                     return '\\(' . $source . '\\)';
                 } else {
                     return '\\[' . $source . '\\]';
                 }
             case PhpLatex_Parser::TYPE_COMMAND:
                 if ($node->value === '\\string') {
                     $value = $node->value;
                     foreach ($node->getChildren() as $child) {
                         $value .= self::toLatex($child);
                     }
                     return $value;
                 }
                 if ($node->symbol || $node->hasChildren()) {
                     return $node->value . self::toLatex($node->getChildren());
                 }
                 // control word, add space that was removed after
                 return $node->value . ' ';
             case PhpLatex_Parser::TYPE_ENVIRON:
                 return "\\begin{" . $node->value . "}\n" . self::toLatex($node->getChildren()) . "\\end{" . $node->value . "}\n";
             case PhpLatex_Parser::TYPE_DOCUMENT:
                 return self::toLatex($node->getChildren());
         }
     } elseif (is_array($node)) {
         // render node list and concatenate results
         $latex = '';
         foreach ($node as $child) {
             $latex .= self::toLatex($child);
         }
         return $latex;
     }
 }
Example #4
0
 public function appendTo(PhpLatex_Node $parent)
 {
     $parent->appendChild($this);
     return $this;
 }