mrpiggi / svg

Handling SVG pictures in LaTeX documents using Inkscape, ImageMagick and/or Ghostscript
Other
65 stars 12 forks source link

svg-extract incompatibility with filecontents #59

Open schtandard opened 5 months ago

schtandard commented 5 months ago

Compiling the MWE

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
\begin{filecontents}[noheader]{drawing.svg}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="96.725mm" height="50.327mm" version="1.1" viewBox="0 0 96.725 50.327" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
 <metadata>
  <rdf:RDF>
   <cc:Work rdf:about="">
    <dc:format>image/svg+xml</dc:format>
    <dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
    <dc:title/>
   </cc:Work>
  </rdf:RDF>
 </metadata>
 <g transform="translate(-27.689 -45.179)">
  <path d="m100.09 88.093-6.259-9.7729-10.235 5.4701 3.7384-10.987-10.658-4.5917 10.921-3.9274-3.0554-11.196 9.8795 6.0894 6.8483-9.3694 1.3988 11.521 11.595-0.48748-8.1351 8.2767 7.6106 8.7615-11.543-1.1998z" fill="#ff0808"/>
  <text x="83.593781" y="83.789787" fill="#000000" font-family="'CMU Serif'" font-size="12.7px" letter-spacing="0px" stroke-width=".26458" text-align="end" text-anchor="end" word-spacing="0px" style="line-height:1.25" xml:space="preserve"><tspan x="83.593781" y="83.789787" font-size="3.8806px" stroke-width=".26458" text-align="end" text-anchor="end">This is a test: $\sum_{k = 0}^\infty 2^{-k} = 2$</tspan></text>
 </g>
</svg>
\end{filecontents}

\documentclass{article}

\usepackage[inkscapearea=page]{svg}
\usepackage{svg-extract}

\begin{document}

\includesvg{drawing}

\end{document}

results in the somewhat obscure error message

Runaway argument?
! File ended while scanning use of ^^M.
<inserted text> 
                \par 
<*> 1-drawing_svg-tex.tex

Looking at 1-drawing_svg-tex.tex we can discover the problem.

%% This file was generated by package `svg-extract'
%% from source `test'
%% It's intended to be compiled with `pdflatex' 
\AtBeginDocument{%
  \svgxsetpapersize%
}
\PassOptionsToPackage{hidelinks}{hyperref}
\begin {filecontents}[noheader]{drawing.svg}
<?xml version="1.0" encoding="UTF-8"?>
<svg width="96.725mm" height="50.327mm" version="1.1" viewBox="0 0 96.725 50.327" xmlns="http://www.w3.org/2000/svg" xmlns:cc="http://creativecommons.org/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<metadata>
<rdf:RDF>
<cc:Work rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
<dc:title/>
</cc:Work>
</rdf:RDF>
</metadata>
<g transform="translate(-27.689 -45.179)">
<path d="m100.09 88.093-6.259-9.7729-10.235 5.4701 3.7384-10.987-10.658-4.5917 10.921-3.9274-3.0554-11.196 9.8795 6.0894 6.8483-9.3694 1.3988 11.521 11.595-0.48748-8.1351 8.2767 7.6106 8.7615-11.543-1.1998z" fill="#ff0808"/>
<text x="83.593781" y="83.789787" fill="#000000" font-family="'CMU Serif'" font-size="12.7px" letter-spacing="0px" stroke-width=".26458" text-align="end" text-anchor="end" word-spacing="0px" style="line-height:1.25" xml:space="preserve"><tspan x="83.593781" y="83.789787" font-size="3.8806px" stroke-width=".26458" text-align="end" text-anchor="end">This is a test: $\sum _{k = 0}^\infty 2^{-k} = 2$</tspan></text>
</g>
</svg>
\end {filecontents}
\documentclass {article}
\usepackage [inkscapearea=page]{svg}
\usepackage {svg-extract}

\usepackage{svg-extract}
\svgxsetbox[lastpage=1]{./svg-inkscape/drawing_svg-tex.pdf}
\usepackage{xr}
\externaldocument{test}

\begin{document}
\pagestyle{empty}
\svgxoutputbox%
\end{document}

All the macros from the original file's preamble are followed by a space. This is not a problem for most macros, but it is in the case of \end {filecontents}.

As a workaround, the filecontents environment can be moved inside the document environment to avoid it being copied to the extraction file (or a different extractpreamble can be used).

mrpiggi commented 5 months ago

Thanks for the report and the provided workaround. I probably have to adapt the catcodes when grabbing the preamble of the main document. I will have some time in June, so that I can work on a new version addressing all the open issues.