如何将 svg 导出到 tikz

如何将 svg 导出到 tikz

我想知道是否有人成功将 .svg 文件导出到 TikZ 代码。我的意思不是将 .svg 文件包含到 latex 文档中,而是获取图像的代码,然后可以使用 TikZ 对其进行编辑。

我知道这已经在这里完成了:http://www.texample.net/tikz/examples/india-map/使用 inkscape 和 svg 转换器svg2tikz(https://github.com/kjellmf/svg2tikz),但是安装 inkscape 后我无法重现它。

当我运行 inkscape 时,我在控制台上收到以下消息(我从控制台运行它):

(inkscape:8700):Gtk-CRITICAL **:gtk_misc_set_alignment:断言“GTK_IS_MISC (misc)”失败

我仍然可以导入图像并导出到 TikZ 路径,但在弹出窗口中收到以下错误消息:

Inkscape 遇到内部错误,将立即关闭。未保存的文档已自动备份到以下位置:/home/squeeze/New document 1.2012_06_19_18_10_24.0.svg

并且导出的 .tex 文件显示以下代码,该代码不会重现图像并编译为空白页:

\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}

有人做过吗?

我运行的是 Debian Squeeze;Inkscape 是 0.47,并且 inkscape-svg 转换器上的论坛并不活跃,所以我想有人可能在这里做过这件事。

我并不想专门使用 .svg;我通常使用 GIMP,所以如果在 GIMP 中有这样的方法,我很乐意这样做(甚至更好)。

.svg以下是未通过上述程序的图像示例:

在此处输入图片描述

答案1

问题是您的源图像是位图(光栅图像),而 TikZ 是矢量图形格式。为了将位图图像导出到 TikZ,您必须先将其转换为矢量格式。此过程称为“跟踪”或“矢量化”。

追踪可以通过多种方式进行:

  1. 将图像插入 Inkscape 文档。选择图像。按下 打开Shift+Alt+B跟踪对话框。转动旋钮或直接按“确定”。然后删除位图。
    • 您还可以使用 inkscape 提供的路径编辑工具手动调整结果。
  2. 虽然 Inkscape 跟踪算法是免费的,而且相对容易使用,但大多数情况下,结果只是平庸之作。尝试输入您最喜欢的搜索引擎术语,如“位图到矢量转换器”,然后尝试其中一个结果。有些结果相当不错。
  3. 如果您在网上找到了该图片文件,请尝试搜索同一图片的矢量版本。如果您通过 Google 图片搜索找到它,只需将“filetype:svg”附加到您的搜索查询中即可仅查找矢量图像。例如,对于 Debian 徽标,Debian 网站还有 svg 版本。
  4. 手动描摹图像。将位图放入 Inkscape 中,锁定对象(以免意外移动它)并开始在其上绘制一条路径,该路径遵循位图图像的轮廓。这是真的乏味。

获得图像的矢量化版本后,使用任何 svg-to-TikZ 转换器将其转换为 TikZ(像 svg2tikz) 应该是可能的。

答案2

程序:

  1. 导入您的位图图像。
  2. 通过 Inkscape(potrace 算法)对其进行矢量化。
  3. 删除位图图像。
  4. 调整文件内容。
  5. 通过 inkscape2tikz 扩展导出它。

结果如下(使用简化的路径来限制源的大小):

\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}

在此处输入图片描述

相关内容