SVG 图形中的箭头颜色错误

SVG 图形中的箭头颜色错误

我用 Inkscape 创建了一个 svg 图形并将其用于我的 LaTeX 文档中。图形中有几条彩色线条,并带有相应的彩色箭头。不幸的是,pdflatex 似乎随意为箭头分配颜色。在 Inkscape 中将 svg 导出为 png 会产生正确的箭头颜色,但我的数学计算当然看起来很丑陋。有没有人遇到过同样的情况,并知道如何获得正确的箭头颜色?已经有两种不同的颜色,所以并不是所有的箭头都得到相同的处理。

这是我的最小例子

\documentclass{article}
\usepackage{svg}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\includesvg{arrowhead}
\caption{wrong colors of arrowheads in svg version}
\end{figure}
\begin{figure}
\includegraphics{pngarrowhead}
\caption{correct colors in png version}
\end{figure}
\end{document}

以下是生成的 pdf(其屏幕截图)

制作的pdf

这是使用 inkscape 导出的 png(pngarrowhead.png)

png箭头.png

这是原始 svg 文件抱歉,我无法直接在这里输入,因此我通过文件托管服务来共享它。

<svg width="98.852mm" height="46.58mm" version="1.1" viewBox="0 0 98.852 46.58" xmlns="http://www.w3.org/2000/svg">
 <defs>
  <marker id="Arrow1Mend" overflow="visible" orient="auto">
   <path transform="matrix(-.4 0 0 -.4 -4 0)" d="m0 0 5-5-17.5 5 17.5 5z" fill="context-stroke" fill-rule="evenodd" stroke="context-stroke" stroke-width="1pt"/>
  </marker>
  <marker id="Arrow1Mstart" overflow="visible" orient="auto">
   <path transform="matrix(.4 0 0 .4 4 0)" d="m0 0 5-5-17.5 5 17.5 5z" fill="context-stroke" fill-rule="evenodd" stroke="context-stroke" stroke-width="1pt"/>
  </marker>
 </defs>
 <g transform="translate(-35.241 -73.283)">
  <path d="m37.042 119.06 47.625-21.167" fill="#0f0" marker-start="url(#Arrow1Mstart)" stop-color="#000000" stroke="#008000" stroke-dasharray="8, 8" style="font-variation-settings:normal"/>
  <g>
   <path d="m84.667 95.25-47.625-21.167" fill="none" marker-end="url(#Arrow1Mend)" stroke="#f0f"/>
   <path d="m84.667 95.25 47.625-21.167" fill="none" marker-end="url(#Arrow1Mend)" stroke="#00f"/>
   <path d="m84.667 95.25 47.625 21.167" fill="#0f0" marker-end="url(#Arrow1Mend)" stroke="#0f0"/>
   <path d="m37.042 116.42 47.625-21.167" fill="#f00" marker-end="url(#Arrow1Mend)" stop-color="#000000" stroke="#f00" style="-inkscape-stroke:none;font-variation-settings:normal"/>
  </g>
  <text x="72.760414" y="82.020821" fill="#000000" font-family="sans-serif" font-size="10.583px" letter-spacing="0px" stroke-width=".26458" word-spacing="0px" style="line-height:1.25" xml:space="preserve"><tspan x="72.760414" y="82.020821" stroke-width=".26458">$x^2$</tspan></text>
 </g>
</svg>

答案1

正如 mrpiggi 正确指出的那样,这是一个已知问题在 inkscape 中。还列出了一种解决方法,非常简单。

在为路径选择标记(如我示例中的箭头)时,请务必始终从下拉列表中选择新的箭头标记,而不是以前使用的箭头标记,因为以前使用的箭头标记会方便地列在列表顶部。要修复损坏的文档,只需为文档中的所有箭头选择新的箭头标记即可。

答案2

在 Inkscape 1.2 中,如果您将文件保存为 SVG 版本 1.1,则可以通过勾选“首选项”>“输入/输出”>“SVG 导出”>“SVG 2 到 SVG 1.1”下的选项来修复这种奇怪的行为,使用正确的标记方向和使用正确的标记颜色。这为我解决了箭头颜色问题和 Firefox 中箭头不推的问题。

答案3

我在 Windows 上使用 Inkscape 1.2.2,其他答案和链接中的 SVG 导出设置建议不起作用。我应用了“使用正确的标记颜色”,然后尝试了多种设置颜色、设置选项和导出,但 Inkscape 中没有任何方法可以解决这个问题。

在文本编辑器中打开导出的 SVG 文件(它只是 XML),发现它始终保存样式颜色,例如

style="fill:context-stroke;fill-rule:evenodd;stroke:context-stroke;stroke-width:0.6pt"

context-stroke用实际颜色替换了这些值,如下所示。这可以在浏览器和其他应用程序中正确呈现。

style="fill:#999999;fill-rule:evenodd;stroke:#999999;stroke-width:0.6pt"

我对<marker> ... <path ...>定义起点和/或终点箭头的每个 XML 组合都执行了此操作。

据我所知,这正是 SVG 导出选项应该做的事情,但它们显然不起作用(总是起作用或者根本不起作用?)。

相关内容