### Supporters and Partners

Sponsored by DANTE e.V.: The German speaking TeX Users Group

 Combining LaTeX and Illustrator
(18 votes, average: 3.83 out of 5)
LaTeX's Friends - Others
Written by Hartmut Lemmel
Tuesday, 30 November 2010 16:02

Adobe Illustrator is a powerful vector graphics program with EPS as native graphics format and is therefore ideally suited to create figures for LaTeX. This article describes how to include LaTeX output (e.g. mathematical formulas) into figures or posters created by Illustrator. In addition I present a way to use the combination Illustrator+LaTeX for creating slide presentations.

The work has been tested with Windows XP, MikTeX 2.8 and Illustrator CS.

# Installing LaTeX fonts on the system

The LaTeX fonts have to be installed as system wide fonts, otherwise Illustrator (as other programs) cannot deal with LaTeX output. The Windows compatible LaTeX font files have the extension *.pfm. Localize these files on your LaTeX installation, e.g.

C:\Programs\MiKTeX 2.8\fonts\type1\public\amsfonts\cm

and copy it to the Windows font directory, e.g.

C:\WINDOWS\Fonts

Instead of just copying you can also download and install the free program Adobe Type Manager Light (ATM). In ATM specify the font source directories, select the fonts and click Add.

Now the LaTeX fonts appear in the Windows font list and can be used by any Windows program.

# Exporting LaTeX to Illustrator

As Illustrator is able to import individual pages form PDF files, you can compile any LaTeX code to PDF (by DviPS or PDFLaTeX) and import it in Illustrator. This way you can include LaTeX formulas in your figures or posters in true vector font quality.

### Known bugs

For some few characters in math mode (e.g. slash '/', point '.', comma ',') there is a confusion between Illustrator and LaTeX about the character codes. The LaTeX expression $1/2$ appears in Illustrator with a small triangle instead of the slash. In this case you have to edit the text expression in Illustrator and select the correct symbol from the Glyph list (Window -> Type -> Glyphs).

# Simplification by Illustrator scripts

Illustrator is programmable by JavaScript. To install a script just copy it to

After the next start of Illustrator the script appears in the menu File -> Scripts. If you edit an existing script you don't need to restart Illustrator. Documentations of Illustrator JavaScript can be found here.

## Illustrator script: LaTeX

The script LaTeX.js prompts the user to enter a single line of LaTeX code, e.g.

$f(x)=\frac{1}{\sqrt{1+x^1}}$

After clicking OK the code is automatically compiled, converted to PDF and copied into the currently opened Illustrator document in the middle of the current view.

The script requires pdflatex.exe to be installed. You have to make sure that the location of pdflatex.exe is included in the system path. Alternatively you can edit the first line of the script and specify pdflatex.exe with full path.

## Illustrator script: LayersToPDF

For me it was always a pain to include LaTeX math into PowerPoint or similar programs. On the other hand, using plain LaTeX to create slide presentations it was a pain as well to correctly position graphics and figures. What you need is a WYSIWYG environment which is capable of handling LaTeX expressions.

The Illustrator script LayersToPDF.js presented here is an attempt in this direction. It converts the layers of a single Illustrator file into pages of a PDF file, i.e. slides of a presentation. The bottom layer becomes the first slide, the top layer the last.

You can exclude certain layers or you can combine several layers to a single slide by the following layer naming conventions:

• layer names starting with a minus sign do not create a slide.
• to include other layers just add their names with a plus sign.

Example: The Illustrator document contains 7 layers with the following names:

slide5+bkg-trashslide4+slide3+bkgslide3+slide2+bkgslide2+bkgslide1+bkg-bkg

The script creates a 5 page PDF with the following pages:

page 1: bkg + slide1page 2: bkg + slide2page 3: bkg + slide2 + slide3page 4: bkg + slide3 + slide4page 5: bkg + slide5

The first alphanumerical part of each layer name (e.g. "bkg", "slide1", "slide2"...) is used in the script to identify the layer. The additional names (e.g. "+slide3+bkg") refer to other layers and can be given in any order. In this example the background for all slides is defined on the bottom layer called "-bkg". As it starts with the minus sign it doesn't create an own slide. It is just included into other slides. The layer "-trash" is not used at all. Page 2 of the PDF file displays layer "slide2" and the background. On page 3 layer "slide3" is added. On page 4 layer "slide4" is added and layer "slide2" is removed. If you want to keep "slide2" you have to repeat it in the layer name, i.e. "slide4+slide3+slide2+bkg".

If you create a presentation you should set the page format of your document to a 4:3 ratio, e.g. width 200mm and height 150mm.

### Some technical issues

Illustrator is not able to create a multi page PDF by itself. Therefore the script first creates a number of single page PDFs which are afterwards combined to the final multi-page PDF by an external tool. I use the command line tool pdfpg.exe from verypdf.com. The program can be downloaded here. It is actually shareware but the included command line version pdfpg.exe seems to be free. You should add the location of pdfpg.exe to your system path or specify the full path in the first script line.

The script successively prepares all individual slides and exports them to PDF files. If the Illustrator file is named mydoc.ai the PDF files mydoc_1.pdf, mydoc_2.pdf etc. are created. The final multi-page PDF is called mydoc.pdf.

In order to obtain small and compact files the PDF option preserveEditability is turned off, which means that only the basic PDF data is saved and no additional Illustrator specific information. This works only if the layers not used on a particular slide are deleted and not just hidden. Therefore the script at first saves the original Illustrator file and reloads it after each PDF export.

To save time the script asks at first, which pages are to be converted. This way we don't have to wait for a whole rebuild if just a few pages have been changed. Please mind that the other pages should already exist from a previous script call because at the end of each script all single page PDFs are merged again into the final multi-page PDF.

### Known bugs

The script fails if too many pages are converted in a single run (observed with Illustrator CS). Then the script aborts with an error message which makes no sense to me. It does not depend on the layer content and is probably caused by some memory overflow in Illustrator. However, you can live with it by calling the script again and continuing with the page where the script stopped before.

Arne Alex
+3

The LaTeX.js script needs a little modification to work properly for me. If you encounter only an empty frame when importing a LaTeX formula, try the script below.

 var pdflatexexe="pdflatex.exe"; // Add full path if necessary // determining the local temporary directory var temppath=Folder.temp.fsName; // path already in Windows syntax: c:\... var i=temppath.indexOf("Temporary Internet Files"); if(i>=0) temppath=temppath.substr(0,i+4); //temppath should now contain something like C:\Documents and Settings\Local Settings\Temp // remember the last user input in a text file var lastcode="" var lastcodefile=File(temppath+"\latex2illustrator_lastcode.txt"); if(lastcodefile.exists) { lastcodefile.open("r"); lastcode=lastcodefile.read(); lastcodefile.close(); } // prompt for user input var latexcode=prompt("Please enter LaTeX code",lastcode,"LaTeX"); if(latexcode!=null) { lastcodefile.open("w"); lastcodefile.write(latexcode); lastcodefile.close(); // add latex header etc. to create a complete latex document var latexfile=new File(temppath+'\latex2illustrator.tex'); latexfile.open("w"); latexfile.writeln("\documentclass[11pt]{article}"); // add or remove additional latex packages here latexfile.writeln("\usepackage{amsmath}"); latexfile.writeln("\usepackage{amssymb}"); latexfile.writeln("\usepackage{gensymb}"); // for \degree latexfile.writeln("\usepackage{textcomp}"); // for \textdegree latexfile.writeln("\usepackage{bm}"); // bold math latexfile.writeln("\begin{document}"); latexfile.writeln("\pagestyle{empty}"); // no page number latexfile.writeln(latexcode); latexfile.writeln("\end{document}"); latexfile.close(); var pdffile=File(temppath+"\latex2illustrator.pdf"); if(pdffile.exists) pdffile.remove(); // create a batch file calling latex var batchfile=new File(temppath+'\latex2illustrator.bat'); batchfile.open("w"); batchfile.writeln(pdflatexexe+' -aux-directory="'+temppath+'" -include-direct ory="'+temppath+'" -output-directo ry="'+temppath+'" "'+temppath+'\latex2illustrator.tex"'); //batchfile.writeln('pause'); batchfile.writeln('del "'+temppath+'\latex2illustrator.bat"'); batchfile.close(); batchfile.execute(); for(; batchfile.exists; ) // wait until the batch file has removed itself var pdffile=File(temppath+"\latex2illustrator.pdf"); if(pdffile.exists) { // import pdf file into the current document var grp=app.activeDocument.activeLayer.groupItems.createFromFile(pdffile); // The imported objects are grouped twice. Now move the subgroup // items to the main group and skip the last item which is the page frame for( var i=grp.pageItems[0].pageItems.length-1; i>0; --i) grp.pageItems[0].pageItems[i].move(grp,ElementPlacemen t.PLACEATEND); grp.pageItems[0].remove(); // Move the imported objects to the center of the current view. grp.translate(app.activeDocument.activeView.centerPoint[0]-grp.left, app.activeDocument.activeView.centerPoint[1]-grp.top); } else alert("File "+temppath+"\"+pdffile.name+" could not be created. LaTeX error?"); } 

### Latest Forum Posts

﻿

I requested the wrong release of LaTeX
05/09/2015 02:13, Singularity

Minipage of table and figure
05/09/2015 00:05, dyuxta

Re: biber - Quotation marks in bibliography
04/09/2015 21:55, cgnieder

Re: Right bottom corner - Two different types of page number
04/09/2015 21:50, Johannes_B

Re: Hi, I'm new here
04/09/2015 21:48, Johannes_B