\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\foreach \x in {-3,...,3}
\draw (\x/2,1pt) -- (\x/2,-2pt);
\foreach \y in {-3,...,3}
\draw (1pt,\y/2) -- (-2pt,\y/2);
\draw[thick,db] (0,0) circle (1.5cm);
\foreach \x [evaluate=\x as \r using \x/1.5] in {-1.5, 1.5}
\draw (\x cm,1pt) -- (\x cm,-1pt)
node[anchor=north, fill=white] {\tiny $\r$};
\end{tikzpicture}%
\end{document}
结果是
我希望它显示 -1 和 1,而不是 -1.0 或 1.0。该怎么做?
答案1
int(...)
为了获得整数结果,请在计算\r
节点文本时使用 TikZ 函数:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\foreach \x in {-3,...,3}
\draw (\x/2,1pt) -- (\x/2,-2pt);
\foreach \y in {-3,...,3}
\draw (1pt,\y/2) -- (-2pt,\y/2);
\draw[thick,blue] (0,0) circle (1.5cm);
\foreach \x [evaluate=\x as \r using int(\x/1.5)] in {-1.5, 1.5}
\draw (\x cm,1pt) -- (\x cm,-1pt)
node[anchor=north, fill=white] {\tiny $\r$};
\end{tikzpicture}%
\end{document}
答案2
只是为了完整性。在这种情况下,不需要评估任何东西,因为坐标会自动解析,所以你可以简单地做
\foreach \X in {-1, 1}
\draw (\X*1.5cm,1pt) -- (\X*1.5cm,-1pt)
node[anchor=north, fill=white,font=\tiny] {$\X$};
请注意,\x
如果您打算使用,这可能不是一个好的循环变量calc
。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-2,0) -- (2,0) node[right] {$x$};
\draw[->] (0,-2) -- (0,2) node[above] {$y$};
\foreach \X in {-3,...,3}
\draw (\X/2,1pt) -- (\X/2,-2pt);
\foreach \y in {-3,...,3}
\draw (1pt,\y/2) -- (-2pt,\y/2);
\draw[thick,blue!80!black] (0,0) circle (1.5cm);
\foreach \X in {-1, 1}
\draw (\X*1.5cm,1pt) -- (\X*1.5cm,-1pt)
node[anchor=north, fill=white,font=\tiny] {$\X$};
\end{tikzpicture}%
\end{document}