我正在查看 tikz/pgf 手册(第 22.5 节),他们有代码
% !!! never ever omit these lines of code !!!
\documentclass[10pt,border=3mm,tikz]{standalone}
\begin{document}
% ... posted code fragment begins
\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot (\x,\x) node[right] {$f(x) =x$};
% \x r means to convert '\x' from degrees to _r_adians:
\draw[color=blue] plot (\x,{sin(\x r)}) node[right] {$f(x) = \sin x$};
\draw[color=orange] plot (\x,{0.05*exp(\x)}) node[right] {$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
% ... end of posted code fragment
% !!! never ever omit these lines of code !!!
\end{document}
但如果我尝试将 $f(x) = \sin x$ 的节点移动到曲线的 80% 处,我会认为
\draw[color=blue] plot (\x,{sin(\x r)}) node[pos=.8] {$f(x) = \sin x$};
应该可以工作,但实际上不行。我猜这是情节未知的问题,但有没有什么解决方案不需要我在空间中手动声明一个节点?
目前结果:
答案1
这是一个简单的解决方案:将节点从其位置移开(135 度,1.5 厘米):
... node[right,shift=(135:1.5)] {$f(x) = \sin x$};
它可能不如使用pos
-parameter好,并且需要一些猜测、反复试验,但它仍然很好、简单、有效。
\documentclass[10pt,border=3mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[domain=0:4]
\draw[very thin,color=gray] (-0.1,-1.1) grid (3.9,3.9);
\draw[->] (-0.2,0) -- (4.2,0) node[right] {$x$};
\draw[->] (0,-1.2) -- (0,4.2) node[above] {$f(x)$};
\draw[color=red] plot (\x,\x) node[right] {$f(x) =x$};
% \x r means to convert '\x' from degrees to _r_adians:
% NEW: shift last node towards the upper left of its last position
\draw[color=blue] plot (\x,{sin(\x r)}) node[right,shift=(135:1.5)] {$f(x) = \sin x$};
\draw[color=orange] plot (\x,{0.05*exp(\x)}) node[right]
{$f(x) = \frac{1}{20} \mathrm e^x$};
\end{tikzpicture}
\end{document}