Skip to content

Commit

Permalink
Support \newenvironment and \renewenvironment.
Browse files Browse the repository at this point in the history
  • Loading branch information
jgm committed Mar 9, 2017
1 parent e504823 commit 6abbd08
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 16 deletions.
11 changes: 8 additions & 3 deletions src/Text/TeXMath/Readers/TeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,18 @@ endLine :: TP Char
endLine = try $ do
symbol "\\\\"
optional inbrackets -- can contain e.g. [1.0in] for a line height, not yet supported
optional $ ctrlseq "hline"
-- we don't represent the line, but it shouldn't crash parsing
return '\n'

arrayLine :: TP ArrayLine
arrayLine = notFollowedBy (ctrlseq "end" >> return '\n') >>
sepBy1 (unGrouped <$> manyExp (notFollowedBy endLine >> expr)) (symbol "&")
sepBy1 (unGrouped <$>
manyExp (try $ ignorable' *>
notFollowedBy endLine *>
expr <*
ignorable')) (symbol "&")
where ignorable' = ignorable >>
optional (try (ctrlseq "hline" >> ignorable'))
-- we don't represent the line, but it shouldn't crash parsing

arrayAlignments :: TP [Alignment]
arrayAlignments = try $ do
Expand Down
20 changes: 9 additions & 11 deletions src/Text/TeXMath/Readers/TeX/Macros.hs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ newcommand = try $ do
name <- inbraces <|> ctrlseq
guard (take 1 name == "\\")
let name' = drop 1 name
pSkipSpaceComments
numargs <- numArgs
pSkipSpaceComments
optarg <- if numargs > 0
Expand Down Expand Up @@ -151,22 +150,21 @@ newenvironment :: GenParser Char st Macro
newenvironment = try $ do
char '\\'
-- we ignore differences between these so far:
try (string "newenvironment")
<|> try (string "renewenvironment")
optional (string "re")
string "newenvironment"
optional (char '*')
pSkipSpaceComments
name <- inbraces <|> ctrlseq
pSkipSpaceComments
numargs <- numArgs
pSkipSpaceComments
optarg <- if numargs > 0
then optArg
then optArg <* pSkipSpaceComments
else return Nothing
let numargs' = case optarg of
Just _ -> numargs - 1
Nothing -> numargs
pSkipSpaceComments
opener <- inbraces <|> ctrlseq
pSkipSpaceComments
closer <- inbraces <|> ctrlseq
let defn = "\\newenvironment{" ++ name ++ "}" ++
(if numargs > 0 then ("[" ++ show numargs ++ "]") else "") ++
Expand All @@ -182,7 +180,6 @@ newenvironment = try $ do
opt <- case optarg of
Nothing -> return Nothing
Just _ -> liftM (`mplus` optarg) optArg
pSkipSpaceComments
args <- count numargs' (pSkipSpaceComments >>
(inbraces <|> ctrlseq <|> count 1 anyChar))
let args' = case opt of
Expand All @@ -196,7 +193,7 @@ newenvironment = try $ do
char '}'
body <- manyTill anyChar ender
return $ apply args'
$ opener ++ "%\n" ++ body ++ "%\n" ++ closer ++ "\n"
$ opener ++ body ++ closer

-- | Parser for \DeclareMathOperator(*) command.
declareMathOperator :: GenParser Char st Macro
Expand Down Expand Up @@ -242,7 +239,7 @@ comment = do
return ()

numArgs :: GenParser Char st Int
numArgs = option 0 $ do
numArgs = option 0 $ try $ do
pSkipSpaceComments
char '['
pSkipSpaceComments
Expand All @@ -268,8 +265,9 @@ inBrackets = try $ do
inbraces :: GenParser Char st String
inbraces = try $ do
char '{'
res <- manyTill (skipComment >> (inbraces' <|> count 1 anyChar <|> escaped "{}"))
(try $ skipComment >> char '}')
res <- manyTill (pSkipSpaceComments >>
(inbraces' <|> count 1 anyChar <|> escaped "{}"))
(try $ pSkipSpaceComments >> char '}')
return $ concat res

inbraces' :: GenParser Char st String
Expand Down
2 changes: 1 addition & 1 deletion tests/readers/tex/macros.native
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ENumber "2",ENumber "5",EGrouped [EIdentifier "y",ESymbol Bin "+",ENumber "3"],EGrouped [EIdentifier "x",ESymbol Bin "+",ENumber "3"],EGrouped [EIdentifier "x",ESymbol Bin "+",EIdentifier "\945"]]
[ENumber "2",ENumber "5",EGrouped [EIdentifier "y",ESymbol Bin "+",ENumber "3"],EGrouped [EIdentifier "x",ESymbol Bin "+",ENumber "3"],EGrouped [EIdentifier "x",ESymbol Bin "+",EIdentifier "\945"],EArray [AlignCenter,AlignCenter] [[[ENumber "2"],[ENumber "3"]],[[ENumber "4"],[ENumber "5"]]]]
11 changes: 11 additions & 0 deletions tests/src/macros.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@
\renewcommand{\phi}{\aaa}
\newcommand{\b}[1]{#1}

\newenvironment{ary}
{\begin{array}{cc}
\hline}
{\hline
\end{array}}

\phi
\abc
\xyz[y] {3}
\xyz 3
\xyz \alpha

\begin{ary}
2 & 3\\
4 & 5
\end{ary}
18 changes: 18 additions & 0 deletions tests/writers/macros.mml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,23 @@
<mo>+</mo>
<mi>α</mi>
</mrow>
<mtable>
<mtr>
<mtd columnalign="center">
<mn>2</mn>
</mtd>
<mtd columnalign="center">
<mn>3</mn>
</mtd>
</mtr>
<mtr>
<mtd columnalign="center">
<mn>4</mn>
</mtd>
<mtd columnalign="center">
<mn>5</mn>
</mtd>
</mtr>
</mtable>
</mrow>
</math>
44 changes: 44 additions & 0 deletions tests/writers/macros.omml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,49 @@
<m:r>
<m:t>α</m:t>
</m:r>
<m:m>
<m:mPr>
<m:baseJc m:val="center" />
<m:plcHide m:val="1" />
<m:mcs>
<m:mc>
<m:mcPr>
<m:mcJc m:val="center" />
<m:count m:val="1" />
</m:mcPr>
</m:mc>
<m:mc>
<m:mcPr>
<m:mcJc m:val="center" />
<m:count m:val="1" />
</m:mcPr>
</m:mc>
</m:mcs>
</m:mPr>
<m:mr>
<m:e>
<m:r>
<m:t>2</m:t>
</m:r>
</m:e>
<m:e>
<m:r>
<m:t>3</m:t>
</m:r>
</m:e>
</m:mr>
<m:mr>
<m:e>
<m:r>
<m:t>4</m:t>
</m:r>
</m:e>
<m:e>
<m:r>
<m:t>5</m:t>
</m:r>
</m:e>
</m:mr>
</m:m>
</m:oMath>
</m:oMathPara>
5 changes: 4 additions & 1 deletion tests/writers/macros.tex
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
25{y + 3}{x + 3}{x + \alpha}
25{y + 3}{x + 3}{x + \alpha}\begin{matrix}
2 & 3 \\
4 & 5 \\
\end{matrix}

0 comments on commit 6abbd08

Please sign in to comment.