texmath 0.1.1 → 0.2
raw patch · 15 files changed
+212/−166 lines, 15 files
Files
- Text/TeXMath/MathMLWriter.hs +22/−2
- Text/TeXMath/Parser.hs +59/−41
- tests/02.xhtml +22/−18
- tests/04.xhtml +9/−9
- tests/05.xhtml +6/−6
- tests/06.xhtml +16/−14
- tests/07.xhtml +6/−6
- tests/08.xhtml +14/−14
- tests/09.xhtml +6/−6
- tests/10.xhtml +27/−25
- tests/11.xhtml +2/−2
- tests/12.xhtml +2/−2
- tests/13.xhtml +10/−10
- tests/19.xhtml +10/−10
- texmath.cabal +1/−1
Text/TeXMath/MathMLWriter.hs view
@@ -32,7 +32,7 @@ toMathML :: DisplayType -> [Exp] -> Element toMathML dt exprs =- add_attr dtattr $ math $ map showExp exprs+ add_attr dtattr $ math $ map (showExp . expandMathOp dt) exprs where dtattr = Attr (unqual "display") dt' dt' = case dt of DisplayBlock -> "block"@@ -78,7 +78,7 @@ Just c' -> unode c' (showExp x) Nothing -> error $ "Unknown unary op: " ++ c -binaryOps :: Node a => M.Map String (a -> Element)+binaryOps :: M.Map String ([Element] -> Element) binaryOps = M.fromList [ ("\\frac", unode "mfrac") , ("\\tfrac", withAttribute "displaystyle" "false" .@@ -89,8 +89,15 @@ , ("\\stackrel", unode "mover") , ("\\overset", unode "mover") , ("\\underset", unode "munder")+ , ("\\binom", showBinom) ] +showBinom :: [Element] -> Element+showBinom lst = mrow [ (withAttribute "stretchy" "true" $ unode "mo" "(")+ , unode "mtable" $ map (unode "mtr" . unode "mtd") lst+ , (withAttribute "stretchy" "true" $ unode "mo" ")")+ ]+ showBinary :: String -> Exp -> Exp -> Element showBinary c x y = case M.lookup c binaryOps of@@ -131,6 +138,14 @@ accent = add_attr (Attr (unqual "accent") "true") . unode "mo" +expandMathOp :: DisplayType -> Exp -> Exp+expandMathOp DisplayInline (EMathOperator x) = EIdentifier x+expandMathOp DisplayInline x = x+expandMathOp DisplayBlock (ESub (EMathOperator x) y) = EUnder (EIdentifier x) y+expandMathOp DisplayBlock (ESubsup (EMathOperator x) y z) = EUnder (ESuper (EIdentifier x) z) y+expandMathOp DisplayBlock (EMathOperator x) = EIdentifier x+expandMathOp DisplayBlock x = x+ showExp :: Exp -> Element showExp e = case e of@@ -138,7 +153,12 @@ EGrouped [x] -> showExp x EGrouped xs -> mrow $ map showExp xs EIdentifier x -> unode "mi" x+ EMathOperator x -> unode "mi" x --in case expandMathOp is not used ESymbol Accent x -> accent x+ EStretchy (ESymbol Open x) -> makeStretchy $ unode "mo" x+ EStretchy (ESymbol Close x) -> makeStretchy $ unode "mo" x+ ESymbol Open x -> withAttribute "stretchy" "false" $ unode "mo" x+ ESymbol Close x -> withAttribute "stretchy" "false" $ unode "mo" x ESymbol _ x -> unode "mo" x ESpace x -> spaceWidth x EBinary c x y -> showBinary c x y
Text/TeXMath/Parser.hs view
@@ -41,6 +41,7 @@ ENumber String | EGrouped [Exp] | EIdentifier String+ | EMathOperator String | ESymbol TeXSymbolType String | ESpace String | EBinary String Exp Exp@@ -110,20 +111,37 @@ number = lexeme $ liftM ENumber $ many1 digit enclosure :: GenParser Char st Exp-enclosure = basicEnclosure <|> leftright <|> scaledEnclosure+enclosure = basicEnclosure <|> left <|> right <|> scaledEnclosure basicEnclosure :: GenParser Char st Exp basicEnclosure = choice $ map (\(s, v) -> try (symbol s) >> return v) enclosures -leftright :: GenParser Char st Exp-leftright = try $ do- typ <- (try (symbol "\\left") >> return Open)- <|> (symbol "\\right" >> return Close)- enc <- enclosure <|> (symbol "." >> return (ESymbol typ "\xFEFF"))+left :: GenParser Char st Exp+left = try $ do+ try (symbol "\\left")+ enc <- basicEnclosure <|> (symbol "." >> return (ESymbol Open "\xFEFF")) case enc of- ESymbol t x | t == typ -> return $ EStretchy $ ESymbol t x- _ -> pzero + (ESymbol Open _) -> tilRight enc <|> return (EStretchy enc)+ _ -> pzero +right :: GenParser Char st Exp+right = try $ do+ try (symbol "\\right")+ enc <- basicEnclosure <|> (symbol "." >> return (ESymbol Open "\xFEFF"))+ case enc of+ (ESymbol Close x) -> return (EStretchy $ ESymbol Open x)+ _ -> pzero++-- We want stuff between \left( and \right) to be in an mrow,+-- so that the scaling is based just on this unit, and not the+-- whole containing formula.+tilRight :: Exp -> GenParser Char st Exp+tilRight start = try $ do+ contents <- manyTill expr+ (try $ symbol "\\right" >> lookAhead basicEnclosure)+ end <- basicEnclosure+ return $ EGrouped $ EStretchy start : (contents ++ [EStretchy end])+ scaledEnclosure :: GenParser Char st Exp scaledEnclosure = try $ do cmd <- command@@ -340,7 +358,7 @@ brackets = lexeme . P.brackets lexer binaryOps :: [String]-binaryOps = ["\\frac", "\\tfrac", "\\dfrac", "\\stackrel", "\\overset", "\\underset"]+binaryOps = ["\\frac", "\\tfrac", "\\dfrac", "\\stackrel", "\\overset", "\\underset", "\\binom"] scalers :: M.Map String String scalers = M.fromList@@ -612,37 +630,37 @@ , ("~", ESpace "0.333em") , ("\\quad", ESpace "1em") , ("\\qquad", ESpace "2em")- , ("\\arccos", EIdentifier "arccos")- , ("\\arcsin", EIdentifier "arcsin")- , ("\\arctan", EIdentifier "arctan")- , ("\\arg", EIdentifier "arg")- , ("\\cos", EIdentifier "cos")- , ("\\cosh", EIdentifier "cosh")- , ("\\cot", EIdentifier "cot")- , ("\\coth", EIdentifier "coth")- , ("\\csc", EIdentifier "csc")- , ("\\deg", EIdentifier "deg")- , ("\\det", EIdentifier "det")- , ("\\dim", EIdentifier "dim")- , ("\\exp", EIdentifier "exp")- , ("\\gcd", EIdentifier "gcd")- , ("\\hom", EIdentifier "hom")- , ("\\inf", EIdentifier "inf")- , ("\\ker", EIdentifier "ker")- , ("\\lg", EIdentifier "lg")- , ("\\lim", EIdentifier "lim")- , ("\\liminf", EIdentifier "liminf")- , ("\\limsup", EIdentifier "limsup")- , ("\\ln", EIdentifier "ln")- , ("\\log", EIdentifier "log")- , ("\\max", EIdentifier "max")- , ("\\min", EIdentifier "min")- , ("\\Pr", EIdentifier "Pr")- , ("\\sec", EIdentifier "sec")- , ("\\sin", EIdentifier "sin")- , ("\\sinh", EIdentifier "sinh")- , ("\\sup", EIdentifier "sup")- , ("\\tan", EIdentifier "tan")- , ("\\tanh", EIdentifier "tanh")+ , ("\\arccos", EMathOperator "arccos")+ , ("\\arcsin", EMathOperator "arcsin")+ , ("\\arctan", EMathOperator "arctan")+ , ("\\arg", EMathOperator "arg")+ , ("\\cos", EMathOperator "cos")+ , ("\\cosh", EMathOperator "cosh")+ , ("\\cot", EMathOperator "cot")+ , ("\\coth", EMathOperator "coth")+ , ("\\csc", EMathOperator "csc")+ , ("\\deg", EMathOperator "deg")+ , ("\\det", EMathOperator "det")+ , ("\\dim", EMathOperator "dim")+ , ("\\exp", EMathOperator "exp")+ , ("\\gcd", EMathOperator "gcd")+ , ("\\hom", EMathOperator "hom")+ , ("\\inf", EMathOperator "inf")+ , ("\\ker", EMathOperator "ker")+ , ("\\lg", EMathOperator "lg")+ , ("\\lim", EMathOperator "lim")+ , ("\\liminf", EMathOperator "liminf")+ , ("\\limsup", EMathOperator "limsup")+ , ("\\ln", EMathOperator "ln")+ , ("\\log", EMathOperator "log")+ , ("\\max", EMathOperator "max")+ , ("\\min", EMathOperator "min")+ , ("\\Pr", EMathOperator "Pr")+ , ("\\sec", EMathOperator "sec")+ , ("\\sin", EMathOperator "sin")+ , ("\\sinh", EMathOperator "sinh")+ , ("\\sup", EMathOperator "sup")+ , ("\\tan", EMathOperator "tan")+ , ("\\tanh", EMathOperator "tanh") ]
tests/02.xhtml view
@@ -8,24 +8,28 @@ <mrow> <mn>2</mn> <mo>=</mo>- <mo stretchy="true">(</mo>- <mfrac>- <mrow>- <mo stretchy="true">(</mo>- <mn>3</mn>- <mo>-</mo>- <mi>x</mi>- <mo stretchy="true">)</mo>- <mo>×</mo>- <mn>2</mn>- </mrow>- <mrow>- <mn>3</mn>- <mo>-</mo>- <mi>x</mi>- </mrow>- </mfrac>- <mo stretchy="true">)</mo>+ <mrow>+ <mo stretchy="true">(</mo>+ <mfrac>+ <mrow>+ <mrow>+ <mo stretchy="true">(</mo>+ <mn>3</mn>+ <mo>-</mo>+ <mi>x</mi>+ <mo stretchy="true">)</mo>+ </mrow>+ <mo>×</mo>+ <mn>2</mn>+ </mrow>+ <mrow>+ <mn>3</mn>+ <mo>-</mo>+ <mi>x</mi>+ </mrow>+ </mfrac>+ <mo stretchy="true">)</mo>+ </mrow> </mrow> </math> </body>
tests/04.xhtml view
@@ -17,16 +17,16 @@ </msub> <mo>-</mo> <mfrac>- <mrow>- <mo stretchy="true">(</mo>- <mn>5</mn>- <mo>-</mo>- <mi>T</mi>- <msup>+ <msup>+ <mrow>+ <mo stretchy="true">(</mo>+ <mn>5</mn>+ <mo>-</mo>+ <mi>T</mi> <mo stretchy="true">)</mo>- <mn>2</mn>- </msup>- </mrow>+ </mrow>+ <mn>2</mn>+ </msup> <mn>2</mn> </mfrac> </mrow>
tests/05.xhtml view
@@ -20,9 +20,9 @@ <mi>s</mi> </msubsup> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>y</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mspace width="0.167em" /> <mi>d</mi> <mi>y</mi>@@ -36,14 +36,14 @@ <mi>x</mi> </msubsup> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>y</mi>- <mo>)</mo>- <mo>(</mo>+ <mo stretchy="false">)</mo>+ <mo stretchy="false">(</mo> <mi>x</mi> <mo>-</mo> <mi>y</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mspace width="0.167em" /> <mi>d</mi> <mi>y</mi>
tests/06.xhtml view
@@ -38,21 +38,23 @@ <mn>3</mn> <mi>m</mi> </msup>- <mo stretchy="true">(</mo>- <mi>m</mi>- <mspace width="0.167em" />- <msup>- <mn>3</mn>- <mi>n</mi>- </msup>- <mo>+</mo>- <mi>n</mi>- <mspace width="0.167em" />- <msup>- <mn>3</mn>+ <mrow>+ <mo stretchy="true">(</mo> <mi>m</mi>- </msup>- <mo stretchy="true">)</mo>+ <mspace width="0.167em" />+ <msup>+ <mn>3</mn>+ <mi>n</mi>+ </msup>+ <mo>+</mo>+ <mi>n</mi>+ <mspace width="0.167em" />+ <msup>+ <mn>3</mn>+ <mi>m</mi>+ </msup>+ <mo stretchy="true">)</mo>+ </mrow> </mrow> </mfrac> </mrow>
tests/07.xhtml view
@@ -11,22 +11,22 @@ <mo>ʹ</mo> <mo>+</mo> <mi>p</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>x</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>u</mi> <mo>ʹ</mo> <mo>+</mo> <mi>q</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>x</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>u</mi> <mo>=</mo> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>x</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>,</mo> <mspace width="1em" /> <mi>x</mi>
tests/08.xhtml view
@@ -6,49 +6,49 @@ <body> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> <mrow>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mover> <mi>z</mi> <mo accent="true">‾</mo> </mover>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mo>=</mo>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mi>z</mi>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mo>,</mo>- <mo>∣</mo>- <mo>(</mo>+ <mo stretchy="false">∣</mo>+ <mo stretchy="false">(</mo> <mover> <mi>z</mi> <mo accent="true">‾</mo> </mover> <msup>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>n</mi> </msup>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mo>=</mo>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mi>z</mi> <msup>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mi>n</mi> </msup> <mo>,</mo> <mi>arg</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msup> <mi>z</mi> <mi>n</mi> </msup>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <mi>n</mi> <mi>arg</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>z</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> </mrow> </math> </body>
tests/09.xhtml view
@@ -6,7 +6,7 @@ <body> <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> <mrow>- <msub>+ <munder> <mi>lim</mi> <mrow> <mi>z</mi>@@ -16,19 +16,19 @@ <mn>0</mn> </msub> </mrow>- </msub>+ </munder> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>z</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>z</mi> <mn>0</mn> </msub>- <mo>)</mo>+ <mo stretchy="false">)</mo> </mrow> </math> </body>
tests/10.xhtml view
@@ -10,9 +10,9 @@ <mo>φ</mo> <mi>n</mi> </msub>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mo>κ</mo>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <mfrac> <mn>1</mn>@@ -36,10 +36,10 @@ <mfrac> <mrow> <mi>sin</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mo>κ</mo> <mi>R</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> </mrow> <mrow> <mo>κ</mo>@@ -53,28 +53,30 @@ <mi>R</mi> </mrow> </mfrac>- <mo stretchy="true">[</mo>- <msup>- <mi>R</mi>- <mn>2</mn>- </msup>- <mfrac>- <mrow>- <mo>∂</mo>- <msub>- <mi>D</mi>- <mi>n</mi>- </msub>- <mo>(</mo>- <mi>R</mi>- <mo>)</mo>- </mrow>- <mrow>- <mo>∂</mo>+ <mrow>+ <mo stretchy="true">[</mo>+ <msup> <mi>R</mi>- </mrow>- </mfrac>- <mo stretchy="true">]</mo>+ <mn>2</mn>+ </msup>+ <mfrac>+ <mrow>+ <mo>∂</mo>+ <msub>+ <mi>D</mi>+ <mi>n</mi>+ </msub>+ <mo stretchy="false">(</mo>+ <mi>R</mi>+ <mo stretchy="false">)</mo>+ </mrow>+ <mrow>+ <mo>∂</mo>+ <mi>R</mi>+ </mrow>+ </mfrac>+ <mo stretchy="true">]</mo>+ </mrow> <mspace width="0.167em" /> <mi>d</mi> <mi>R</mi>
tests/11.xhtml view
@@ -10,9 +10,9 @@ <mo>φ</mo> <mi>n</mi> </msub>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mo>κ</mo>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <mn>0</mn> <mo>.</mo>
tests/12.xhtml view
@@ -7,9 +7,9 @@ <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> <mrow> <mi>f</mi>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mi>x</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <mrow> <mo stretchy="true">{</mo>
tests/13.xhtml view
@@ -14,7 +14,7 @@ <mi>F</mi> <mi>q</mi> </msub>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>a</mi> <mn>1</mn>@@ -40,7 +40,7 @@ </msub> <mo>;</mo> <mi>z</mi>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mo>=</mo> <msubsup> <mo>∑</mo>@@ -53,44 +53,44 @@ </msubsup> <mfrac> <mrow>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>a</mi> <mn>1</mn> </msub> <msub>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>n</mi> </msub> <mo>⋯</mo>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>a</mi> <mi>p</mi> </msub> <msub>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>n</mi> </msub> </mrow> <mrow>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>c</mi> <mn>1</mn> </msub> <msub>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>n</mi> </msub> <mo>⋯</mo>- <mo>(</mo>+ <mo stretchy="false">(</mo> <msub> <mi>c</mi> <mi>q</mi> </msub> <msub>- <mo>)</mo>+ <mo stretchy="false">)</mo> <mi>n</mi> </msub> </mrow>
tests/19.xhtml view
@@ -7,7 +7,7 @@ <math display="block" xmlns="http://www.w3.org/1998/Math/MathML"> <mrow> <mrow>- <mo>[</mo>+ <mo stretchy="false">[</mo> <mtable> <mtr> <mtd>@@ -99,10 +99,10 @@ </mtd> </mtr> </mtable>- <mo>]</mo>+ <mo stretchy="false">]</mo> </mrow> <mrow>- <mo>(</mo>+ <mo stretchy="false">(</mo> <mtable> <mtr> <mtd>@@ -121,10 +121,10 @@ </mtd> </mtr> </mtable>- <mo>)</mo>+ <mo stretchy="false">)</mo> </mrow> <mrow>- <mo>{</mo>+ <mo stretchy="false">{</mo> <mtable> <mtr> <mtd>@@ -143,10 +143,10 @@ </mtd> </mtr> </mtable>- <mo>}</mo>+ <mo stretchy="false">}</mo> </mrow> <mrow>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> <mtable> <mtr> <mtd>@@ -165,10 +165,10 @@ </mtd> </mtr> </mtable>- <mo>∣</mo>+ <mo stretchy="false">∣</mo> </mrow> <mrow>- <mo>∥</mo>+ <mo stretchy="false">∥</mo> <mtable> <mtr> <mtd>@@ -187,7 +187,7 @@ </mtd> </mtr> </mtable>- <mo>∥</mo>+ <mo stretchy="false">∥</mo> </mrow> </mrow> </math>
texmath.cabal view
@@ -1,5 +1,5 @@ Name: texmath-Version: 0.1.1+Version: 0.2 Cabal-version: >= 1.2 Build-type: Simple Synopsis: Conversion of LaTeX math formulas to MathML.