在下面的代码中,通过原点绘制了两条线。一条线的斜率为 1,另一条线的斜率为 2。我想给这两条线贴上标签j
,k
并将这些标签放在第一象限的箭头处。由于线的斜率j
为1/2
,并且其域的右端点为15
,我想j
用类似这样的方式定位标签
($(15,7.5) + (arctan(1/2):10pt)$)
。
同样,由于线的斜率k
为2
,并且其域的右端点也是15
,所以我想k
用类似的方式定位标签
($(15,30) + (arctan(2):10pt)$)
。
我收到一条错误消息。
\ell_{1}
图中两条垂直线的标签\ell_{2}
放错了位置。为什么我提供的代码没有将这些标签放在线下?
\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}
答案1
问题在于
($(15,7.5) + (arctan(1/2):10pt)$).
arctan
重命名后,atan
TikZ 认为节点以结束参数括号结尾,因此找不到节点名称atan(1/2
。花括号有帮助:
($(15,7.5) + ({atan(1/2)}:10pt)$).
第二个问题
($(5,-10) + (0,-10pt)$)
是 TikZ 为混合表达式选择了错误的坐标系(0, -10pt)
。如果两者都是长度,则 TikZ 会为此坐标规范选择正确的画布坐标系:
($(5,-10) + (0pt,-10pt)$)
(第一个坐标(5,-10)
在环境中默认使用轴坐标系axis
。)
坐标计算的替代方法是使用yshift=-10pt
命令选项\node
。
完整示例:
\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}