[Tex/LaTex] Adding simple labels to axes in tikz

tikz-pgf

So, I have a graph generated by tikz, and I'd like to add labels to the axes, along the lines of "-2, -1, 0, 1, 2," and so on. I've searched for hours and pretty much all I can find is stuff having to do with "{axis}" which is not what I am using. It seems like this should be simple but I can't seem to figure it out. Any help would be much appreciated.

Thanks in advance!

 \documentclass{article}
 \usepackage{tikz}
 \begin{document}
 \begin{tikzpicture}
  \draw[step=.5cm,gray!50,very thin] (-2,-2) grid (2,2);
  \draw[<->,scale=.5,thick] (-4,0) -- (4,0);
  \draw[<->,scale=.5,thick] (0,-4) -- (0,4);
  \draw[<->,scale=.5,domain=-2.4495:2.4495,smooth,very thick,variable=\x,blue] plot ({\x},{-\x*\x+2});
 \end{tikzpicture}
 \end{document}

enter image description here

EDIT: the image for schtandard (see comments below) is as follows.

enter image description here

Best Answer

I recommend you use the TikZ library datavisualization. It has a lot of tools for drawing curves like this, including different styles for axes and mechanisms for generating ticks. Have a look at chapter VI in the TikZ manual.

\documentclass{scrartcl}

\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}

\begin{document}

\begin{tikzpicture}[scale=.7]
    \datavisualization [school book axes, visualize as smooth line,
                        x axis={min value=-4, max value=4},
                        y axis={min value=-4, max value=4},
                        all axes={grid}]
        data [format=function] {
            var x : interval [-2.5:2.5];
            func y = - \value x*\value x + 2;
        };
\end{tikzpicture}

\end{document}

The output using the datavisualization library


If you prefer to do everything by hand, you could use a \foreach loop, for example:

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture} [scale=.7]
    \draw[help lines] (-4 ,-4) grid (4, 4);
    \draw[<->,thick] (-4, 0) -- (4, 0);
    \draw[<->,thick] (0, -4) -- (0, 4);
    \foreach \x in {-3, ..., -1, 1, 2, ..., 3} {
        \draw [thick] (\x, 0) -- ++(0, -.2) ++(0, -.15) node [below, outer sep=0pt, inner sep=0pt] {\small\(\x\)};
        \draw [thick] (0, \x) -- ++(-.2, 0) ++(-.15, 0) node [left, outer sep=0pt, inner sep=0pt] {\small\(\x\)};
    }
    \draw[<->,domain=-2.4495:2.4495,smooth,very thick,variable=\x,blue] plot ({\x},{-\x*\x+2});
\end{tikzpicture}

\end{document}

The output drawing everything by hand

Some things to note:

  • I replaced the scaling of every path by a scaling of the whole picture. I set scale=.7, because the ticks are too crammed with scale=.5 (you could of course use fewer ticks).

  • I replaced gray!50, very thin by help lines, which is a style made just for uses like this. If you wish to have different help lines (e.g. a different color), it is probably better to change this style than to hard code it into the grid command.

  • The use of ... in the \foreach loop is a bit overkill here (1, 2, ..., 3 doesn't do much, after all) and only there to show you how it can be used.