I found some code from Axis with trigonometric labels in PGFPlots
to use multiples of \pi in trig graphing but it causes an error if I also load \siunitx which I also would like to have.
The errors that are listed include:
xparse function not expandable
Package \siunitx error Invalid numerical input
Package tikz math library error: Unknown function or keyword
\numerror
Can someone help me reconcile the two? I would like the capability the code offers but also keep the functionality of \siunitx. I would like someone to help me fix the error and give a simple explanation of my error.
\documentclass[11pt]{exam}
\usepackage{mathtools}
\usepackage{xcolor}
\usepackage{amssymb}
\usepackage{pifont}
\usepackage{array}
\usepackage{tikz}
\usepackage{xintexpr}
\usetikzlibrary{math}
\usepackage{xfp}
\usepackage[font=scriptsize,hypcap=false]{caption}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.16}
%\usepackage{siunitx} %%%%if this is loaded, it causes an error.
\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, \number, \denom, \absnum;
\numorig = round(\tick*#1/pi);
% Calculate reduced fraction for \numorig/#1
\gcd = gcd(\numorig,#1);
\number = \numorig / \gcd;
\absnum = abs(\number);
\denom = #1 / \gcd;
% Build label text
if \number < 0 then {
let \sign = -;
} else {
let \sign =;
};
if \absnum == 1 then {
let \numpi = \pi;
} else {
let \numpi = \absnum\pi;
};
if \denom == 1 then {
if \number == 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$ };
};
}
},
},
}
\printanswers
\graphicspath{ {./dir1/} }
\usepackage{hyperref}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=14cm,
axis equal image,
domain=-2pi:2pi,
axis lines=center,
enlargelimits={abs=0.4},
no markers,
samples=100,
ytick distance = 1,
x axis in pi=2, % tick distance as fraction of pi
]
\addplot {sin(x)};
\end{axis}
\end{tikzpicture}
\end{document}
</code></pre>
<p>Please help. In summary, explain the error and describe how I can fix it (i.e. keeping <code>\siunitx</code> and the code.</p>
<p>Here is the full copy of the errors that I get using Overleaf:</p>
<blockquote>
<pre><code>Compile Error. Sorry, your LaTeX code couldn't compile for some reason.
Please check the errors below for details, or view the raw log.
test.tex, line 85
Missing \endcsname inserted.
<to be read again>
\xparse function is not expandable
l.85 \end{axis}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
test.tex, line 85
Package siunitx Error: Invalid numerical input '='.
For immediate help type H <return>.
...
l.85 \end{axis}
test.tex, line 85
Extra \endcsname.
\tikz@math@next ...math@parsed@keyword \endcsname
\relax \expandafter \ifx \...
l.85 \end{axis}
I'm ignoring this, since I wasn't doing a \csname.
test.tex, line 85
Extra \endcsname.
\tikz@math@next ...math@parsed@keyword \endcsname
\relax \tikz@math@error {U...
l.85 \end{axis}
I'm ignoring this, since I wasn't doing a \csname.
test.tex, line 85
Package tikz Error: tikz math library: Unknown function or keyword '\num ='.
See the tikz package documentation for explanation.
Type H <return> for immediate help.
...
l.85 \end{axis}
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.
test.tex, line 85
Package tikz Error: tikz math library: Unknown function or keyword '\num ='.
See the tikz package documentation for explanation.
Type H <return> for immediate help.
...
l.85 \end{axis}
(That was another \errmessage.)
test.tex, line 85
Package siunitx Error: Invalid numerical input '='.
For immediate help type H <return>.
...
l.85 \end{axis}
(That makes 100 errors; please try again.)
Here is how much of TeX's memory you used:
37290 strings out of 480906
871318 string characters out of 5908280
1235087 words of memory out of 5000000
52330 multiletter control sequences out of 15000+600000
541835 words of font info for 55 fonts, out of 8000000 for 9000
1141 hyphenation exceptions out of 8191
73i,5n,127p,1576b,2023s stack positions out of 5000i,500n,10000p,200000b,80000s
test.tex
==> Fatal error occurred, no output PDF file produced!
</code></pre>
</blockquote>
\numbbut it still gave errors. – E.Yu Dec 06 '20 at 01:33\numas a variable in your example – daleif Dec 06 '20 at 08:46\numto\number– E.Yu Dec 06 '20 at 14:40\numberis also not a good name. I used\numberXand the example compiles just fine. – daleif Dec 07 '20 at 08:40