diff --git a/library/PhpLatex/Parser.php b/library/PhpLatex/Parser.php index db033f2..e487c27 100644 --- a/library/PhpLatex/Parser.php +++ b/library/PhpLatex/Parser.php @@ -318,6 +318,7 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{ assert(($mode & ($mode - 1)) === 0); // mode must be a power of 2 $math = false; + $optArgs = array(); $args = array(); if (isset($this->_environs[$name])) { @@ -342,12 +343,23 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{ // or displaymath), if so, prepare math node instead of environ node $math = isset($spec['math']) && $spec['math']; + $parseArgs = isset($spec['parseArgs']) ? $spec['parseArgs'] : true; + + $numOptArgs = isset($spec['numOptArgs']) ? intval($spec['numOptArgs']) : 0; + while (count($optArgs) < $numOptArgs) { + if (false !== ($arg = $this->_parseOptArg($mode, $environ, $parseArgs))) { + $optArgs[] = $arg; + } else { + break; + } + } + // parse args, will be placed as environs first children, with // no spaces between them, btw: \begin{tabular}c is a perfectly // correct specification for a single-column table. $nargs = isset($spec['numArgs']) ? intval($spec['numArgs']) : 0; while (count($args) < $nargs) { - if (false === ($arg = $this->_parseArg($mode, $environ))) { + if (false === ($arg = $this->_parseArg($mode, $environ, $parseArgs))) { $arg = $this->_createNode(self::TYPE_GROUP, $mode); } $arg->setProp('arg', true); @@ -364,6 +376,10 @@ protected function _createEnviron($name, $mode, $environ = null) // {{{ $node->math = $math; } + foreach ($optArgs as $arg) { + $node->appendChild($arg); + } + foreach ($args as $arg) { $node->appendChild($arg); } diff --git a/library/PhpLatex/Renderer/Abstract.php b/library/PhpLatex/Renderer/Abstract.php index dc5aac9..60402fb 100644 --- a/library/PhpLatex/Renderer/Abstract.php +++ b/library/PhpLatex/Renderer/Abstract.php @@ -71,7 +71,7 @@ public static function toLatex($node) // {{{ $argsEnd = 0; foreach ($children as $child) { - if ($child->arg) { + if ($child->arg || $child->optional) { ++$argsEnd; } else { break; diff --git a/library/PhpLatex/environs.php b/library/PhpLatex/environs.php index 3e75b95..ab0c035 100644 --- a/library/PhpLatex/environs.php +++ b/library/PhpLatex/environs.php @@ -2,62 +2,65 @@ return array( 'verbatim' => array( - 'verbatim' => true, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), - 'starred' => true, + 'verbatim' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), + 'starred' => true, // verbatim in tabular causes // ! LaTeX Error: Something's wrong--perhaps a missing \item. ), 'Verbatim' => array( - 'verbatim' => true, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), + 'verbatim' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), ), 'lstlisting' => array( - 'verbatim' => true, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), + 'numOptArgs' => 1, + 'verbatim' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), ), 'enumerate' => array( - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), ), 'itemize' => array( - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), + 'numOptArgs' => 1, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), // itemize in tabular causes // ! LaTeX Error: Something's wrong--perhaps a missing \item. ), 'displaymath' => array( - 'math' => true, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate'), + 'math' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate'), // displaymath in tabular causes // ! LaTeX Error: Bad math environment delimiter. ), 'math' => array( - 'math' => true, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate', 'tabular'), + 'math' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate', 'tabular'), ), 'equation' => array( - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'math' => true, - 'starred' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'math' => true, + 'starred' => true, ), 'eqnarray' => array( - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'math' => true, - 'starred' => true, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'math' => true, + 'starred' => true, ), 'tabular' => array( - 'numArgs' => 1, - 'mode' => PhpLatex_Parser::MODE_TEXT, - 'environs' => array('itemize', 'enumerate', 'tabular'), + 'numArgs' => 1, + 'numOptArgs' => 1, + 'mode' => PhpLatex_Parser::MODE_TEXT, + 'environs' => array('itemize', 'enumerate', 'tabular'), ), 'array' => array( - 'numArgs' => 1, - 'mode' => PhpLatex_Parser::MODE_MATH, + 'numArgs' => 1, + 'mode' => PhpLatex_Parser::MODE_MATH, ), ); diff --git a/tests/PhpLatex/Test/ParserTest.php b/tests/PhpLatex/Test/ParserTest.php index 08fc6bf..258ba3c 100644 --- a/tests/PhpLatex/Test/ParserTest.php +++ b/tests/PhpLatex/Test/ParserTest.php @@ -128,4 +128,10 @@ public function testNewlinesAfterComment() $tree = $this->parser->parse("A% comment\n\nB"); $this->assertSame("A\par B", PhpLatex_Renderer_Abstract::toLatex($tree)); } + + public function testEnvironmentWithArgsConfig() + { + $tree = $this->parser->parse("\\begin{tabular}[t]{|c|c|c|}\ncell1 & cell2 & cell3\n\\end{tabular}"); + $this->assertSame("\\begin{tabular}[{t}]{|c|c|c|}\n cell1 & cell2 & cell3\n\\end{tabular}", PhpLatex_Renderer_Abstract::toLatex($tree)); + } }