8

I'm trying to position a piece of text at a certain position along a multi-part path. Here's what I have been trying so far.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
  \path[fill=red,even odd rule, postaction={decorate},decoration={markings,mark=at position .2 with {\node (0,20) {my text};}}] (0,0) -- (5,5) -- (5,0);
\end{tikzpicture}
\end{document}

I would like the node to be rotated so that the text is aligned with the path at the position of the node. I can of course manually rotate the node, but I would like this to happen automatically.

Please Note: I know about decorations.text, which could achieve what I'm trying to do. The problem here is that I would need to specify exactly at which relative position the text should appear (for example at 20% of the path length). If you know a way how to achieve this using decorations.text - please go ahead and compile an answer. Otherwise, I'll stick with this node marking hack.

  • 2
    A caveat about using the text along path decoration: That approach doesn't work with things like equations or foreign characters like ü or è. – Jake Aug 19 '13 at 14:44
6

For the extra bit;

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.text}

\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
\draw (-2,-2) rectangle (6,6);
\path[draw=red,
      postaction={decorate},
      decoration={text along path,
                  text=my text,
                  text align={left indent={0.2\dimexpr\pgfdecoratedpathlength\relax}}
                  }
                  ] 
(0,0) -- (5,5) -- (5,0);
\end{tikzpicture}
\end{document}

Notice that with text marking decorations, the position of the text is where the first letter is but not the center of the text. I've added a quick tick decoration to demonstrate every 0.2-long segment but it is not in the code above. Also possible to center by negative phantom spaces etc. but I guess a small tweak won't hurt much if a centering is needed.

enter image description here

4

You can automatically align the node by adding transform shape to the node options:

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[inner sep=0pt, outer sep=0pt]
  \path[
    fill=red,
    postaction={decorate},
    decoration={
        markings,
        mark=at position .2 with {
            \node [transform shape, anchor=base] (0,20) {my text};
        }
    }
  ] (0,0) -- (5,5) -- (5,0);
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.