\begin{tikzpicture}
\draw[step=2cm,gray,very thin] (-8,-1) grid (9,9);
\draw[thick,->] (-8,0) -- (9,0);
\draw[thick,->] (0,-1) -- (0,9);
\foreach \x in {-6,-4,-2,0,2,4,6,8}
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\foreach \y in {2,4,6,8}
\draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor=east]{$\y$};
\draw[black, thick] (-2,-1) -- (1,2);
\draw[black, thick] (1,2) -- (7,-1);
\filldraw[black] (0,0) node[anchor=north east] {0};
\draw[black,thick,domain=-8:9] plot (\x,{\dfrac{2x^2-4x+5}{x^2+1})}) node[right,black]{$C_f$};
\end{tikzpicture}
答案1
\dfrac
排版分数。要绘制它,您需要使用 pgf 可以理解的函数。使用一个小技巧,您可以避免转换为x
,\x
但您必须输入乘法符号。
\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[step=2cm,gray,very thin] (-8,-1) grid (9,9);
\draw[thick,->] (-8,0) -- (9,0);
\draw[thick,->] (0,-1) -- (0,9);
\foreach \x in {-6,-4,-2,0,2,4,6,8}
{\unless\ifnum\x=0
\draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\x$};
\fi}
\foreach \y in {2,4,6,8}
{\draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor=east]{$\y$};}
\draw[black,thick] (-2,-1) -- (1,2);
\draw[black, thick] (1,2) -- (7,-1);
\filldraw[black] (0,0) node[anchor=north east] {0};
\draw [black,thick,declare function={x=\x;}] plot[smooth,domain=-8:9]
(x,{(2*x^2-4*x+5)/(x^2+1))}) node[right,black]{$C_f$};
\end{tikzpicture}
\end{document}
不用说,pgfplots 使这变得更加方便。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=middle,axis equal,
xmin=-8,xmax=9,ymin=-1,ymax=9,
xtick={-6,-4,-2,0,2,4,6,8},
ytick={2,4,6,8},
grid=major]
\addplot[no marks,thick] coordinates {(-2,-1) (1,2) (7,-1)};
\addplot[no marks,smooth,thick] {(2*x^2-4*x+5)/(x^2+1))} node[pos=1,right] {$C_f$};
\end{axis}
\end{tikzpicture}
\end{document}