1

This questions is based on Jake's answer given here, and the sample code is modified from there.

I am using the markings library to get obtain a tangent coordinate system, but would like to combine this with the |- command in the coordinate specification. For a MWE, consider this:

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

\begin{document}

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

\draw [ tangent=0.4, tangent=0.56 ] (0,0) to [out=20,in=120] (5,2) to [out=-60, in=110] (9,1); \path (6,4) coordinate (x); \draw [orange, thick, use tangent=2] (-2,0) -- (2,0) (0,0 -| x) -- (x); \end{tikzpicture} \end{document}

The original code had

\draw [orange, thick, use tangent=2] (-2,0) -- (2,0) (0,0) -- (0,1);

instead of

\draw [orange, thick, use tangent=2] (-2,0) -- (2,0) (0,0 -| x) -- (x);

and obtained a nice orange tangent coordinate system. In my case, I want the orthogonal line not be centered at (0,0) but somewhere off to the side so that it ends at the (named) node x. Is there a way to adapt the |- so that it, too, uses the tangent coordinate system rather than the original one?

In pictures, my current code produces a line that is vertical in the original coordinate system: What I have What I would like to see is a line that is vertical in the tangent coordinate system: What I want

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
mimuller
  • 800

2 Answers2

2

Your image is still in cartesian coordinate system. Used solution only enable to draw tangent to selected point on drawn figure. To draw projection to the tangent you need coordinate calculation with use of the calc TikZ library.

By use of projection coordinate defined in the calc library (for details see section 13.5.5 The Syntax of Projection Modifiers, TikZ & PGF manual v 3.1.9a, page 151):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, % <---
                decorations.markings}

\begin{document}

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

\draw [ tangent=0.4, tangent=0.56 ] (0,0) to [out=20,in=120] (5,2) to [out=-60, in=110] (9,1); \path (6,4) coordinate (x); \draw [orange, thick, use tangent=2] (-2,0) coordinate (a) -- (2,0) coordinate (b); % <--- \draw [red, thick] (x) -- ($(a)!(x)!(b)$); % <--- \end{tikzpicture}

\end{document}

enter image description here

Zarko
  • 296,517
2

You can change the definition of use tangent to use a true rotation (using the calc TikZ library and the \anglebetween macro):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,decorations.markings}

\makeatletter \def\anglebetween#1#2#3{%macro, s, t \path let \p{s}=(#2), \p{t}=(#3), \n{angle}={atan2(\y{t}-\y{s},\x{t}-\x{s})} in \pgfextra { \pgfmathsetmacro\temp{\n{angle}} \xdef#1{\temp} }; } \makeatother

\begin{document}

\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 (1cm,0pt); \coordinate (tangent orthogonal unit vector-\pgfkeysvalueof{/pgf/decoration/mark info/sequence number}) at (0pt,1cm); } }, postaction=decorate }, use tangent/.style={ /utils/exec={\anglebetween{\tangentangle}{0,0}{$(tangent unit vector-#1)-(tangent point-#1)$}}, shift=(tangent point-#1), rotate=\tangentangle, }, use tangent/.default=1 ]

\draw [ tangent=0.4, tangent=0.56 ] (0,0) to [out=20,in=120] (5,2) to [out=-60, in=110] (9,1);

\path (6,4) coordinate (x); \drawuse tangent=2,orange, thick -- (2,0) (0,0 -| x) -- (x); \end{tikzpicture} \end{document}

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
  • Thank you! Both answers are REALLY nice, I upvoted both but ultimately decided to go with the other one because it seems to require less "custom code" that I need to copy into the specific document. – mimuller May 29 '21 at 11:52