[Tex/LaTex] How to convert an SVG image with LaTeX in Inkscape, to PNG

conversioninkscapepngsvg

I'm using Inkscape to draw images, together with the ability to produce the separate LaTeX file with all the text, as described here. So far, this has been working great, but now I am restricted to use .png, .jpeg, or .bmp for my figures.

Is there a way for me to keep using Inkscape with SVG + LaTeX and then convert to other formats from the SVG + LaTeX image (e.g. to .png) in such a way that the LaTeX fonts and their scaling are unchanged when compared to the included SVG in the document?

Additional info:

If I just draw SVG in Inkscape, add the LaTex stuff to the picture and export it to a bitmap, the LaTex text will of course not be processed.

If I use TexTex, the scaling of the fonts is lost, which is usually there for an included SVG.

Best Answer

Install inkscape2tikz. The following example is taken from inkscape2tikz home page.

enter image description here

Using inkscape2tikz we export this in to tikz code:

\documentclass{article}
\usepackage{tikz}
\usepackage{charter}               %%%% Added for demo of fonts

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}

\begin{document}
\definecolor{cff8080}{RGB}{255,128,128}

\begin{tikzpicture}[y=0.80pt,x=0.80pt,yscale=-1]
\begin{scope}[draw=black,line join=round,miter limit=4.00,line width=3.200pt]
  \path[draw=black,fill=cff8080,line join=round,even odd rule,line cap=butt,miter
    limit=4.00,line width=3.200pt] (91.0455,165.7194) rectangle
    (250.7252,347.8104);
  \path[draw=black,line join=round,even odd rule,line cap=butt,miter
    limit=4.00,line width=3.200pt] (168.0839,258.1656) .. controls
    (169.6992,260.2725) and (166.0408,261.3824) .. (164.5822,260.8503) .. controls
    (160.6293,259.4083) and (160.6636,254.1057) .. (162.7146,251.1621) .. controls
    (166.3833,245.8967) and (174.0594,246.2369) .. (178.5892,250.1116) .. controls
    (185.2369,255.7979) and (184.5602,266.1224) .. (178.8226,272.1726) .. controls
    (171.1753,280.2366) and (158.1221,279.2067) .. (150.5752,271.5890) .. controls
    (141.0782,262.0028) and (142.4690,246.1872) .. (151.9759,237.1551) .. controls
    (163.4900,226.2161) and (182.0854,227.9716) .. (192.5962,239.3729) .. controls
    (204.9828,252.8090) and (202.8604,274.1941) .. (189.5613,286.1796) .. controls
    (174.2071,300.0173) and (150.0257,297.5267) .. (136.5682,282.3277) .. controls
    (121.2770,265.0577) and (124.1368,238.0757) .. (141.2372,223.1481) .. controls
    (160.4213,206.4017) and (190.2069,209.6312) .. (206.6032,228.6342) .. controls
    (224.8061,249.7311) and (221.2064,282.3227) .. (200.3000,300.1866);
\end{scope}

\end{tikzpicture}
\end{document}

Call this file as image.tex (say). Following is your main.tex file. Put image.tex and main.tex in the same folder. For this example, I have added image.tex by filecontents.

\documentclass{article}
\usepackage{graphicx}
\usepackage{charter}                  %%%% Added for demo of fonts Keep this in image.tex also to have same fonts
\usepackage{filecontents}
\begin{filecontents*}{image.tex}      
    \documentclass{article}            %%% tex file for image begins
    \usepackage{tikz}
    \usepackage{charter}               %%%% Added for demo of fonts
    \usepackage[active,tightpage]{preview}
    \PreviewEnvironment{tikzpicture}

    \begin{document}
    \definecolor{cff8080}{RGB}{255,128,128}

    \begin{tikzpicture}[y=0.80pt,x=0.80pt,yscale=-1]
    \begin{scope}[draw=black,line join=round,miter limit=4.00,line width=3.200pt]
      \path[draw=black,fill=cff8080,line join=round,even odd rule,line cap=butt,miter
        limit=4.00,line width=3.200pt] (91.0455,165.7194) rectangle
        (250.7252,347.8104);
      \path[draw=black,line join=round,even odd rule,line cap=butt,miter
        limit=4.00,line width=3.200pt] (168.0839,258.1656) .. controls
        (169.6992,260.2725) and (166.0408,261.3824) .. (164.5822,260.8503) .. controls
        (160.6293,259.4083) and (160.6636,254.1057) .. (162.7146,251.1621) .. controls
        (166.3833,245.8967) and (174.0594,246.2369) .. (178.5892,250.1116) .. controls
        (185.2369,255.7979) and (184.5602,266.1224) .. (178.8226,272.1726) .. controls
        (171.1753,280.2366) and (158.1221,279.2067) .. (150.5752,271.5890) .. controls
        (141.0782,262.0028) and (142.4690,246.1872) .. (151.9759,237.1551) .. controls
        (163.4900,226.2161) and (182.0854,227.9716) .. (192.5962,239.3729) .. controls
        (204.9828,252.8090) and (202.8604,274.1941) .. (189.5613,286.1796) .. controls
        (174.2071,300.0173) and (150.0257,297.5267) .. (136.5682,282.3277) .. controls
        (121.2770,265.0577) and (124.1368,238.0757) .. (141.2372,223.1481) .. controls
        (160.4213,206.4017) and (190.2069,209.6312) .. (206.6032,228.6342) .. controls
        (224.8061,249.7311) and (221.2064,282.3227) .. (200.3000,300.1866);
    \end{scope}

    \end{tikzpicture}
    \end{document}
\end{filecontents*}                   %%% tex file for image ends
% compile with pdflatex
\immediate\write18{pdflatex image}
% convert to PNG
\makeatletter
\immediate\write18{convert -density 500 -alpha on image.pdf image.png}
\makeatother
%
\begin{document}
\includegraphics[width=5cm]{image}    %%% insert .png file
\end{document}

This assumes that you have imagemagick installed.