10
\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} 

The result is

enter image description here

I want it to display -1 and 1 instead of -1.0 or 1.0. How can it be done?

2 Answers 2

15

To get integer results, use the TikZ function int(...) when computing \r for the node text:

\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} 

enter image description here

3
  • I was thinking that there should be an option like this, almost tried this. Thank you very much.
    – Kumarm
    Jul 4, 2019 at 23:14
  • How can I do this with an arbitrary number say 'a'?
    – Kumarm
    Jul 4, 2019 at 23:17
  • What do you mean? 'a' is not a number.
    – siracusa
    Jul 5, 2019 at 0:05
5

Just for completeness. In this case there is no need to evaluate anything since coordinates get parsed automatically, so you could simply do

\foreach \X in {-1, 1} 
               \draw (\X*1.5cm,1pt) -- (\X*1.5cm,-1pt) 
                      node[anchor=north, fill=white,font=\tiny] {$\X$};

Please note also that \x may not be a good loop variable if you ever intend also to make use of 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} 

enter image description here

1
  • @marmot...Thank you...
    – Kumarm
    Jul 5, 2019 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .