38

I was wondering if anyone had managed exporting .svg files to TikZ code. I do not mean include an .svg file into a latex document, but rather to obtain the code of an image so you can then edit it with TikZ.

I know that this has been done here: http://www.texample.net/tikz/examples/india-map/ using inkscape and the svg converter svg2tikz (https://github.com/kjellmf/svg2tikz) but I cannot reproduce it after installing inkscape.

When I run inkscape, I get the following message on the console (I run it from the console):

(inkscape:8700): Gtk-CRITICAL **: gtk_misc_set_alignment: assertion `GTK_IS_MISC (misc)' failed

I can still import an image and export to TikZ path but I get the following error message in a pop-up window:

Inkscape encountered an internal error and will close now. Automatic backups of unsaved documents were done to the following locations: /home/squeeze/New document 1.2012_06_19_18_10_24.0.svg

and the exported .tex file shows the following code which does not reproduce the image and compiles to a blank page:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[y=0.80pt,x=0.80pt,yscale=-1, inner sep=0pt, outer sep=0pt]
  \begin{scope}[cm={{2.4087301,0.0,0.0,3.1036527,(-508.27381,-1163.9469)}}]
    \begin{scope}[cm={{1.25,0.0,0.0,-1.25,(213.2835,636.20725)}}]
      \begin{scope}[scale=0.100]
        \begin{scope}[cm={{2304.0,0.0,0.0,1195.2,(141.732,141.732)}}]
        \end{scope}
      \end{scope}
    \end{scope}
  \end{scope}

\end{tikzpicture}
\end{document}

Has anyone done it?

I run Debian Squeeze; Inkscape is 0.47 and the forum on the inkscape-svg converter is not really active so I thought someone may have done it here.

I do not wish to specifically use .svg; I use GIMP usually, so if there is such a way to do it in GIMP, I am happy to do it (even better).

Here is an example of an .svg image that fails the above procedure:

enter image description here

8
  • Maybe a bug... Difficult to prove without without an offending svg file example. Jun 19, 2012 at 17:06
  • Can you upload the svg file somewhere so I can try to convert it? I usually do this on Windows 7 Miktex 2.9.
    – azetina
    Jun 19, 2012 at 19:12
  • I've done it, but on a Win7 machine...
    – Count Zero
    Jun 19, 2012 at 19:21
  • OK, here is a link to an image I cannot convert. humbertobz.net/wp-content/uploads/debian-swirl.png. I imported it on inkscape, then saved it as .svg, then opened it and tried to export it. Same problem, although this time the dimension is too large, which I can correct, but I still end up with an empty page when compiling the code provided. Thanks,
    – jejuba
    Jun 20, 2012 at 0:20
  • 2
    That sounds like you just place the bitmap image into inkscape and then save the document. My guess is that the svg-to-TikZ converter does not handle bitmap images. Try to convert your image to a path using the Trace Bitmap function from the Path menu in Inkscape (select the image and press Shift+Alt+B).
    – Fritz
    Jun 20, 2012 at 15:43

2 Answers 2

31

The problem is that your source image is a bitmap (raster image), while TikZ is a vector graphics format. In order to export your bitmap image to TikZ you have to convert it to a vector format first. This process is called "tracing" or "vectorization".

Tracing can be done in numerous ways:

  1. Insert the image into an Inkscape document. Select the image. Press Shift+Alt+B to open the tracing dialogue. Twiddle with the knobs or just press "Ok". Then delete the bitmap.
    • You can also adjust the result manually using the path editing tools provided by inkscape.
  2. While the Inkscape tracing algorithm is free and relatively easy to use, the results are only mediocre, most of the time. Try feeding your favourite search engine terms like "bitmap to vector converter" and try one of the results. Some are quite good.
  3. If you found the image file online, try searching for a vector version of the same image. If you found it via Google image search, you can just append "filetype:svg" to your search query to find only vector images. For example in case of the Debian logo, the Debian website also has an svg version.
  4. Trace the image manually. Place the bitmap in Inkscape, lock the object (as to not accidentally move it) and start painting a path over it, that follows the contours of the bitmap image. This is really tedious.

Once you have a vectorized version of your image, converting it to TikZ with any svg-to-TikZ converter (like svg2tikz) should be possible.

2
  • 1
    by saying "any svg-to-TikZ converter" which one do you have in mind? Can you add some links please?
    – yegor256
    Oct 9, 2014 at 9:11
  • @yegor256: There is a link to inkscape2tikz (now svg2tikz) at the beginning of the question. That is also the only one I know of. However, it seems to have moved to github: github.com/kjellmf/svg2tikz
    – Fritz
    Oct 9, 2014 at 19:07
22

Procedure:

  1. import your bitmap image.
  2. vectorize it by Inkscape (potrace algorithm).
  3. delete bitmap image.
  4. adjust document to content.
  5. export it by inkscape2tikz extension.

Here is the result (with a simplified path to limit size of source):

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\begin{document}
\definecolor{cd60952}{RGB}{214,9,82}

\begin{tikzpicture}[y=0.80pt,x=0.80pt,yscale=-1, inner sep=0pt, outer sep=0pt]
\path[fill=cd60952] (93.3195,230.9447) .. controls (59.0303,225.9343) and
  (31.1801,199.9495) .. (15.3715,170.2215) .. controls (5.5821,151.9201) and
  (1.0492,131.0592) .. (2.0713,110.3467) .. controls (2.9610,99.6354) and
  (-0.0316,88.4491) .. (3.6168,78.1309) .. controls (6.5315,67.3276) and
  (11.4331,57.5607) .. (18.0741,49.0323) .. controls (22.8424,36.3018) and
  (33.0142,26.6294) .. (43.3849,18.3207) .. controls (51.2637,15.0948) and
  (58.7196,8.4670) .. (67.3399,5.6708) .. controls (73.0270,2.1030) and
  (86.2192,-0.8740) .. (89.3376,0.7517) .. controls (87.1714,3.1299) and
  (96.8246,1.5167) .. (97.2769,4.1226) .. controls (107.4188,3.5384) and
  (115.9073,1.2904) .. (125.1827,6.6191) .. controls (130.1554,7.5866) and
  (134.4480,12.7054) .. (139.6428,10.2061) .. controls (146.0329,9.0034) and
  (150.3844,18.7601) .. (153.4049,15.9781) .. controls (167.0146,25.0067) and
  (179.0895,38.0628) .. (183.8766,53.8550) .. controls (180.8933,56.9429) and
  (186.6577,70.4714) .. (181.7816,59.7076) .. controls (180.8871,49.0368) and
  (168.0386,56.5647) .. (177.2058,61.0368) .. controls (179.2498,65.0404) and
  (182.0841,75.4132) .. (177.7802,71.7036) .. controls (180.1689,78.0221) and
  (180.9576,84.7644) .. (181.6522,89.9051) .. controls (179.1588,98.4216) and
  (176.1557,101.9826) .. (173.6042,111.8841) .. controls (170.9595,115.2866) and
  (171.0389,123.7597) .. (166.8992,120.5686) .. controls (165.2312,131.1547) and
  (155.5965,133.9765) .. (147.0796,141.2926) .. controls (132.2241,150.3707) and
  (112.9512,151.3272) .. (97.0139,144.6226) .. controls (84.6896,134.9490) and
  (70.7373,122.6945) .. (71.4998,105.4775) .. controls (70.3035,95.6431) and
  (74.2758,85.9523) .. (80.1058,78.2380) .. controls (88.0500,64.3879) and
  (106.9185,57.9312) .. (121.3004,65.4772) .. controls (125.4446,66.6683) and
  (135.3869,75.8586) .. (125.6405,70.0166) .. controls (107.1390,60.8500) and
  (82.4175,73.0004) .. (78.4654,93.3286) .. controls (76.0174,103.5799) and
  (80.0184,114.3881) .. (84.8167,123.4648) .. controls (93.9795,138.1966) and
  (113.4423,146.7081) .. (130.0863,140.0509) .. controls (139.2098,136.5960) and
  (146.4827,132.8242) .. (153.8322,125.8050) .. controls (158.6678,121.0523) and
  (159.0520,114.5674) .. (163.7343,109.5653) .. controls (167.1049,100.0360) and
  (164.7856,89.6203) .. (165.4952,79.5371) .. controls (164.0280,57.0736) and
  (149.4942,35.8534) .. (129.2629,26.0379) .. controls (115.1366,19.7678) and
  (99.0343,20.6064) .. (84.0979,22.4500) .. controls (80.5464,24.0798) and
  (67.4651,29.1244) .. (69.5458,30.0952) .. controls (72.5853,31.9093) and
  (56.7456,31.7609) .. (54.7847,36.8147) .. controls (50.3008,39.5539) and
  (57.4995,40.3709) .. (50.0780,41.5699) .. controls (38.7079,46.8575) and
  (29.9921,56.8048) .. (22.7900,66.8497) .. controls (24.2223,74.7738) and
  (14.2196,82.5255) .. (15.2267,91.6025) .. controls (14.1505,104.8645) and
  (11.1105,118.9822) .. (16.2776,131.6949) .. controls (15.8162,141.4332) and
  (19.5505,151.1444) .. (28.7610,155.2116) .. controls (30.8205,158.2024) and
  (33.8190,164.2180) .. (28.6238,161.1753) .. controls (32.7528,168.8945) and
  (34.4245,176.2610) .. (38.7270,183.6412) .. controls (45.0741,189.2992) and
  (47.5361,199.7078) .. (55.4974,202.5513) .. controls (63.5448,208.0481) and
  (63.3757,214.0812) .. (72.5802,217.5521) .. controls (79.7480,217.8771) and
  (85.3707,228.8788) .. (94.9911,228.7819) .. controls (100.7223,229.5682) and
  (108.6001,231.6152) .. (98.2271,231.1305) .. controls (96.5907,231.1102) and
  (94.9496,231.1072) .. (93.3195,230.9447) -- cycle(109.3652,153.7333) ..
  controls (96.4622,150.5469) and (82.9305,143.6407) .. (76.0433,131.9692) ..
  controls (79.5521,134.7556) and (88.1011,145.5911) .. (85.2368,139.0080) ..
  controls (93.3693,144.5154) and (101.6217,150.7951) .. (111.5236,152.3574) ..
  controls (116.2089,152.7513) and (130.2176,153.5790) .. (117.6684,154.3630) ..
  controls (114.8884,154.4545) and (112.0843,154.3802) .. (109.3652,153.7333) --
  cycle(74.3902,136.2898) .. controls (70.9933,133.9581) and (67.6115,123.2929)
  .. (72.5676,131.3425) .. controls (72.9289,132.3219) and (79.0347,140.7760) ..
  (74.3902,136.2898) -- cycle(75.3359,126.6654) .. controls (74.7471,124.0079)
  and (76.8271,128.4288) .. (75.3359,126.6654) -- cycle(113.5388,123.1048) ..
  controls (105.3566,120.3225) and (128.7758,120.8069) .. (116.9857,123.7831) --
  (115.4446,123.6429) -- (113.5388,123.1048) -- (113.5388,123.1048) --
  cycle(132.1116,115.1364) .. controls (132.2953,111.7123) and
  (137.6293,113.8487) .. (132.1116,115.1364) -- cycle(139.1412,95.2606) ..
  controls (137.7252,89.2646) and (142.7469,94.5535) .. (139.6272,96.2902) --
  (139.1412,95.2606) -- cycle(0.0872,71.0995) .. controls (-0.2143,65.5523) and
  (4.4217,66.0913) .. (0.0872,71.0995) -- cycle(5.9486,51.8358) .. controls
  (8.5268,42.0593) and (8.4517,55.9487) .. (5.9486,51.8358) --
  cycle(101.3758,1.5022) .. controls (104.1751,0.8687) and (101.0824,2.7449) ..
  (101.3758,1.5022) -- cycle;

\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .