Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In one of my tikzpictures, I found an angle I labelled \n2. I would like to use this value to rotate a node. However, when I try, in the node options, rotate = \n2, I am told undefined control sequence. My guess is \n2 is only accessible in the \draw let command.

Can I set \n2 to be used globally?


\documentclass[tikz, convert = false]{standalone}%

\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \draw[-stealth] let
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$} \pgfextra{\xdef\myn{\n2}} ;

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw (P5) -- (P6) node[pos = .5, below, font = \tiny, rotate = {\n2}] {$M$};
\end{tikzpicture}
\end{document}

No matter where I place \pgfextra{\xdef\myn{\n2}} after in, I get undefined control sequence

share|improve this question
2  
after in write \pgfextra{\xdef\myn{\n1}} somewhere suitable, preferably at the very end. –  percusse Apr 25 at 15:34
    
My mistake. I changed it to xdef –  percusse Apr 25 at 15:42
3  
After you define \myn you need to use \myn instead of \n2 –  percusse Apr 25 at 15:49
    
@percusse ok thanks. Do you want to make this an answer? –  dustin Apr 25 at 15:50

1 Answer 1

up vote 8 down vote accepted

To do auxillary computations, you can make use of \pgfextra{}. Then you can define a global variable using expanded global definition.

\documentclass[tikz, convert = false]{standalone}%

\usetikzlibrary{calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}[line join = round, line cap = round]
  \coordinate (O) at (0, 0);

  \draw (O) -- +(3, 0) coordinate (P1);
  \draw[name path = sline] (O) -- (3, 2) coordinate (P2);

  \draw[-stealth] let
    \p0 = (O),
    \p1 = (P1),
    \p2 = (P2),
    \n1 = {atan2(\y1 - \y0, \x1 - \x0)},
    \n2 = {atan2(\y2 - \y0, \x2 - \x0)},
    \n3 = {1cm},
    \n4 = {(\n1 + \n2)/2}
  in (O) +(\n1:\n3) arc[radius = \n3,
  start angle = \n1, end angle = \n2] node[right, font = \tiny] at (\n4:\n3)
  {$\theta$} \pgfextra{\xdef\myn{\n2}} ;

  \path[name path = line1] (1.5, 0) -- +(0, 1.25);
  \path[name path = line2] (2, 0) -- +(0, 1.5);
  \path[name intersections = {of = sline and line1, by = P3}];
  \path[name intersections = {of = sline and line2, by = P4}];

  \draw (P3) -- ($(P3)!.25cm!-90:(O)$) coordinate (P5);
  \draw (P4) -- ($(P4)!.25cm!-90:(O)$) coordinate (P6);
  \draw (P5) -- (P6) node[pos = .5, below, font = \tiny, rotate = {\myn}] {$M$};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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