[Tex/LaTex] How to change how pgfplot steps the labels along a given axes

pgfplotstikz-pgf

I have the following MWE

\documentclass{article}
\usepackage{pgfplots}
\pagestyle{empty}
\usepackage{mathtools}
\begin{document}
\begin{tikzpicture}
\begin{axis}
  [
    minor x tick num=1,
    axis y line=center,
    axis x line=middle,
    xlabel=$x$,
    ylabel=$y\mathmbox{{}={}}\frac{1}{3}x-2$
  ]
\addplot
  [
    smooth,
    blue,mark=none,
    domain=-5:10,
    samples=40
  ]
{1/3*x-2};
\end{axis}
\end{tikzpicture}

\end{document}

I have no idea how to get pgfplots to step the labels along the x-axis by 1 unit (instead of 2 as shown) without doing something funky like changing the size or compact or font used.

enter image description here

UPDATE

Though I'm not entirely happy with this. Here's a bit of a work around which gives me the level of control I want:

\def\xmin{-6}
\def\xmax{10}
\def\xinc{3}
\def\xlabels{\xmin}
\multido{\nx=\xmin+\xinc}{\number\numexpr(\xmax-\xmin)/\xinc+1\relax}{
  \ifnum\nx=\xmin\else
    \xdef\xlabels{\xlabels,\nx}
  \fi
}

\def\ymin{-6}
\def\ymax{4}
\def\yinc{2}
\def\ylabels{\ymin}
\multido{\ny=\ymin+\yinc}{\number\numexpr(\ymax-\ymin)/\yinc+1\relax}{
  \ifnum\ny=\ymin\else
    \xdef\ylabels{\ylabels,\ny}
  \fi
}

A short-coming of this is that it will only work with interger values.

So, put into action this looks like:

\documentclass{article}
\usepackage{pgfplots}
\pagestyle{empty}
\usepackage{mathtools}
\usepackage{multido}
\pgfplotsset{compat=1.7}
\begin{document}

\def\xmin{-6}
\def\xmax{10}
\def\xinc{3}
\def\xlabels{\xmin}
\multido{\nx=\xmin+\xinc}{\number\numexpr(\xmax-\xmin)/\xinc+1\relax}{
  \ifnum\nx=\xmin\else
    \xdef\xlabels{\xlabels,\nx}
  \fi
}

\def\ymin{-6}
\def\ymax{4}
\def\yinc{2}
\def\ylabels{\ymin}
\multido{\ny=\ymin+\yinc}{\number\numexpr(\ymax-\ymin)/\yinc+1\relax}{
  \ifnum\ny=\ymin\else
    \xdef\ylabels{\ylabels,\ny}
  \fi
}


\begin{tikzpicture}[]
\begin{axis}
  [
    unit vector ratio=1 1,
    minor x tick num=1,
    axis y line=center,
    axis x line=middle,
    xtick={\xlabels},
    ytick={\ylabels},
    xlabel=$x$,
    ylabel=$y\mathmbox{{}={}}\frac{1}{3}x-2$
  ]
\addplot
  [
    smooth,
    blue,mark=none,
    domain=\xmin:\xmax,
    samples=40
  ]
{1/3*x-2};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

But it seems, given all the power pgfplots has to manipulate so many other aspects of the graph, that there should be a simpler approach that what I've come up with.

Best Answer

The easiest but not automatically approach would be to use

xtick = {-5, ..., 10}

which will use \foreach’s rules for completing ....

If you give xmin and xmax values you can also use them

xmin=-5, xmax=10,
xtick={\pgfkeysvalueof{/pgfplots/xmin},...,\pgfkeysvalueof{/pgfplots/xmax}}

The pgfplots package also provides the options max space between ticks and try min ticks, the pgfplots, 4.14.4 “Tick Fine-Tuning”:

/pgfplots/max space between ticks={<number>} (initially 35)
Con figures the maximum space between adjacent ticks in full points. The sux “pt” has to be omitted and fractional numbers are not supported.

/pgfplots/try min ticks={<number>} (initially 4)
Confi gures a loose lower bound on the number of ticks. It should be considered as a suggestion, not a tight limit. This number will increase the number of ticks if ‘max space between ticks ’ produces too few of them. The total number of ticks may still vary because not all fractional numbers in the axis’ range are valid tick positions.

Setting max space between ticks to 17.5 increments the x ticks by 1 and the y ticks by 0.5. The same can be said for try min ticks = 12 but the y ticks will aready be doubled with a value of 8.

(I also got rid of \mathmbox and replaced it by a standard =, the = just needs to be protected from pgfkeys’ parser, see Equal (=) sign within TikZ label.)

Code

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\newcommand*{\myPlot}[1][]{%
\begin{tikzpicture}\begin{axis}[
    minor x tick num=1, axis y line=center, axis x line=middle,
    xlabel=$x$, ylabel={$y = \frac{1}{3}x-2$}, #1]
\addplot[smooth,blue,mark=none,domain=-5:10,samples=2]{1/3*x-2};
\end{axis}\end{tikzpicture}}
\begin{document}
\myPlot
\myPlot[xtick={-5,...,10}]
\myPlot[xmin=-5, xmax=10,
        xtick={\pgfkeysvalueof{/pgfplots/xmin},...,\pgfkeysvalueof{/pgfplots/xmax}}]
\myPlot[max space between ticks=17.5]
\myPlot[try min ticks=8]
\end{document}