Help getting diagrams-latex to work

I would like to use the diagrams package to generate some figures in a latex file. I have the package installed and working for the command line examples, but when I try to use it within a latex file I run into a problem with hidden libs/modules.

Here is a simple latex file based on the examples on the diagrams web pages.


\documentclass{article}
\usepackage[backend=pgf, extension=pgf, outputdir=diagrams, input]{diagrams-latex}
\usepackage{graphicx}

\begin{document}

\begin{diagram}[width=300,height=200]
  {-# LANGUAGE FlexibleContexts #-}
  import Data.List

  foo = circle 1 # fc green
  dia = foo ||| foo
\end{diagram}

\end{document}

And here are the messages I get when running pdflatex --enable-write18 newtest.tex


/tmp/Diagram29469-0.hs:4:1: error:
    Could not load module ‘Diagrams.Prelude’
    It is a member of the hidden package ‘diagrams-lib-1.4.6.2’.
    You can run ‘:set -package diagrams-lib’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    It is a member of the hidden package ‘diagrams-lib-1.4.6.2’.
    You can run ‘:set -package diagrams-lib’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    It is a member of the hidden package ‘diagrams-lib-1.4.6.2’.
    You can run ‘:set -package diagrams-lib’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    It is a member of the hidden package ‘diagrams-lib-1.4.6.2’.
    You can run ‘:set -package diagrams-lib’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
/tmp/Diagram29469-0.hs:5:1: error:
    Could not load module ‘Diagrams.Backend.PGF’
    It is a member of the hidden package ‘diagrams-pgf-1.4.2.1’.
    You can run ‘:set -package diagrams-pgf’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    It is a member of the hidden package ‘diagrams-pgf-1.4.2.1’.
    You can run ‘:set -package diagrams-pgf’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
1 Like

I got it to sort of work, though it is brittle, and I suspect there must be a better way. I created a module Mymod.hs that just basically is there to import the Diagram components I need, and then those packages are added to the build depends section of a mymod.cabal file which I build. I also then do a cabal install --lib diagrams diagrams-lib diagrams-pgf and then the programs can find everything to compile the latex source. Advice for a more sensible way to do this?

I think the best way to do it for now is to let cabal create an environment file for the dependencies in the same directory as your .tex file. You can specify where cabal should place the environment file like this:

cabal install --lib diagrams diagrams-lib diagrams-pgf --package-env .

Using a single . means it will create (or update) the environment file in the current directory. For me the file is named .ghc.environment.x86_64-linux-9.6.5 but it changes depending on your ghc version and platform. It is relatively human readable.

You don’t need to create a package for this.

I think the ideal solution would be for diagrams-builder to communicate with cabal to make sure the required packages are installed in an appropriate location so that you wouldn’t have to do anything special as the user.

3 Likes

Thanks for the help. Your environment suggestion works for me, generates a similarly named file, and is obviously cleaner. I don’t know that diagrams is in much more than maintenance mode. I was trying to embed them in a latex file, and the use case for that has to be pretty small. It is a wonderful package for programmatic generation of images. I made this demonstration of the Pythagorean theorem with the package.

2 Likes