/** * 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; } }
public static function getTextValue(DOMText $node) { $value = str_replace(array("\r\n", "\r"), "\n", $node->wholeText); $value = PhpLatex_Utils::escape($value); // replace UTF-8 characters with their counterparts if encoding is not UTF-8, // otherwise remove invalid UTF-8 characters if (in_array(self::$_outputEncoding, array('UTF-8', 'UTF8'), true)) { // regex taken from http://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string $regex = ' / ( (?: [\\x00-\\x7F] # single-byte sequences 0xxxxxxx | [\\xC0-\\xDF][\\x80-\\xBF] # double-byte sequences 110xxxxx 10xxxxxx | [\\xE0-\\xEF][\\x80-\\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2 | [\\xF0-\\xF7][\\x80-\\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3 ){1,100} # ...one or more times ) | . # anything else /x'; $value = preg_replace($regex, '$1', $value); } else { $value = PhpLatex_Utils::escapeUtf8($value); } return $value; }