# Label axis in units of pi (or pi/2) and use minor tick num [duplicate]

Using pgfplots, I often want to label the horizontal axis major ticks in units of pi or pi/2, or pi/3. Is it possible to have such "trigonometric major ticks" use minor tick num = <value> to specify the number of minor ticks?

Currently my approach for "trigonometric major ticks" is to make a style wholes that specifies the xtick=<integer multiples of pi> and xticklabels=<labels>. I specify the ticks for a range -4*pi to +4*pi, and crop the graph as needed. I similarly have a style called halves for half-integer multiples of pi:

\documentclass{standalone}

\usepackage{pgfplots} %This package also loads tikz

\pgfplotsset{
% Global Styles
axis lines = middle,
xlabel = $x$,
ylabel = $y$,
no markers,
samples=50,
grid = both,
enlargelimits = false,
axis line style = {line width=0.5pt},
every axis plot/.append style={
line width = 1.25pt,
smooth,
},
% Label every pi
halves/.style={
xtick = {-12.5664, -10.9956, -9.42478, -7.85398, -6.28319, -4.71239, -3.14159, -1.5708, 0, 1.5708, 3.14159, 4.71239, 6.28319, 7.85398, 9.42478, 10.9956, 12.5664, 14.1372, 15.708, 17.2788, 18.8496, 20.4204, 21.9911, 23.5619, 25.1327},
xticklabels = {$-4\pi$,$-\frac{7\pi}{2}$,$-3\pi$,$-\frac{5\pi}{2}$,$-2\pi$,$-\frac{3\pi}{2}$,$-\pi$,$-\frac{\pi}{2}$,$0$,$\frac{\pi}{2}$,$\pi$,$\frac{3\pi}{2}$,$2\pi$,$\frac{5\pi}{2}$,$3\pi$,$\frac{7\pi}{2}$,$4\pi$,$\frac{9\pi}{2}$,$5\pi$,$\frac{11\pi}{2}$,$6\pi$,$\frac{13\pi}{2}$,$7\pi$,$\frac{15\pi}{2}$,$8\pi$}
},
% Label every pi
wholes/.style={
xtick = {-12.5664, -9.42478, -6.28319, -3.14159, 0., 3.14159, 6.28319, 9.42478, 12.5664, 15.708, 18.8496, 21.9911, 25.1327},
xticklabels = {$-4\pi$,$-3\pi$,$-2\pi$,$-\pi$,$0$,$\pi$,$2\pi$,$3\pi$,$4\pi$,$5\pi$,$6\pi$,$7\pi$,$8\pi$}
}
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[wholes,domain=-pi:3*pi,ymin=-2,ymax=+4,minor tick num = 1]
\legend{$1-2\cos(2x)$}
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[halves,domain=-pi:pi,ymin=-2,ymax=+4,minor tick num = 1]
\legend{$2+0.5\sin(4x)$}
\end{axis}
\end{tikzpicture}

\end{document}


with output

Problem: I'm fairly happy with this solution, except minor tick num=1 is not working on the x-axis. I think this is due to rounding errors. Minor grid in pgfplot not showing when specifying ticks manualy

Certainly I could specify the minor ticks manually. I really don't want to do this---I need to create many graphs like this, some times I want minor tick num = 1, sometimes I want minor tick num = 3.

Is there a better approach?

• @StefanPinnow I'm aware of that question (it's essentially the method I'm using now). The existing answers to that question do not allow for use of minor tick num = <value> Feb 9, 2016 at 19:45
• Ok, I've edited my answer ... We will see if this minor change satisfies not beeing a duplicate. Nontheless you have a working solution now which -- I think -- is the most important point, right? ;) Feb 9, 2016 at 20:02
• @Herbert Is this really a duplicate? I am (trying) to ask specifically about a method for generating trigonometric labels that allows for automatic minor ticks using minor tick num Feb 9, 2016 at 20:49

Using my solution given here you can easily add minor tick num to add as many minor ticks as you want.

\documentclass[border=1mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
axis lines=middle,
xlabel=$x$,
ylabel=$y$,
no markers,
samples=50,
every axis plot/.append style={
line width=1.25pt,
smooth,
},
}
% define precision of \pi
% this is set here to the value of \pgfmathpi
\pgfmathsetmacro{\PI}{3.141592654}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=-0.5*pi:1.5*pi,
ymin=-1.5,
ymax=+1.5,
%
% scale x axis values by \pi and
% remove the corresponding label
scaled x ticks={real:\PI},
xtick scale label code/.code={},
% in case you want to set an explicit tick distance
xtick distance=\PI/2,
% add code here for formatting the xlabels'
% I configured exceptions for \pm\pi where no number in front
% of these are shown and for fractional values these should be
% shown as fractions
xticklabel={%
\ifdim \tick pt = 1 pt
\strut$\pi$%
\else\ifdim \tick pt = -1 pt
\strut$-\pi$%
\else
% to avoid some mess with TeX precision, first
% round the \tick value to one digit after the comma
\pgfmathparse{round(10*\tick)/10}
% depending on whether the resulting number is an integer
% show it as integer only, otherwise use the style given
% in xticklabel style'
\pgfmathifisint{\pgfmathresult}{%
\strut$\pgfmathprintnumber[int detect]{\pgfmathresult}\pi$%
}{%
\strut$\pgfmathprintnumber{\pgfmathresult}\pi$%
}
\fi\fi
},
% set number plotting to frac style
xticklabel style={
/pgf/number format/frac,
/pgf/number format/frac whole=false,
},
minor tick num=1,
]

• you might want to add % at the end of the lines to avoid whitespaces Feb 9, 2016 at 20:02
• When using xtick distance=pi/4, I got rounding errors that caused pi to be displayed as 1pi. Moving the \pgfmathparse{round(100*\tick)/100} above the if and switching \tick to \pgfmathresult fixed this. Feb 17, 2016 at 23:05