2

Question

How to add text to arbitrary positions in TikZ-pictures relative to certain objects (maybe even following their shape) ?

Situation

I have combined two solutions to Tikz-questions found here on stackexchange, namely:

Even though I could create the desired drawing, I do not fully understand the provided solutions. Therefore the following problem came up: I was unable to figure out the right way to insert some text (a is not close enough and could be rotated along the curve) as depicted in this image:

desired result

MWE

\documentclass[preview]{standalone}

\usepackage{filecontents}

\usepackage{tikz}
    \usetikzlibrary{decorations.markings}
    \usetikzlibrary{intersections}
\begin{filecontents}{projection.tikz}
    % tangent: https://tex.stackexchange.com/a/25940/74942
    % intersection: https://tex.stackexchange.com/a/58216/74942
    \begin{tikzpicture}[
    tangent/.style={
        decoration={
            markings,% switch on markings
            mark=
            at position #1
            with
            {
                \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
            }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
    ] % tangent

    \draw[help lines,xstep=.5,ystep=.5] (0,0) grid (4,3); %grid

    \draw [
    name path=curve,
    tangent=0.4
    ] (0,0)
    to [out=60,in=180] (2,2)
    to [out=0, in=160] (3,1.75)
    to [out=-20, in=-120] (4,3);

    \draw [blue, name path=tangent, use tangent] (-1,0) -- (2,0)
    node[sloped,inner sep=0cm,below,pos=.8,
    anchor=north west,
    minimum height=1cm,
    minimum width=1cm](N){a};

    \path [name path=proj] (N.north west)% intersection
    node (P) {b} -- (N.south west);

    \draw [name intersections={of=curve and proj}, blue] (P.center) 
    --
    (intersection-1);

    \end{tikzpicture}
\end{filecontents}

\begin{document}
\input{./projection.tikz}
\end{document}
3

1 Answer 1

4

With Andrew Swanns hint I was able to figure it out:

result

MWE

\documentclass[preview]{standalone}

\usepackage{filecontents}

\usepackage{tikz}
    \usetikzlibrary{decorations.markings}
    \usetikzlibrary{decorations.text}
    \usetikzlibrary{intersections}
\begin{filecontents}{projection.tikz}
    % tangent: https://tex.stackexchange.com/a/25940/74942
    % intersection: https://tex.stackexchange.com/a/58216/74942
    % text along path: https://tex.stackexchange.com/a/22316/74942
    \begin{tikzpicture}[
    tangent/.style={
        decoration={
            markings,% switch on markings
            mark=
            at position #1
            with
            {
                \coordinate (tangent point-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,0pt);
                \coordinate (tangent unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (1,0pt);
                \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1);
            }
        },
        postaction=decorate
    },
    use tangent/.style={
        shift=(tangent point-#1),
        x=(tangent unit vector-#1),
        y=(tangent orthogonal unit vector-#1)
    },
    use tangent/.default=1
    ] % tangent

    \draw[help lines,xstep=.5,ystep=.5] (0,0) grid (4,3); %grid

    \def\myshift#1{\raisebox{-2.5ex}} %text along path - height from line
    \draw [
                name path=curve,
                tangent=0.4, % where tangent is set (percent of total curve)
                postaction={decorate,
                    decoration={text along path,
                        text align=center,
                        text={|\sffamily\footnotesize\myshift| and maybe here too }
                    }
                } %text command
                ] (0,0)
                    to [out=60,in=180] (2,2)
                    to [out=0, in=160] (3,1.75)
                    to [out=-20, in=-120] (4,3);

    \def\myshift#1{\raisebox{1ex}} %text along path - height from line
    \draw [blue, 
                name path=tangent,
                use tangent,
                postaction={decorate,
                    decoration={text along path,
                        text align=center,
                        text={|\sffamily\footnotesize\myshift| I want some text here }
                    }
                } %text command
                ] (-1,0) -- (2,0)
    node[sloped,inner sep=0cm,below,pos=.8,
    anchor=north west,
    minimum height=1cm,
    minimum width=1cm](N){a};

    \path [name path=proj] (N.north west)% intersection
    node (P) {b} -- (N.south west);

    \draw [ name intersections={of=curve and proj},
                blue,
                postaction={decorate,
                    decoration={text along path,
                        text align=center,
                        text={|\sffamily\tiny\myshift| and here }
                    }
                } %text command
            ] (P.center) 
    --
    (intersection-1);

    \end{tikzpicture}
\end{filecontents}

\begin{document}
\input{./projection.tikz}
\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.