有没有办法在我的图中添加轴标签仅有的x = sqrt(2) 和 x = -sqrt(2)?
\documentclass{article}
\usepackage{tikz}
\begin{tikzpicture}
\draw[<->] (-3,0) -- (3,0) node[right] {$x$};
\draw[<->] (0,-3) -- (0,3) node[above] {$y$};
\draw[scale=0.5,domain=-2.5:2.5,smooth,variable=\x,blue] plot ({\x},{\x*\x-2});
\end{tikzpicture}
答案1
欢迎光临!是的。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw[<->] (-3,0) -- (3,0) node[right] {$x$};
\draw[<->] (0,-3) -- (0,3) node[above] {$y$};
\draw[scale=0.5,domain=-2.5:2.5,smooth,variable=\x,blue] plot ({\x},{\x*\x-2});
\draw foreach \X in {-1,1}
{ ({\X*sqrt(2)},0.1) -- ++ (0,-0.2) node[below]{$\ifnum\X<0 -\fi\sqrt{2}$}};
\end{tikzpicture}
\end{document}
或者,如果您想让标签接受比例变换。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw[<->] (-3,0) -- (3,0) node[right] {$x$};
\draw[<->] (0,-3) -- (0,3) node[above] {$y$};
\draw[scale=0.5,domain=-2.5:2.5,smooth,variable=\x,blue] plot ({\x},{\x*\x-2});
\draw[xscale=0.5] foreach \X in {-1,1}
{ ({\X*sqrt(2)},0.1) -- ++ (0,-0.2)
\ifnum\X<0 [below left] \else [below right] \fi node
{$\ifnum\X<0 -\fi\sqrt{2}$}};
\end{tikzpicture}
\end{document}
答案2
这是一个解决方案pgfplots
(尽管我无法删除 x 刻度):
\documentclass[12pt]{article}
\usepackage{amsmath, amssymb}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture} %y=x^-4
\begin{axis}[
axis lines=middle,
xticklabels={\empty},
yticklabels={\empty},
ymajorticks=false,
extra x ticks ={-sqrt(2),sqrt(2)},
extra x tick labels={$\sqrt{2}$,$-\sqrt{2}$},
xmin=-3,xmax=3,
ymin=-3,ymax=3,
extra x ticks={sqrt(2),-sqrt(2)},
xlabel=x,ylabel=y,
samples=200
]
\addplot[domain=-3:3,<->,>=latex,blue] {\x*\x-2};
\end{axis}
\end{tikzpicture}
\end{document}