0

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 \num error

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=-2*pi:2*pi,
        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}

```

Please help. In summary, explain the error and describe how I can fix it (i.e. keeping \siunitx and the code.

Here is the full copy of the errors that I get using Overleaf:

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!
8
  • 3
    Num is an siunitx macro as well so using it as a variable is probably not a good idea. Often a good idea to prefix your personal macro names when in dought
    – daleif
    Commented Dec 5, 2020 at 22:42
  • yes. I noticed that too. I changed all the code to \numb but it still gave errors.
    – E.Yu
    Commented Dec 6, 2020 at 1:33
  • Then please cite the full log on this example not just this error, there might be more relevant info in the log
    – daleif
    Commented Dec 6, 2020 at 5:31
  • I'm not sure if I did it correctly but I copied and pasted the errors I saw.
    – E.Yu
    Commented Dec 6, 2020 at 8:10
  • 2
    Also you're still using \num as a variable in your example
    – daleif
    Commented Dec 6, 2020 at 8:46

1 Answer 1

0

The main issue here seems to be using already defined macros as internal variables. TikZ/pgf sometimes have a tendency to make these variables global and thus this creates problem later on.

In this case \num comes from siunitx and is expected to behave in a certain way.

Using \number is not a good idea either as this is an important Plain TeX macro.

For scratch variables like this is is often better to use \myNumA, \myNumB etc to ensure that these are not already in use.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .