使用 LaTeX 的默认计数器机制交叉引用 SVG 图形内的项目

使用 LaTeX 的默认计数器机制交叉引用 SVG 图形内的项目

这是基于上一个问题我试图使用该svg包来引用 SVG 绘图内的项目,但从未收到完整的答案。

我尝试采用不同的方法,即使用基本的 LaTeX 计数器机制,如上文所述这里并创建/引用我自己的计数器。

以下是得出的结论:

\documentclass{article}

\usepackage{svg}

\newcounter{svgelem}[figure]
\newcommand{\labelsvgelem}[1]{\thesvgelem\label{#1}}

\begin{document}
    \begin{figure}
        \refstepcounter{svgelem}
        \includesvg{example.svg}
    \end{figure}
   
Referring to \ref{test-rect} as rectangle and \ref{test-circ} as circle 

\end{document}

对于以下 SVG 文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   width="210mm"
   height="297mm"
   viewBox="0 0 210 297"
   version="1.1"
   id="svg349"
   inkscape:version="1.2.2 (732a01da63, 2022-12-09)"
   sodipodi:docname="example.svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <sodipodi:namedview
     id="namedview351"
     pagecolor="#ffffff"
     bordercolor="#000000"
     borderopacity="0.25"
     inkscape:showpageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:deskcolor="#d1d1d1"
     inkscape:document-units="mm"
     showgrid="false"
     inkscape:zoom="1.0255231"
     inkscape:cx="-14.139126"
     inkscape:cy="404.67153"
     inkscape:window-width="2560"
     inkscape:window-height="1377"
     inkscape:window-x="-8"
     inkscape:window-y="-8"
     inkscape:window-maximized="1"
     inkscape:current-layer="layer1" />
  <defs
     id="defs346" />
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <text
       xml:space="preserve"
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;fill:#1100d0;stroke:#1100d0;stroke-width:0;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       x="34.092796"
       y="171.38493"
       id="text1134"><tspan
         sodipodi:role="line"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0;stroke-dasharray:none"
         x="34.092796"
         y="171.38493"
         id="tspan1136">\labelsvgelem{test-circ}</tspan></text>
    <text
       xml:space="preserve"
       style="font-size:2.11667px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';fill:#1100d0;stroke:#1100d0;stroke-width:0;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       x="34.570831"
       y="78.820068"
       id="text1134-3"><tspan
         sodipodi:role="line"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0;stroke-dasharray:none"
         x="34.570831"
         y="78.820068"
         id="tspan1136-8">\labelsvgelem{test-rect}</tspan></text>
    <rect
       style="fill:none;stroke:#1100d0;stroke-width:0.334852;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       id="rect368"
       width="91.481369"
       height="24.185263"
       x="24.678236"
       y="67.893257" />
    <ellipse
       style="fill:none;stroke:#1100d0;stroke-width:0.600001;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       id="path1130"
       cx="54.287098"
       cy="170.39189"
       rx="19.289074"
       ry="14.594594" />
  </g>
</svg>

我编译如下:

latexmk -lualatex --shell-escape test.tex

不幸的是我得到的是:

在此处输入图片描述

如您所见,图中第一个元素的标签确实出现了(尽管很小),但第二个元素的标签却没有出现,我不知道为什么。

我怎样才能让它工作? 有没有更干净的替代方案?

答案1

标签被椭圆的线遮住了:

% !TeX program = txs:///arara
% arara: pdflatex: {synctex: on, interaction: nonstopmode, shell: yes}

\documentclass{article}

\begin{filecontents*}[overwrite]{example.svg}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   width="210mm"
   height="297mm"
   viewBox="0 0 210 297"
   version="1.1"
   id="svg349"
   inkscape:version="1.2.2 (732a01da63, 2022-12-09)"
   sodipodi:docname="example.svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <sodipodi:namedview
     id="namedview351"
     pagecolor="#ffffff"
     bordercolor="#000000"
     borderopacity="0.25"
     inkscape:showpageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:deskcolor="#d1d1d1"
     inkscape:document-units="mm"
     showgrid="false"
     inkscape:zoom="1.0255231"
     inkscape:cx="-14.139126"
     inkscape:cy="404.67153"
     inkscape:window-width="2560"
     inkscape:window-height="1377"
     inkscape:window-x="-8"
     inkscape:window-y="-8"
     inkscape:window-maximized="1"
     inkscape:current-layer="layer1" />
  <defs
     id="defs346" />
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <text
       xml:space="preserve"
       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;fill:#1100d0;stroke:#1100d0;stroke-width:0;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       x="34.092796"
       y="171.38493"
       id="text1134"><tspan
         sodipodi:role="line"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0;stroke-dasharray:none"
         x="34.092796"
         y="171.38493"
         id="tspan1136">\Huge\labelsvgelem{test-circ}</tspan></text>
    <text
       xml:space="preserve"
       style="font-size:2.11667px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';fill:#1100d0;stroke:#1100d0;stroke-width:0;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       x="34.570831"
       y="78.820068"
       id="text1134-3"><tspan
         sodipodi:role="line"
         style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444px;font-family:sans-serif;-inkscape-font-specification:'sans-serif, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-east-asian:normal;stroke-width:0;stroke-dasharray:none"
         x="34.570831"
         y="78.820068"
         id="tspan1136-8">\Huge\labelsvgelem{test-rect}</tspan></text>
    <rect
       style="fill:none;stroke:#1100d0;stroke-width:0.334852;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       id="rect368"
       width="91.481369"
       height="24.185263"
       x="24.678236"
       y="67.893257" />
    <ellipse
       style="fill:none;stroke:#1100d0;stroke-width:0.600001;stroke-linejoin:bevel;stroke-miterlimit:3.5;stroke-dasharray:none"
       id="path1130"
       cx="54.287098"
       cy="170.39189"
       rx="19.289074"
       ry="14.594594" />
  </g>
</svg>

\end{filecontents*}

\usepackage{svg}

\newcounter{svgelem}[figure]
\newcommand{\labelsvgelem}[1]{\refstepcounter{svgelem}\label{#1}\thesvgelem}

\begin{document}

    \begin{figure}
%        \refstepcounter{svgelem}
        \includesvg{example.svg}
    \end{figure}
   
Referring to \ref{test-rect} as rectangle and \ref{test-circ} as circle 

\end{document}

在此处输入图片描述

相关内容