3

I'm trying to implement the Haskell solution by John MacFarlane here, which should allow me to convert HTML files with MathJax (latex) input to .tex while preserving the math. The script is:

import Text.Pandoc

main = toJsonFilter fixmath

fixmath :: Block -> Block
fixmath = bottomUp fixmathBlock . bottomUp fixmathInline

fixmathInline :: Inline -> Inline
fixmathInline (RawInline "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
  RawInline "tex" $ take (length xs - 3) xs
fixmathInline x = x

fixmathBlock :: Block -> Block
fixmathBlock (RawBlock "html" ('<':'!':'-':'-':'M':'A':'T':'H':xs)) =
  RawBlock "tex" $ take (length xs - 3) xs
fixmathBlock x = x

I installed the 64-bit OSX version of Haskell, and also gave the command cabal install pandoc to get the pandoc functions. However, upon executing

ghc --make fixmath.hs

I get the following error:

[1 of 1] Compiling Main             ( fixmath.hs, fixmath.o )

fixmath.hs:9:26:
    Couldn't match expected type `Format' with actual type `[Char]'
    In the pattern: "html"
    In the pattern:
      RawInline "html"
                ('<' : '!' : '-' : '-' : 'M' : 'A' : 'T' : 'H' : xs)
    In an equation for `fixmathInline':
        fixmathInline
          (RawInline "html"
                     ('<' : '!' : '-' : '-' : 'M' : 'A' : 'T' : 'H' : xs))
          = RawInline "tex" $ take (length xs - 3) xs

fixmath.hs:10:13:
    Couldn't match expected type `Format' with actual type `[Char]'
    In the first argument of `RawInline', namely `"tex"'
    In the expression: RawInline "tex"
    In the expression: RawInline "tex" $ take (length xs - 3) xs

fixmath.hs:14:24:
    Couldn't match expected type `Format' with actual type `[Char]'
    In the pattern: "html"
    In the pattern:
      RawBlock "html"
               ('<' : '!' : '-' : '-' : 'M' : 'A' : 'T' : 'H' : xs)
    In an equation for `fixmathBlock':
        fixmathBlock
          (RawBlock "html"
                    ('<' : '!' : '-' : '-' : 'M' : 'A' : 'T' : 'H' : xs))
          = RawBlock "tex" $ take (length xs - 3) xs

fixmath.hs:15:12:
    Couldn't match expected type `Format' with actual type `[Char]'
    In the first argument of `RawBlock', namely `"tex"'
    In the expression: RawBlock "tex"
    In the expression: RawBlock "tex" $ take (length xs - 3) xs

What's gone wrong and what can I do?

Community
  • 1
  • 1
TSGM
  • 263
  • 1
  • 9

1 Answers1

5

Recent versions of pandoc changed the type of the "format" for RawBlock and RawInline. If you replace "html" and "tex" with (Format "html") and (Format "tex"), you should be okay. (Assuming there aren't other problems besides the one the compiler flagged.)

John MacFarlane
  • 8,511
  • 39
  • 33
  • Thanks for this. It now only returns one (different) error. I get the following (sorry for formatting): _fixmath.hs:3:8: No instance for (ToJsonFilter (Block -> Block)) arising from a use of toJsonFilter Possible fix:add an instance declaration for (ToJsonFilter (Block -> Block)) In the expression: toJsonFilter fixmath In an equation for main: main = toJsonFilter fixmath_ – TSGM Oct 20 '13 at 12:11
  • This is due to another change in pandoc since I posted that script. Try using `toJSONFilter` instead of `toJsonFilter`. – John MacFarlane Oct 20 '13 at 16:32
  • That seemed to do the trick, with the small addition of `import Text.Pandoc.JSON` at the top of the code. I'm not sure why I had to declare the second import command, but the single `import Text.Pandoc` was still giving me errors. – TSGM Oct 20 '13 at 17:49