5

In the following code, two lines are drawn through the origin. One has slope 1 and the other has slope 2. I want to label these lines j and k and put these labels at the arrowhead in the first quadrant. Since the slope of line j is 1/2 and since the right endpoint of its domain is 15, I would like to position the label j with something like

($(15,7.5) + (arctan(1/2):10pt)$).

Likewise, since the slope of line k is 2 and since the right endpoint of its domain is also 15, I would like to position the label k with something like

($(15,30) + (arctan(2):10pt)$).

I got an error message.

The labels for two vertical lines \ell_{1} and \ell_{2} in the drawing are misplaced. Why doesn't the code that I provide put these labels under the lines?

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in,clip=false,axis equal image,
    axis lines=middle,
    xmin=-5,xmax=15,
    xlabel=$x$,ylabel=$y$,
    ymin=-10,ymax=30,
    restrict y to domain=-10:30,
    enlargelimits={abs=0.5cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-5:15,blue,name path = A] {0.5*x};
\addplot[latex-latex,samples=2,domain=-5:15,blue,name path = B] {2*x};
\addplot[latex-latex,samples=2,domain=-10:30,blue,name path = C] (5,x);
\addplot[latex-latex,samples=2,domain=-10:30,blue,name path = D] (9,x);

%These commands label the lines.
\node[font=\footnotesize] at ($(15,7.5) + (45:10pt)$) {$j$};
\node[font=\footnotesize] at ($(15,30) + (60:10pt)$) {$k$};
\node[font=\footnotesize] at ($(5,-10) + (0,-10pt)$) {$\ell_{1}$};
\node[font=\footnotesize] at ($(9,-10) + (0,-10pt)$) {$\ell_{2}$};


\end{axis}
\end{tikzpicture}

\end{document}
Adelyn
  • 3,373

1 Answers1

5

The problem with

($(15,7.5) + (arctan(1/2):10pt)$).

is that after renaming arctan to atan TikZ thinks that the node ends with the closing argument bracket and will not find the node name atan(1/2. Curly braces help:

($(15,7.5) + ({atan(1/2)}:10pt)$).

The second problem with

($(5,-10) + (0,-10pt)$)

is that TikZ chooses the wrong coordinate system for the mixed expression (0, -10pt). If both are lengths, then TikZ chooses the correct canvas coordinate system for this coordinate specification:

($(5,-10) + (0pt,-10pt)$)

(The first coordinate (5,-10) is using the axis coordinate system as default in environment axis.)

Alternative to the coordinate calculation is using option yshift=-10pt to the \node command.

Full example:

\documentclass{amsart}
\usepackage{tikz}
\usetikzlibrary{calc,angles,positioning,intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\begin{tikzpicture}
\begin{axis}[width=5in,clip=false,axis equal image,
    axis lines=middle,
    xmin=-5,xmax=15,
    xlabel=$x$,ylabel=$y$,
    ymin=-10,ymax=30,
    restrict y to domain=-10:30,
    enlargelimits={abs=0.5cm},
    axis line style={latex-latex},
    ticklabel style={font=\tiny,fill=white},
    xtick={\empty},ytick={\empty},
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[latex-latex,samples=2,domain=-5:15,blue,name path = A] {0.5*x};
\addplot[latex-latex,samples=2,domain=-5:15,blue,name path = B] {2*x};
\addplot[latex-latex,samples=2,domain=-10:30,blue,name path = C] (5,x);
\addplot[latex-latex,samples=2,domain=-10:30,blue,name path = D] (9,x);

%These commands label the lines.
\node[font=\footnotesize] at ($(15,7.5) + ({atan(1/2)}:10pt)$) {$j$};
\node[font=\footnotesize] at ($(15,30) + ({atan(2)}:10pt)$) {$k$};
\node[font=\footnotesize] at ($(5,-10) + (0pt,-10pt)$) {$\ell_{1}$};
\node[font=\footnotesize] at ($(9,-10) + (0pt,-10pt)$) {$\ell_{2}$};           

\end{axis}
\end{tikzpicture}

\end{document}

Result

Heiko Oberdiek
  • 271,626
  • Yes, I have to include { and } so that TikZ doesn not misinterpret the first appearance of a right parenthesis. Thanks. – Adelyn May 25 '15 at 17:49
  • Why isn't there any confustion with at ($(15,7.5) + ({atan(1/2)}:10pt)$)? The (15,7.5) is a point with respect to the axis coordinate system, but the 10pt in ({atan(1/2)}:10pt) is a measurement in terms of the unit point. – Adelyn May 25 '15 at 17:49
  • @Adelyn The polar coordinate system is taken because of the colon. – Heiko Oberdiek May 25 '15 at 17:53
  • If you have (1,1pt) as the coordinates of a point, where will TikZ place it? – Adelyn May 25 '15 at 17:58
  • 1
    @Adelyn (1, 1pt) is placed at x=1 and 1pt above the x-axis in axis coordinate system. The problem with ($(1,2) + (3, 4)$) is, that the axis coordinate system has a different origin than the canvas coordinate system, the origin of the drawn axes is somewhere in the middle. To correct this, the origin has to be distracted. Thus the formula would be starting point plus difference: ($(1, 2) + (3, 4) - (0, 0)$) – Heiko Oberdiek May 25 '15 at 18:09
  • I understand your comment about the placement of a node at (1,1pt). I have not eard of the "canvas coordinate system." Why wouldn't the position of the node ($(1,2) + (3, 4)$) be at (4,6) with respect to the axis coordinate system? – Adelyn May 25 '15 at 18:22
  • This reminds me of the (recent) post Help with Help Lines. The grid lines are not drawn with respect to the axis coordinate system. – Adelyn May 25 '15 at 18:22
  • @Adelyn The canvas coordinate system is the normal coordinate system in TikZ. You have xmin=-5 and ymin=-10. Then (1,2) transformed to pt values with origin at the lower left corner (-5, -10) of your axis plot area. Thus (1, 2) are (6u, 12u) from the lower left corner with u as fictive factor between axis units to pt. Then (3, 4) is added, also (8u, 14u). The sum is (14u, 26u) or back to axis coordinates: (9, 16). – Heiko Oberdiek May 25 '15 at 18:30
  • @Adelyn See also a visualization in a previous answer from me. – Heiko Oberdiek May 25 '15 at 18:33
  • I am looking at your answer now. In your other post, you use "relative coordinates" to get the proper positioning by "adding" points. Would that apply to the post Help with help lines? How would grid lines be drawn so that they align with the axis coordinate system? The command given is draw[help lines,blue] (-10.5, -10.5) grid (10.5,10.5); – Adelyn May 25 '15 at 18:43
  • @Adelyn I have written an answer there. – Heiko Oberdiek May 25 '15 at 19:24
  • I am still looking at your code at the post "Calculation of coordinates for TikZ annotations in PGFplots axis environment." I appreciate your help. – Adelyn May 25 '15 at 19:32