Pages

Friday, August 2, 2013

Adding vector graphics images (svg) in Latex/pdf files


Adding an image in svg format in Latex keeps the picture intact on performing reasonable zoom in  and zoom out which means, its quality is not distorted.

There are a few easy steps to create and add svg image. First we will create a very simple image using Inkscape and then add into the Latex file.

Step 1:

Download and install latest version of Inscape from here. I downloaded 'inkscape-0.48.4-1-win32.exe' file.

Step 2:
 
Create a new Default page in Inkscape and add some random rectangles and circles. You can definitely create your desired figure but right now, create only without adding any text.

Step 3:

For my test example, I created the following one. Click Save-as and choose .pdf format. There will be one dialogue shown up before pdf file is created. Choose following configurations. PDF+Latex checkbox is important here. When you click on OK button, two files are created; .pdf and .pdf_tex


I created following image for my testing which I changed later, never mind.


Step 4:

Go to top of your Latex document where you add/use other packages. Ctrl+c and Ctrl+v following lines there. {First line will add 'color' package}

\usepackage{color}
\newcommand{\executeiffilenewer}[3]{%
\ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
{\pdffilemoddate{#2}}>0%
{\immediate\write18{#3}}\fi%
}
\newcommand{\includesvg}[1]{%
\executeiffilenewer{#1.svg}{#1.pdf}%
{inkscape -z -D --file=#1.svg %
--export-pdf=#1.pdf --export-latex}%
\input{#1.pdf_tex}%
}


Step 5:

Now we will add image we created in step-3. Add following lines in the latex document where you want to add image. We will give only name here without giving extension (.pdf_tex).


\begin{figure}
\centering
\def\svgwidth{\columnwidth}
\includesvg{test2}
\end{figure}



test2 is the file name we want to add and it should be in the same folder where .tex file is. At this point, this test2 image should be displayed in the pdf file.

However, if we add text to this file, its not displayed in the generated pdf file from Inkscape.

Step 6:

Add a text object by clicking text icon in the lower left corner as shown in following picture. Type  '$$' text here.


 Now click on arrow icon and right click on this object. Go to 'object properties'.


 Anything you type in Title and Description will be appeared in the output file. Set the value and save it as .pdf with the same settings.



Step 7:

Now recompile your latex file. It should show the text. Rest is just playing with these features.

Draw Figure with Visio:

Drawing figures in Inkscape seemed a little bit difficult to me as I was not able to figure out how to draw arrow and other simple shapes easily.

So create a figure in Microsoft Visio,

Ctrl+a, Ctrl+c and then Ctrl+v in Inkscape and save it as .pdf with the same settings.  

OR

Export it as .svg from visio, and import it into the Inkscape.


No comments:

Post a Comment