# Adding simple labels to axes in tikz?

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.

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


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

• Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. A MWE should compile and be as small as possible to demonstrate your problem. it's much easier to help you if we have full working code to start from. – Andrew Mar 27 '18 at 8:34
• Sure, sorry, I think the edit I did just now should work. I'm not using any fancy packages. – Ben W Mar 27 '18 at 8:40
• My opinion is that you should switch to plotting with PGFPlots as fast as possible - it is super easy to use, and produces high quality output. PGFPlots is build on top of TikZ, and is therefor fully compatible with everything else you want to do with TikZ. It is very bad in my opinion that the TikZ manual features a plot as part of the introduction. – hpekristiansen Mar 27 '18 at 8:54
• I appreciate your advice, but I have had trouble understanding how to work with pgfplots. I've been wrestling with this for some time and I was able to make tikz work, as above. But I don't know how to do the same with pgfplots. – Ben W Mar 27 '18 at 9:00

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}


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}


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.

• Perfect! Thank you! I had been trying something like that but couldn't get it to work with my scaling. Look above for an example of the crap output I was getting instead, which I edited in for you to see. I will have to study your code to see where I went wrong. – Ben W Mar 27 '18 at 11:26
• I think you went wrong with the scaling (you scaled everything, but not the tick positions). – schtandard Mar 27 '18 at 11:32

fort such of diagrams is intended the package pgfplots:

 \documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines = middle,
grid,
domain=-2.4495:2.4495,
xmin=-3.5, xmax=3.5,
ymin=-4.5, ymax=4.5,
no marks
]
\addplot +[very thick] {-\x*\x+2};
\end{axis}
\end{tikzpicture}
\end{document}