我希望以下代码左侧的输出与下图完全相似。我该怎么做?
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-1.5,0) -- (3.5,0);
\foreach \x in {-1,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-2.5) -- (0,3);
\foreach \y in {-2,-1,1,2,2.5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
\draw[color=black] (0pt,-1pt) node[below left] {\footnotesize $0$};
\clip(-1.5,-2.5) rectangle (3.5,3);
\draw [very thick,domain=-1.5:2.5] plot(\x,{-1*(\x)});
\draw [smooth, very thick,domain=-2:3] plot(\x,{-1*(\x)^2+2});
\node at (2,-2) [right] {\small $(2,-2)$};
\node at (.7,-1.2) [below ] {\small $y=-x$};
\node at (1,1) [right ] {\small $y=2-x^2$};
\node at (-1,1) [left ] {\small $(-1,1)$}; % this line, doesn't appear completely in the output.
\draw [fill=gray,fill opacity=0.3] plot [domain=-1:2](\x,{-1*(\x)^2+2}) -- plot [domain=-2:3] (\x,{-1*(\x)});
\end{tikzpicture}
答案1
问题是你通过 剪辑它\clip(-1.5,-2.5) rectangle (3.5,3);
。因此,我只是将节点移动到前你进行剪辑:
笔记:
- 我将
current bounding box
绘制命令作为注释保留在代码中,因为我发现它在调试这类事情时很有用。 - 正如 Marc van Dongen 指出的那样,这个解决方案之所以有效,是因为
\clip
仅有的影响 之后的内容clip
,您可以通过选项查看剪辑路径\clip[draw]
。由于您通常不希望节点被剪辑,因此您可能应该将节点放在 之前\clip
。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,>=triangle 45,x=1.0cm,y=1.0cm]
\draw[->,color=black] (-1.5,0) -- (3.5,0);
\foreach \x in {-1,-1,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
\draw[->,color=black] (0,-2.5) -- (0,3);
\foreach \y in {-2,-1,1,2,2.5}
\draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
\draw[color=black] (0pt,-1pt) node[below left] {\footnotesize $0$};
\node at (-1,1) [left ] {\small $(-1,1)$}; % this line, didn't used to appear completely in the output.%
\clip(-1.5,-2.5) rectangle (3.5,3);
\draw [very thick,domain=-1.5:2.5] plot(\x,{-1*(\x)});
\draw [smooth, very thick,domain=-2:3] plot(\x,{-1*(\x)^2+2});
\node at (2,-2) [right] {\small $(2,-2)$};
\node at (.7,-1.2) [below ] {\small $y=-x$};
\node at (1,1) [right ] {\small $y=2-x^2$};
\draw [fill=gray,fill opacity=0.3] plot [domain=-1:2](\x,{-1*(\x)^2+2}) -- plot [domain=-2:3] (\x,{-1*(\x)});
%\draw [red, ultra thick] (current bounding box.south west) rectangle (current bounding box.north east);
\end{tikzpicture}
\end{document}