2

Nothing's wrong with the PNG as far as I'm aware, but I'm getting some wacky behavior from it (MWE below). The problem goes away when I remove -transparent white from the ImageMagick options.

MWE

% arara: xelatex: {shell: yes}
\begin{filecontents*}{heart.svg}
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
  <g transform="matrix(26.37104, 0, 0, 24.680786, 232.422821, 233.229111)" style="fill: rgb(223, 0, 0); fill-opacity: 1;">
    <path style="fill:#df0000;fill-opacity:1" d="M 3.676,-9 C 0.433,-9 0,-5.523 0,-5.523 0,-5.523 -0.433,-9 -3.676,-9 -5.946,-9 -8,-7.441 -8,-4.5 -8,-0.614 -1.4208493,3.2938141 0,9 1.35201,3.2985969 8,-0.614 8,-4.5 8,-7.441 5.946,-9 3.676,-9 z"/>
  </g>
</svg>
\end{filecontents*}
\immediate\write18{convert -units PixelsPerInch -size 2000x2000 heart.svg -density 600 -trim -transparent white heart.png}
\documentclass{article}
\usepackage{graphicx}

\newcommand\CHeart[1][]{\includegraphics[width=\linewidth,#1]{heart.png}}

\begin{document}
\CHeart
\end{document}

Output: (blank pdf sometimes, other times…)

output

Version: ImageMagick 6.9.6-7 Q16 x86_64 2016-12-07 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules 
Delegates (built-in): bzlib cairo fontconfig freetype jng jpeg ltdl lzma png rsvg tiff xml zlib

Using -background none before the SVG (instead of -transparent white before the PNG) gets me this lovely 'modern art':

output

Sean Allred
  • 27,421

1 Answers1

0

Too long for a comment.

I do get the expected normal heart with transparent background with option -transparent white or a white background with -background none. Compiler: pdflatex -shell-escape and xelatex -shell-escape

Version of ImageMagick: 6.9.5-7 Q16 x86_64 2016-08-27

Version of XeTeX: 3.14159265-2.6-0.99996 (TeX Live 2016)

Transparent background

\RequirePackage{filecontents}% overwriting environment "filecontents"
\begin{filecontents*}{heart.svg}
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
  <g transform="matrix(26.37104, 0, 0, 24.680786, 232.422821, 233.229111)" style
    <path style="fill:#df0000;fill-opacity:1" d="M 3.676,-9 C 0.433,-9 0,-5.523
  </g>
</svg>
\end{filecontents*}
\immediate\write18{convert -units PixelsPerInch -size 2000x2000 heart.svg -densi
%\immediate\write18{convert -units PixelsPerInch -size 2000x2000 heart.svg -dens
\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\newcommand\CHeart[1][]{\includegraphics[width=\linewidth,#1]{heart.png}}

\begin{document}
\pagecolor{yellow}
\noindent
\CHeart
\end{document}

Result with transparent background

White background

\RequirePackage{filecontents}% overwriting environment "filecontents"
\begin{filecontents*}{heart.svg}
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
  <g transform="matrix(26.37104, 0, 0, 24.680786, 232.422821, 233.229111)" style
    <path style="fill:#df0000;fill-opacity:1" d="M 3.676,-9 C 0.433,-9 0,-5.523
  </g>
</svg>
\end{filecontents*}
%\immediate\write18{convert -units PixelsPerInch -size 2000x2000 heart.svg -dens
\immediate\write18{convert -units PixelsPerInch -size 2000x2000 heart.svg -densi
\documentclass{article}
\usepackage{graphicx}
\usepackage{color}

\newcommand\CHeart[2][]{\includegraphics[width=\linewidth,#1]{heart.png}}

\begin{document}
\pagecolor{yellow}
\noindent
\CHeart
\end{document}

Result with white background

The standard environment filecontents does not overwrite the file. If the contents of heart.svg changes, the file must be deleted first to get the new version on the next compile run.

The example uses package filecontents which always overwrites the files.

\pagecolor{yellow} is used to show the different background types.

TikZ version

The path description of the SVG file can directly be used in TikZ to get a vector graphics without external image file:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{svg.path}
\usepackage{xcolor}
\usepackage{graphicx}
\definecolor{heartred}{RGB}{223, 0, 0}
\begin{document}
\pagecolor{yellow}
\noindent
\resizebox{\linewidth}{!}{%
  \tikz\fill[heartred, yscale=-1] svg {
    M 3.676,-9 C 0.433,-9 0,-5.523 0,-5.523 0,-5.523 -0.433,-9 -3.676,
    -9 -5.946,-9 -8,-7.441 -8,-4.5 -8,-0.614 -1.4208493,3.2938141 0,
    9 1.35201,3.2985969 8,-0.614 8,-4.5 8,-7.441 5.946,-9 3.676,-9 z
  };%
}
\end{document}
Heiko Oberdiek
  • 271,626