The code provided here (where the workaround for gcd() issue isn't needed anymore, at least in pgfplots 1.18) does:
- work if the class used is e.g.
article, - not work if the class used is
beamer.
Indeed, the compilation of the following MCE:
\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
% Load math library, for \tikzmath
\usetikzlibrary{math}
\pgfplotsset{
% Typeset fractions of pi at regular intervals on x axis
x axis in pi/.style={
% Make sure the x axis is in radians
trig format plots=rad,
% Set tick distance from style argument
xtick distance={pi/#1},
% Set label style: calculate reduced fraction of pi
xticklabel={
\tikzmath{
% Calculate this tick's multiple of pi/#1
int \numorig, \gcd, \num, \denom, \absnum;
\numorig = round(\tick*#1/pi);
% Calculate reduced fraction for \numorig/#1
\gcd = gcd(\numorig,#1);
\num = \numorig / \gcd;
\absnum = abs(\num);
\denom = #1 / \gcd;
% Build label text
if \num < 0 then {
let \sign = -;
} else {
let \sign =;
};
if \absnum == 1 then {
let \numpi = \pi;
} else {
let \numpi = \absnum\pi;
};
if \denom == 1 then {
if \num == 0 then {
{ \strut$0$ };
} else {
{ \strut$\sign\numpi$ };
};
} else {
{ \strut$\sign\frac{\numpi}{\denom}$ };
% Other style with all pi symbols same and aligned:
% { \strut$\sign\frac{\absnum}{\denom}\pi$ };
};
}
},
},
}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
x axis in pi=2, % tick distance as fraction of pi
]
\addplot {sin(x)};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
fails with the error:
(./test-beamer.nav)
! Missing \endcsname inserted.
<to be read again>
\protect
l.62 \end{frame}
?
Note that adding the fragile option to the frame environment doesn't help.
Do you understand what's going on?