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.

Comments (11)

  1. May 28, 2005
    Greg Pelcha said...

    I would not suggest doing it in this way. Its must better, and always gives the correct result if you simply export the diagram as a png with a resolution of 720×720 pixels/in. This gives correct color, shapes, and scaling as apposed to the suggestion you have provided.

    There are simply to many problems with most translators today to get the exact output desired. In the end, if you are transforming it into a pdf anyway, take the shortcut, and do what Visio is best at doing. Save the drawing as a normal visio drawing (so that you can always modify it later if needed), then export it as a high resolution png or jpg. Be sure to turn on the transparency feature so you don’t get any wierd results (I always have it to white). And verify that the size of the drawing is set to source.

    Let me know what you think.

    Greg Pelcha

  2. June 26, 2005
    Jonathan Sprinkle said...

    I disagree with Greg; producing PNG files is not much better than a JPG, if you want your figures to look good. After all, you want your figures to look good no matter how they are resized, and PNG fails this basic test, as far as I can tell.

    I go through this EPS production through the EMF2EPS (similar to the above mentioned) and though there is some small amount of headache, the output justifies it, I think. The process takes a little longer, but I think if you are taking the time to use LaTeX in the first place, you might as well make your figures look good. Else, you might as well just use Word, and keep your EMF files intact.

  3. September 28, 2006
    Kenneth Brun said...

    Visio can export directly to eps – typical M$ that they removed that feature in later versions :-(

  4. December 3, 2006
    BTreeHugger said...

    Why not export to SVG in your diagramming tool, and then use either Inkscape or ImageMagick (both Win32 and Linux versions ought to work) by trying the scripts at: http://wiki.lyx.org/Tips/UseInkscapeSVGImages

  5. February 2, 2008
    Mathias said...

    Hey… this saved my life… ok ok… its in danish, but never the less it might be a litle help…


    well…. combined it with a small bat file in print dir it works like a charm…
    @echo off
    echo .
    echo Based on http://www.hvemerhan.dk/eps.php (install printer WMF2EPS1.32.ZIP
    echo and set port to tile + advanced settings set postscriptsettings to EPS
    echo and TTfont to kontur). With miktex no need to gosth download
    echo .
    echo inputfilename: out.eps
    echo .
    set INPUT=
    set /P INPUT=Name of picture (no space): %=%
    if “%INPUT%”==”" goto input
    echo Your input was: %INPUT%

    ps2epsi out.eps %INPUT%.eps
    epstopdf %INPUT%.eps


    I had some problems with the size of my elements were too big and if I just try to rescale in visio all text remains and borders and everything f*** up if you first downscale and the upscale… so i solved this by using the zoom funcion in page propertyes. you have to test a litle bit on the zoom percent. If you have several drawing then remember to do the math based on the widt of THIS drawing… excel is a great helper…

  6. August 21, 2009
    Stoyan said...

    I found an extremely easy way to do it. I use cutepdf printer to print visio drawings. The size of the sheet is the size of the paper so that there are no white spaces around. Then I simply include picturename.pdf. It comes out really nice.

  7. September 3, 2009
    Jonathan Thomas said...

    OK guys this is 2009 – to simply get your diagram in:
    Open up your Lyx 1.6.
    Open your Visio diagram.
    In Visio, select the part of your diagram you want and copy (Ctrl+C).
    Go to Lyx, and position the caret where you want to insert.
    Paste. (Ctrl+V)

    Yes, it’s really that simple. Cut and paste between apps. Lyx will automatically create the eps for you – after you hit paster, it will pop a dialog box asking you where you want to save the resultant eps file. So simple and beautiful!

  8. October 6, 2009
    Lasse said...


    I can recommend Mathias’ solution – I have just tried it on my Vista installation – and it works perfect with Visio 2007 and LaTeX (miktex distri)!

    Please note that “hvemerhan.dk” links to an old WMF2EPS printer driver – so, if you are using vista, then download the new driver from this site ..

  9. November 12, 2009
    Manuel said...

    The easiest solution for me is to use Visio 2007. This program has the possibility of exporting directly to PDF. The only problem is the bounding box, but it’s quite easy to solve in Visio, because you can configure the page of your drawing to have the size of only the content of your design. If you use PdfLatex then this is, in my opinion, the best and easiest solution.

  10. November 22, 2009
    inaki said...

    Congratulations Jonathan!

    As said the classic and always useful Ctrl+C & Ctrl+V worked just perfect between the two applications. It’s nice to see this kind of extremely simple solutions amongst others such as converting to pdf, then to eps, then …

    Just one little thing, Lyx asks you where to find the resulting EMF file, not EPS

  11. September 11, 2010
    Gabi said...

    For general knowledge, the Ctrl+C & Ctrl+V methods suggested by Jonathan also works for Visio 2003 and SWP.