<< Using LyX with Tgif | Page list | WordCount >>
Include SVG Images in LyX
The are two possibilities these days. One based on Inkscape and new one based on Apache Batik. Please read details below about first of these two techniques.
Inkscape
Inkscape from http://www.inkscape.org/ can be used to configure LyX to directly include .svg images and automaticaly generate PNG preview images in lyx, PDF for pdflatex, and EPS for latex.
Just add an SVG file type and conversion rules as described below.
I use those settings for several months now and I am very happy with that solution, because inkscape is a great tool to draw vector graphics with and I don't have to keep the eps files up to date manually to match the svg files - LyX does that for me! Another great thing is, that inkscape exports text to eps as a whole and not single characters (you may not resize text blocks with the mouse to be sure about that). This is useful for psfrag.
Windows
I have tested the scripts from SVG Files and Inkscape and the configuration below in Windows XP with LyX 1.4.2, 1.5.x, 1.6.1 and Inkscape 0.44, 0.46. You need both, the Windows command script inkscape.cmd as well as the linux shell script inkscape. LyX for Windows uses a built in shell for generating the preview images and the windows command interpreter for converting svg to eps. That's why you need both scripts.
You can configure LyX in a few steps:
- Install Inkscape
- Copy the script(s) from SVG Files and Inkscape into your LyX scripts directory and change the INKSCAPE_DIR variable in the script(s) to fit your configuration. You can also change the PNG_BACKGROUND_COLOR Variable to change the background color of the PNG files.
- Adopt your LyX Preferences as follows:
- In the Section Paths add to Path-Prefix the directory where the above scripts are located.
- In the Section File Formats
- In the Section Converters
- Press Save
You need to restart and maybe reconfigure LyX.
Linux
You can configure LyX in a few steps:
- Install Inkscape
- Update 2009-02-23: With LyX-1.6.1 on Ubuntu 8.10, the LyX insert->graphics menu will allow the insertion of SVG files without any customization of the Lyx Preferences. Output documents can be viewed without any configuration changes. However, LyX will not be able to launch the SVG editor (The right-click on the graphic to edit the SVG file will not work). The first set of steps outlined next is needed to let LyX know that the editor program is the executable "inkscape". The end result of that first set of instructions will be new lines in the $/.lyx/preferences file's FORMATS section:
\format "svg" "svg" "SVG" "" "inkview" "inkscape" "vector"
In my experience, this is the only configuration change that is truly necessary to make LyX-1.6.1 work together with SVG and inkscape. The Converter options described below are not strictly necessary, but they may improve the quality of output obtained in pdf and postscript. These settings may make a difference because inkscape might generate more suitable pdf or eps output.
- Adapt your LyX Preferences as follows (The dialog entries are bold.)
- In the Section File Formats
- Fill in the text fields with the following values
Format: svg
GUI-Name: SVG
Extention: svg
Viewer: inkview
Editor: inkscape
- Important: Press add
- Make sure the Vector graphics format checkbox is checked.
The following steps add these lines in the CONVERTERS section of $/.lyx/preferences
\converter "svg" "eps" "inkscape $$i --export-eps=$$o" ""
\converter "svg" "png" "inkscape $$i --export-png=$$o" ""
\converter "svg" "pdf" "inkscape --export-area-drawing $$i --export-pdf=$$o" ""
- In the Section Converters
- Fill in the fields with the following values
From: SVG
To: EPS
Converter: inkscape $$i --export-eps=$$o
- Important: Press add
- Fill in the fields with the following values
From: SVG
To: PDF (ps2pdf)
Converter: inkscape --export-area-drawing $$i --export-pdf=$$o
- Important: Press add
- (Optionally) Fill in the fields with the following values
From: SVG
To: PNG
Converter: inkscape $$i --export-png=$$o
- Important: Press add
- Press Save
You need to restart and maybe reconfigure LyX.
Mac OS X
Under Mac OSX, it should work just as described in the Linux section. You need to provide the full path to the inkscape executable, on my system, this is: /Applications/Multimedia/Inkscape.app/Contents/Resources/bin/inkscape because I have Inkscape.app installed in /Applications/Multimedia.
Hints
- it might work out of the box
- On 3/17/2008 Steve Litt observed in LyX version 1.5.3 that SVG files could be directly placed in the LyX file, even without defining converters for SVG. Your mileage may vary. (Others reported that this produces a bitmap graphic with bad quality)
- preview
- Some have found using
convert (ImageMagick) works better for the preview: Leave out the SVG -> PNG definition and LyX will generate the preview the default way.
You can also use convert with Windows. Replace "inkscape PNG" with "convert" in the converter section.
- text export bug
- There is a bug in Inkscape 0.44: It does not export text correctly to EPS if the font name has spaces in it. In order to succeed in exporting text, it can be converted to "paths", by using the command
inkscape --export-bbox-page -T --without-gui --file=$$i --export-eps=$$o (The text cannot be processed by psfrag after that (see psfrag below).) in the conversion from SVG to EPS, that is the same command used in the instructions above with the -T (--export-text-to-path) option added.
- psfrag
- You can use psfrag (in ERTs) to replace text in the images with LaTeX Text and formulas (It would be great if LyX would support that without ERTs!).
Make sure you do not use the -T (--export-text-to-path) option when converting from SVG to EPS with Inkscape. Likewise, when exporting figures by hand via the Inkscape GUI, the checkbox labeled "Convert text to path" should be unchecked. See the Inkscape manual page for details.
Get the right size
If the SVG is converted to EPS, the size of the resulting graphic is automatically set to the size of the content.
For both, bitmap and PDF output, by default the size is set to the "paper-size" of the canvas (letter or A4).
In order to get a graphic that can be input in a LyX document,
the Inkscape drawing must be sized right. Either add --export-area-drawing to the SVG->PDF converter definition (see above) or, after your drawing is complete, do the following:
- File->Document_Properties.
- Uncheck all checkboxes in the Borders section.
- Select everything in the drawing. (either with Strg-A or dragging a rectangle around all elements with the mouse.)
- Click the "fit page to selection" button.
- Move both scrollbars to their centers and verify that the picture is still visible.
- If the picture is not visible, exit without saving and try again.
- If the picture is still visible, File->Save.
Correction 18/05/08: setting the figure size in Inkscape's File->Document_Properties seems to be mendatory, while the --export-area-drawing doesn't seem to do anything.
Categories