July 2, 2004

Putting Visio diagrams into LyX/LaTeX

Today I was again faced with a need to put a Visio diagram into a LaTeX document via LyX. I’ve had this problem before, and I’ve worked it out, but I think I’ve always forgotten to write down how. (If I’m wrong, please give me a link to the entry where I wrote it down, by all means.)

On a side note, I tried using Dia but I just couldn’t bring myself to use it again. I’ve gotten the update version from Dag or FC2 or whatever, compared to the relatively ancient version I used to be running in RH9, but I still can’t get used to it. I can’t seem to transform the rulers from metric into imperial units. I thought there was an option for this, but I can’t find it now. I’m also not sure if it has auto-routing connectors like Visio, but I think it does. Anyway, it generally didn’t seem as easy to use and the little diagram I quickly produced didn’t seem as pretty.

So back to Visio 2003 for me. Here’s how I got my Visio diagram into a LaTeX document through LyX. For starters, my document was only about 3″x3″ so it fit easily on the page. If you need a diagram that takes up the whole page, check on LyX’s site for tips, or maybe the LyX mailing list; I think I saw something about it. Moving on, first you probably want to clip the page size to encompass only your document. Go to File->Page Setup, then the Page Size tab, then “Size to fit drawing contents.” Hit OK and the page size should shrink to the exact size it needs to be. Now save this as a “Windows Metafile (.WMF)” file. WMF is apparently a vector graphics format, as is EPS. I arrived at the need for vector graphics when I wanted to have a graphic at 600 DPI so it would be suitable for printing, but then found that it wasn’t downsampled to whatever resolution LyX/LaTeX/dvips/whatever was using for printing or displaying in a PDF. Sigh.

Here’s the kind of weird part. There’s a libwmf for converting WMF to EPS (among other formats) but it didn’t work very well on my document. So I ended up using wmf2eps which is a Win32 application. Bizarre part: go into its help files and read about how you have to add a printer, and continue reading about how you have to go into the advanced options for the printer and change something or another to “Enhanced Postscript.” In Win2k, the exact setting for that last part is reached through the printer’s properties page, “Advanced” tab, “Printing Defaults” button at the bottom, “Advanced” button again at the bottom of that dialog, then Document Options->PostScript Options->PostScript Output Option from there, and select “Encapsulated PostScript (EPS).” If wmf2eps was running when you made this change, you may need to restart it (I did). Anyway, once you’ve got that set up and selected that “WMF2EPS” printer in wmf2eps, you can tell it to convert your WMF to EPS. This worked fine for me, and then I was able to include the EPS file in LyX. Don’t be super concerned if LyX makes it look like there’s some huge white border around the image: I saw this, but when I did a DVI preview of it (C-d in LyX) everything was right as rain.

wmf2eps (the Win32 application) might have some sort of registration limitation. I notice there’s a pause whenever I start it up. I can’t tell if it’s getting longer. I’m sure if I were to just read the documentation I could find out if there will be further penalties on me if I keep using it. Instead of going to all that trouble, next time I need to do this I’ll seriously consider looking at Adobe Illustrator, which I think I have lying around somewhere. I wouldn’t be surprised if it could open WMF, and I would be surprised if it couldn’t write out EPS.

Update: I tried Illustrator CS and it works like a charm for converting EMF generated by Visio 2003 into EPS. Based on these instructions for embedding an Adobe Illustrator file in LaTeX, I figured out I just needed to turn off thumbnails and set preview to none. (Don’t worry about the ‘version 6 compatibility’ thing there. Also, I had no real success exporting to EPS from File->Export->Adobe Illustrator EPS Legacy.)

Update 2: I have had some problems with going EPS->PDF in LyX. It uses epstopdf which, in FC2 at least, has some problems with Illustrator-generated EPS I guess. Really I suspect Illustrator PS has some problems that epstopdf doesn’t fix. The symptom was gs hanging around for like six minutes spinning the CPU with nothing in strace. Then I found a Debian teTeX bug report that talks about an apparently substitute/replacement program called a2ping. So I downloaded a2ping.pl from http://tug.ctan.org/tex-archive/graphics/a2ping/, stuffed it in my path, reconfigured LyX’s EPS->PDF translation to use it (slightly different command line: a2ping $$i pdf: $$o), and all was right. On a side note, I’ll also note that my old method of going through ps2pdfwr to get a good looking PDF may not be necessary nor working on my FC2 box. Now I just use LyX’s pdflatex output path and I get a nice looking PDF. Look at the curve at the bottom of your lowercase letter ‘T’ closely and make sure it doesn’t look screwed up, as happened with everything except pdflatex, I think.