将文本放在包含 svg 路径的 tikzpicture 上

将文本放在包含 svg 路径的 tikzpicture 上

我有一个 tikzimage,实际上是我从网上的矢量中提取出来的。它如下:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,graphicx}
\usetikzlibrary{svg.path}
\usepackage{showframe}

\begin{document}

\resizebox{\textwidth}{!}{%
\begin{tikzpicture}
\draw svg "M1554.91 315.193s-84.284 199.35-152.003 273.916c-23.735 26.13-75.527 70.43-123.5 44.65-34.282-18.43-60.32-65.55-77.9-105.45-19.533-44.34-35.135-91.87-48.45-134.9-13.84-44.73-26.548-93.75-45.6-137.75-17.836-41.2-46.9-81.06-97.85-85.5-48.686-4.25-83.366 20.24-108.3 44.65-25.405 24.86-48.03 62.27-63.65 90.25-36.565 65.49-64.975 136.7-96.9 209.95-44.65 102.44-53.042 115.6-53.042 115.6s30.243-70.33 47.027-136.83c-11.747-38.35-54.02-189.58-96.583-278.99-20.6-43.27-47.04-86.4-82.65-118.75-33.26-30.21-83.76-57.08-150.1-52.25-40.83 2.97-75.71 20.32-104.5 39.9-29.46 20.03-54.02 44.72-74.1 70.3-42.59 54.24-75.24 120.44-100.7 189.05-26.57 71.57-47.69 144.41-61.75 228-13.26 78.74-28.4 179.26-12.35 264.1 7.97 42.14 23.46 69.97 51.3 93.1 24.36 20.25 66.19 36.04 113.05 24.7 39.04-9.45 65.83-32.56 83.6-60.8 19.8-31.49 20.68-72.64 26.6-118.75 16.25-126.8 43.75-249.32 77.9-356.25 43.06-134.9 106.87-198.71 106.87-198.71s-35.99 46.78-55.34 106.99c47 77.233 72.8 169.694 103.31 266.514 14.8 46.963 29.78 98.05 51.3 142.5 21.19 43.78 50.29 82.013 97.85 95.95 60.53 17.733 109.8-12.28 140.6-43.7 32.66-33.33 54.92-77.318 74.1-119.7 30.55-67.52 54.35-141.068 77.9-212.8 34.2-104.187 61.59-141.236 61.59-141.236s-17.33 37.66-28.02 82.37c29.55 55.84 56.15 111.68 92.77 170.014 17.43 27.77 36.26 55.9 61.75 79.8 23.42 21.97 53.78 46.202 92.15 49.4 59.46 4.95 94.66-28.68 121.6-59.85 73.68-85.267 152-319.52 152-319.52z";
\end{tikzpicture}
}%

\end{document}

我使用 aresizebox使其等于\textwidth。通常,如果我想在 上放置任何文本tikzimage,我会使用一个节点。但是,这需要输入节点的坐标,而我不知道在这种情况下这些坐标是什么。

是否有一种简单的方法来绘制一些以此为中心的文本tikzimage

答案1

current bounding box.center可以在当前边界框的中心放置一些东西:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,graphicx}
\usetikzlibrary{svg.path}
\usepackage{showframe}

\begin{document}

\begin{tikzpicture}[scale=0.21]
\draw[use as bounding box] svg "M1554.91 315.193s-84.284 199.35-152.003 273.916c-23.735 26.13-75.527 70.43-123.5 44.65-34.282-18.43-60.32-65.55-77.9-105.45-19.533-44.34-35.135-91.87-48.45-134.9-13.84-44.73-26.548-93.75-45.6-137.75-17.836-41.2-46.9-81.06-97.85-85.5-48.686-4.25-83.366 20.24-108.3 44.65-25.405 24.86-48.03 62.27-63.65 90.25-36.565 65.49-64.975 136.7-96.9 209.95-44.65 102.44-53.042 115.6-53.042 115.6s30.243-70.33 47.027-136.83c-11.747-38.35-54.02-189.58-96.583-278.99-20.6-43.27-47.04-86.4-82.65-118.75-33.26-30.21-83.76-57.08-150.1-52.25-40.83 2.97-75.71 20.32-104.5 39.9-29.46 20.03-54.02 44.72-74.1 70.3-42.59 54.24-75.24 120.44-100.7 189.05-26.57 71.57-47.69 144.41-61.75 228-13.26 78.74-28.4 179.26-12.35 264.1 7.97 42.14 23.46 69.97 51.3 93.1 24.36 20.25 66.19 36.04 113.05 24.7 39.04-9.45 65.83-32.56 83.6-60.8 19.8-31.49 20.68-72.64 26.6-118.75 16.25-126.8 43.75-249.32 77.9-356.25 43.06-134.9 106.87-198.71 106.87-198.71s-35.99 46.78-55.34 106.99c47 77.233 72.8 169.694 103.31 266.514 14.8 46.963 29.78 98.05 51.3 142.5 21.19 43.78 50.29 82.013 97.85 95.95 60.53 17.733 109.8-12.28 140.6-43.7 32.66-33.33 54.92-77.318 74.1-119.7 30.55-67.52 54.35-141.068 77.9-212.8 34.2-104.187 61.59-141.236 61.59-141.236s-17.33 37.66-28.02 82.37c29.55 55.84 56.15 111.68 92.77 170.014 17.43 27.77 36.26 55.9 61.75 79.8 23.42 21.97 53.78 46.202 92.15 49.4 59.46 4.95 94.66-28.68 121.6-59.85 73.68-85.267 152-319.52 152-319.52z";
\node at (current bounding box.center) (test) {Text};
\end{tikzpicture}

\end{document}

对于这个解决方案,如果您保留它并不重要resizebox,我用它替换它,scale=...因为不必担心字体大小。

答案2

我提出了一个解决方案,即使路径不是第一个,并且您不想为了边界框的目的而忽略先前的内容,它也能发挥作用。事实上,这个解决方案可以用于多个svg paths 或任何其他可以放入某种本地框的东西。

这是我经常使用的一种技术。它基于以下问题的答案:http://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz经过卡拉姆迪尔杰克它解决了在节点中包含的外部图像上进行绘制的问题。该方法使用slocal bounding box的任意位,可以完美地概括tikzpicture

我定义了一个新的环境 {boxhelper},它接受​​ 1 个强制参数和 1 个可选参数。例如,强制参数应该是本地框或节点的名称。可选参数可用于添加 TiZ 选项到本地范围。

环境假设您处于 中tikzpicture,但考虑到其目的,这似乎是合理的。

在环境中,您可以使用诸如 这样的坐标(0.1,0.6)来指代从左到右跨越框 10% 的点以及从下到上跨越框 60% 的点。(0,0)是左下角,(1,1)是右上角。

为了帮助放置,使用 TiZ 键helper marks。这将在框的上方、下方、左侧和右侧添加坐标,并在框上绘制网格。要自定义样式,您可以重新定义 TiZ 样式every helper markevery helper node。如果您将其添加到图片中,则每个boxhelper环境都会出现辅助标记。如果您将其作为可选参数添加到特定boxhelper环境,您将在那里获得网格等。

添加标记不会改变当前边界框,因此不会影响图片或局部范围的任何内容。

显然,关闭最终版本的标记!要么删除密钥helper marks,要么写入helper marks=false

例如,要把一个盒子放在你的周围svg path,试试这样的

\begin{tikzpicture}
  \begin{scope}[local bounding box=wiggle]
    \draw [scale=.225] svg "M1554.91 315.193s-84.284 199.35-152.003 273.916c-23.735 26.13-75.527 70.43-123.5 44.65-34.282-18.43-60.32-65.55-77.9-105.45-19.533-44.34-35.135-91.87-48.45-134.9-13.84-44.73-26.548-93.75-45.6-137.75-17.836-41.2-46.9-81.06-97.85-85.5-48.686-4.25-83.366 20.24-108.3 44.65-25.405 24.86-48.03 62.27-63.65 90.25-36.565 65.49-64.975 136.7-96.9 209.95-44.65 102.44-53.042 115.6-53.042 115.6s30.243-70.33 47.027-136.83c-11.747-38.35-54.02-189.58-96.583-278.99-20.6-43.27-47.04-86.4-82.65-118.75-33.26-30.21-83.76-57.08-150.1-52.25-40.83 2.97-75.71 20.32-104.5 39.9-29.46 20.03-54.02 44.72-74.1 70.3-42.59 54.24-75.24 120.44-100.7 189.05-26.57 71.57-47.69 144.41-61.75 228-13.26 78.74-28.4 179.26-12.35 264.1 7.97 42.14 23.46 69.97 51.3 93.1 24.36 20.25 66.19 36.04 113.05 24.7 39.04-9.45 65.83-32.56 83.6-60.8 19.8-31.49 20.68-72.64 26.6-118.75 16.25-126.8 43.75-249.32 77.9-356.25 43.06-134.9 106.87-198.71 106.87-198.71s-35.99 46.78-55.34 106.99c47 77.233 72.8 169.694 103.31 266.514 14.8 46.963 29.78 98.05 51.3 142.5 21.19 43.78 50.29 82.013 97.85 95.95 60.53 17.733 109.8-12.28 140.6-43.7 32.66-33.33 54.92-77.318 74.1-119.7 30.55-67.52 54.35-141.068 77.9-212.8 34.2-104.187 61.59-141.236 61.59-141.236s-17.33 37.66-28.02 82.37c29.55 55.84 56.15 111.68 92.77 170.014 17.43 27.77 36.26 55.9 61.75 79.8 23.42 21.97 53.78 46.202 92.15 49.4 59.46 4.95 94.66-28.68 121.6-59.85 73.68-85.267 152-319.52 152-319.52z";
  \end{scope}

现在,我们已经有了wiggle足够像节点和盒子的东西,可以满足我们的目的。因此,我们可以将此名称提供给环境boxhelper,在本地进行切换helper marks

  \begin{boxhelper}[helper marks]{wiggle}

生成的网格使得出合适的坐标变得容易。例​​如,如果wiggle是一条蛇,我们可能想要添加一个用于嘴和尾巴的节点。因此,我们从网格中读取相关坐标,将尾部标记放置在中间水平位置。

    \node at (.1,.8) {Mouth};
    \node at (.95,.3) {Tail};

当我们完成标记后,我们退出环境并回到通常的坐标系。

  \end{boxhelper}

带注释的<code>wiggle</code>

完整代码:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{svg.path,positioning}
\usepackage{showframe}
\newif\ifhelpermarks
\newenvironment{boxhelper}[2][]{%
  \begin{scope}[shift=(#2.south west), x=(#2.south east), y=(#2.north west), #1]%
    \ifhelpermarks
      \begin{pgfinterruptboundingbox}%
        \begin{scope}[every helper line, every node/.append style={every helper mark}]
          \foreach \i [evaluate=\i as \j using {\i/10}, evaluate=\i as \k using { \i>0 ? ( \i<10 ? 0.\i : 1 ) : 0 } ] in {0,1,...,10}%
          {%
            \node [below right=2.5pt and \j of #2.south west, anchor=north] {\k};
            \node [above left=\j and 2.5pt of #2.south west, anchor=east] {\k};
            \node [above right=2.5pt and \j of #2.north west, anchor=south] {\k};
            \node [above right=\j and 2.5pt of #2.south east, anchor=west] {\k};
            \draw (\j,0) -- (\j,1) (0,\j) -- (1,\j);
          }%
        \end{scope}%
    \fi
}{%
    \ifhelpermarks
      \end{pgfinterruptboundingbox}%
    \fi
  \end{scope}%
}
\tikzset{%
  helper marks/.is if=helpermarks,
  helper marks/.initial=false,
  helper marks/.default=true,
  every helper mark/.style={help lines, font=\sffamily\scriptsize},
  every helper line/.style={help lines},
}
\begin{document}
\centering
\begin{tikzpicture}
  \begin{scope}[local bounding box=wiggle]
    \draw [scale=.225] svg "M1554.91 315.193s-84.284 199.35-152.003 273.916c-23.735 26.13-75.527 70.43-123.5 44.65-34.282-18.43-60.32-65.55-77.9-105.45-19.533-44.34-35.135-91.87-48.45-134.9-13.84-44.73-26.548-93.75-45.6-137.75-17.836-41.2-46.9-81.06-97.85-85.5-48.686-4.25-83.366 20.24-108.3 44.65-25.405 24.86-48.03 62.27-63.65 90.25-36.565 65.49-64.975 136.7-96.9 209.95-44.65 102.44-53.042 115.6-53.042 115.6s30.243-70.33 47.027-136.83c-11.747-38.35-54.02-189.58-96.583-278.99-20.6-43.27-47.04-86.4-82.65-118.75-33.26-30.21-83.76-57.08-150.1-52.25-40.83 2.97-75.71 20.32-104.5 39.9-29.46 20.03-54.02 44.72-74.1 70.3-42.59 54.24-75.24 120.44-100.7 189.05-26.57 71.57-47.69 144.41-61.75 228-13.26 78.74-28.4 179.26-12.35 264.1 7.97 42.14 23.46 69.97 51.3 93.1 24.36 20.25 66.19 36.04 113.05 24.7 39.04-9.45 65.83-32.56 83.6-60.8 19.8-31.49 20.68-72.64 26.6-118.75 16.25-126.8 43.75-249.32 77.9-356.25 43.06-134.9 106.87-198.71 106.87-198.71s-35.99 46.78-55.34 106.99c47 77.233 72.8 169.694 103.31 266.514 14.8 46.963 29.78 98.05 51.3 142.5 21.19 43.78 50.29 82.013 97.85 95.95 60.53 17.733 109.8-12.28 140.6-43.7 32.66-33.33 54.92-77.318 74.1-119.7 30.55-67.52 54.35-141.068 77.9-212.8 34.2-104.187 61.59-141.236 61.59-141.236s-17.33 37.66-28.02 82.37c29.55 55.84 56.15 111.68 92.77 170.014 17.43 27.77 36.26 55.9 61.75 79.8 23.42 21.97 53.78 46.202 92.15 49.4 59.46 4.95 94.66-28.68 121.6-59.85 73.68-85.267 152-319.52 152-319.52z";
  \end{scope}
  \begin{boxhelper}[helper marks]{wiggle}
    \node at (.1,.8) {Mouth};
    \node at (.95,.3) {Tail};
  \end{boxhelper}
\end{tikzpicture}
\end{document}

相关内容