[Tex/LaTex] Modulo calculation in PGFplots tick label

calculationspgfplotstikz-pgf

I am printing a diagram and want to use a_[0…3] as tick labels on the x-axis. Right now I have a[0…7] as labels and look for a way to print a[\tick mod 4] (i.e. a_0, a_1, a_2, a_3, a_0, a_1, a_2, a_3). I tried using the solutions found found here (How do I calculate n modulo 3 in LaTeX?), but they all bring up errors when used in the line xticklabel={$a_{\pgfmathprintnumber[int trunc]{\tick}}$}.

Here is an MWE. Can anyone help?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
   \begin{axis}[
         height=8cm,
         width=14.6cm,
         xtick={0,...,7},
         ytick={0,...,3},
         xticklabel={$a_{\pgfmathprintnumber[int trunc]{\tick}}$},
         yticklabel={$b_{\pgfmathprintnumber[int trunc]{\tick}}$}
   ]

   \addplot coordinates {(0,3) (1,2) (2,1) (3,0)} {};
   \addplot coordinates {(1,3) (2,2) (3,1) (4,0)} {};
   \addplot coordinates {(2,3) (3,2) (4,1) (5,0)} {};
   \addplot coordinates {(3,3) (4,2) (5,1) (6,0)} {};
   \end{axis}
\end{tikzpicture}

\end{document}

Best Answer

You can change one line as follows:

xticklabel={$a_{\pgfmathparse{int(mod(\tick,4))}\pgfmathresult}$},

\pgfmathparse evaluates its argument as a mathematical expression, but does not return a result. It normally handles floating-point numbers, so we use int to truncate the resultant value to an integer. \pgfmathresult prints the result of the most-recent invocation of \pgfmathparse.

Related Question