Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion library/PhpLatex/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion library/PhpLatex/Renderer/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
65 changes: 34 additions & 31 deletions library/PhpLatex/environs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
6 changes: 6 additions & 0 deletions tests/PhpLatex/Test/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}