From fc81b18ad0d266b848d11deb469d54c2ccdfe89f Mon Sep 17 00:00:00 2001 From: Lars Willighagen Date: Sat, 7 Dec 2024 23:39:47 +0100 Subject: [PATCH] Allow 'parseArgs' and 'numOptArgs' in environments Close #13 --- library/PhpLatex/Parser.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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); }