Parse error with Haskell diagrams-builder

160 Views Asked by At

I'm trying to use the diagrams-builder package with LaTeX in order to generate diagrams in line. Following the tutorial here, I have a file called test.tex with the following contents:

\documentclass[10pt]{article}

\usepackage[backend=ps, extension=eps, outputdir=diagrams]{diagrams-latex}
\usepackage{graphicx}

\begin{document}
\begin{diagram}[width=300,height=200]
{-# LANGUAGE FlexibleContexts #-}
dia = circle 1 
\end{diagram}

\end{document}

I created an empty directory called diagrams and tried to compile, only to be told that there is a parse error in what looks to be a file automatically generated by diagrams-builder-ps.

$ pdflatex --enable-write18 --shell-escape test.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016) (preloaded format=pdflatex)
 \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2016/03/31>
Babel <3.9r> and hyphenation patterns for 83 language(s) loaded.
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/size10.clo))
(/Users/nhalford/Library/texmf/tex/diagrams-latex.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/moreverb/moreverb.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/tools/verbatim.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/xkeyval/xkeyval.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/xkeyval.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/xkvutils.tex
(/usr/local/texlive/2016/texmf-dist/tex/generic/xkeyval/keyval.tex))))
(/usr/local/texlive/2016/texmf-dist/tex/latex/base/ifthen.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphicx.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/graphics.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/trig.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics-cfg/graphics.cfg)
(/usr/local/texlive/2016/texmf-dist/tex/latex/pdftex-def/pdftex.def
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ltxcmds.sty))))
No file test.aux.
(/usr/local/texlive/2016/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/pdftexcmds.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/ifpdf.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/epstopdf-base.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/grfext.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/kvdefinekeys.sty))
(/usr/local/texlive/2016/texmf-dist/tex/latex/oberdiek/kvoptions.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/kvsetkeys.sty
(/usr/local/texlive/2016/texmf-dist/tex/generic/oberdiek/etexcmds.sty)))
(/usr/local/texlive/2016/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg))
Opening diagrams-latex stream diagrams/test-diagrams-latex-fig1.hs Error while compiling diagrams/test-diagrams-latex-fig1.hs
/var/folders/2g/_6mplpfn4fl338wr3cj949440000gn/T/Diagram58258-0.hs:3:20: error:
    parse error on input ‘-’



Package diagrams-latex Warning: Conversion of diagrams/test-diagrams-latex-fig1
.hs failed.


Package diagrams-latex Warning: Please convert diagrams/test-diagrams-latex-fig
1.hs manually.

(./test.aux) )
No pages of output.
Transcript written on test.log.

I also get this error when attempting to run diagrams-builder-ps or diagrams-builder-cairo on the .hs file directly:

$ cd diagrams
$ ls
test-diagrams-latex-fig1.hs
$ diagrams-builder-ps -w 300 -h 200 test-diagrams-latex-fig1.hs
Error while compiling test-diagrams-latex-fig1.hs
/var/folders/2g/_6mplpfn4fl338wr3cj949440000gn/T/Diagram58289-0.hs:3:20: error:
    parse error on input ‘-’

$ diagrams-builder-cairo -w 300 -h 200 test-diagrams-latex-fig1.hs
Error while compiling test-diagrams-latex-fig1.hs
/var/folders/2g/_6mplpfn4fl338wr3cj949440000gn/T/Diagram58294-0.hs:3:20: error:
    parse error on input ‘-’

$ cat test-diagrams-latex-fig1.hs
{-# LANGUAGE FlexibleContexts #-}
dia = circle 1
$

Unfortunately the system deletes the temporary .hs file with the parse error immediately, so I'm unable to examine that file to see what exactly is causing this problem.

Thanks for your help!

1

There are 1 best solutions below

3
On BEST ANSWER

Oh, I think I just figured out what must be wrong, although I'm not sure of the best way to fix it. The key is this line:

/var/folders/2g/_6mplpfn4fl338wr3cj949440000gn/T/Diagram58289-0.hs:3:20: error:

Notice that the name of the temporary file is Diagram58289-0.hs. diagrams-builder calls openTempFile (see here) and then uses the base name of the resulting file as the name of the module. So the contents of Diagram58289-0.hs must look something like this:

{-# LANGUAGE FlexibleContexts #-}

module Diagram58289-0 where

dia = circle 1

The problem is that a hyphen is not valid in a module name, hence the parse error (sure enough, it is in column 20 on line 3).

Although the documentation for openTempFile says the created file will have a name of the form "fooXXX.ext where XXX is some random number", obviously that is not what is happening here. And indeed, it looks like the behavior of openTempFile changed recently; now it will always include a hyphen. I'm guessing this behavior changed in base-4.10, so it will show up for anyone using GHC 8.2.

I have created an issue on the diagrams-builder issue tracker to keep track of this problem. For now, one solution would be to downgrade to GHC 8.0, but that may not be a very attractive option.