答案1
使用tkz-fct
软件包,使用\tkzFct[<options>]{<function>}
,--shell-escape
启用后进行编译,我相信您需要已经gnuplot
安装。
\documentclass[border=5mm]{standalone}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}[scale = 0.5]
\tkzInit[xmax=5,ymax=5,xmin=-5,ymin=-5]
\tkzGrid
\tkzLabelX[orig=true,label options={font=\tiny}]
\tkzLabelY[orig=false,label options={font=\tiny}]
\tkzDrawX
\tkzDrawY
\tkzFct[domain=-5:0,black]{\x**2-4}
\tkzFct[domain=0:5,black]{2*\x-4}
\end{tikzpicture}
\end{document}
如果您想要一种不涉及gnuplot
和的方法shell-escape
,您可以尝试pgfplots
:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}[scale = 0.5]
\begin{axis}[
xmin=-5.5,xmax=5.5,
ymin=-5.5,ymax=5.5,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
grid,
xtick={-5,...,5},
ytick={-5,...,5},
ticklabel style={font=\tiny,fill=white}]
\addplot [black,domain=-5:0] {x^2-4};
\addplot [black,domain=0:5] {2*x-4};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
谢谢,@Torbjørn T. 我想我刚刚通过使用\draw
\documentclass[border=5mm]{standalone}
\usepackage{tkz-fct}
\usepackage{tkz-euclide}
\usepackage{graphicx}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}[scale = 0.5]
\tkzInit[xmax=5,ymax=5,xmin=-5,ymin=-5]
\tkzGrid
\tkzLabelX[orig=true,label options={font=\tiny}]
\tkzLabelY[orig=false,label options={font=\tiny}]
\tkzDrawX
\tkzDrawY
\draw[scale=1, domain=-3:0,smooth,variable=\x,blue, line width = 1pt] plot ({\x},{\x*\x-4});
\draw[scale=1, domain=0:4.5, smooth, variable=\x, blue, line width = 1pt] plot({\x}, {2*\x-4});
\end{tikzpicture}
\end{document}