7

I have a tikzimage which is actually something I extracted from a vector online. Here it is:

\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}

I'm using a resizebox to make it equal to \textwidth. Usually if I wanted to put any text on a tikzimage, I would use a node. However, that requires inputting coordinates of the node, and I don't know what these coordinates would be in this case.

Is there a simple way to draw some text which would be centered on this tikzimage?

0

2 Answers 2

7

with current bounding box.center you can place something at the centre of the current bounding box:

\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}

For this solution it does not matter if you keep your resizebox, I replaced it with scale=... for not having to worry about the font size.

5
  • Wow, cool! This works. However, I did want to use my resizebox to automatically make the path fit the page, just in case I changed my page size (without automatically having to calculate it). If you don't know how that's possible, though, I think that this answer will be fine! I don't mind calculating the scales myself too much. Jun 8, 2017 at 22:40
  • @SkeletonBow The positioning of the text is independent of whether you use resizebox or scale - in the first case, you will have to increase the font size drastically, but the text is still in the centre. BTW: I guess your resizing does not completely work as parts of the image penetrate into the margin. Jun 8, 2017 at 22:45
  • Well, you're right. Actually, it works fine for my document, but whatever, I'll step away from it :) Jun 8, 2017 at 22:53
  • @SkeletonBow The resizing works, but you have a paragraph indent, as well as the picture, which exceeds the margin.
    – cfr
    Jun 8, 2017 at 23:18
  • @cfr Thanks for your comment, you are completely right, the overfull boxed was caused by the indent. Jun 8, 2017 at 23:23
2
+100

I present a solution which will work even if the path is not the first thing and you do not want to ignore prior stuff for bounding box purposes. Indeed, this solution can be used for multiple svg paths or anything else which you can put in some kind of local box.

This formalises a technique I use a lot. It is based on answers to http://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz by Caramdir and Jake which address drawing on an external image included in a node. The method generalises beautifully with the use of a local bounding box around arbitrary bits of tikzpictures.

I define a new environment {boxhelper} which takes 1 mandatory and 1 optional argument. The mandatory argument should be the name of a local box or node, for example. The optional argument can be used to add TikZ option to the local scope.

The environment assumes you are in a tikzpicture, but that seems reasonable, given its purpose.

Within the environment, you can use coordinates such as (0.1,0.6) to refer to the point 10% of the way across the box from left to right and 60% of the way up the box from bottom to top. (0,0) is the lower left corner and (1,1) is the upper right.

To help with placement, use the TikZ key helper marks. This will add coordinates above, below, left and right of the box and draw a grid over the box. To customise the style, you can redefine the TikZ styles every helper mark and every helper node. If you add this to your picture, the helper markings will appear for every boxhelper environment. If you add it as the optional argument to a particular boxhelper environment, you'll get the grid and so on just there.

The addition of the markings will not alter the current bounding box and should not, therefore, affect anything in the picture or the local scope.

Obviously, switch off the markings for the final version! Either remove the helper marks key or write helper marks=false.

For example, to put a box thing around your svg path, try something like this

\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}

Now we have wiggle which is sufficiently node-like and box-like for our purposes. So we can feed this name to the boxhelper environment, switching on helper marks locally.

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

The resulting grid makes it easy to come up with appropriate coordinates. For example, if wiggle is a snake, we might want to add a node for the mouth and tail. So, we read off the relevant coordinates from the grid, placing the tail marker at an intermediate horizontal position.

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

When we're done labelling, we exit the environment and find ourselves back in the usual coordinate system.

  \end{boxhelper}

annotated <code>wiggle</code>

Complete 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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .