使用 pgfplots 和自定义代码加载 siunitx 包时出错 我包含了一些似乎与 `\siunitx` 不兼容的代码。我该如何修复它?

使用 pgfplots 和自定义代码加载 siunitx 包时出错 我包含了一些似乎与 `\siunitx` 不兼容的代码。我该如何修复它?

我发现了一些代码PGFPlots 中带有三角标签的轴

在三角图中使用倍数,但如果我还加载我想要的,\pi就会导致错误。\siunitx

列出的错误包括:

xparse 函数无法扩展

包 \siunitx 错误无效的数字输入

软件包 tikz 数学库错误:未知函数或关键字\num 错误

有人能帮我协调两者吗?我想要代码提供的功能,但也要保留 的功能\siunitx。我希望有人能帮我修复错误并简单解释一下我的错误。

\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}

```

请帮忙。总之,解释错误并描述如何修复它(即保留\siunitx和代码)。

以下是使用 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!

答案1

这里的主要问题似乎是使用已定义的宏作为内部变量。TikZ/pgf 有时倾向于将这些变量全球的因此这会在以后产生问题。

在这种情况下,\num来自siunitx并有望以某种方式行事。

使用\number也不是一个好主意,因为这是一个重要的 Plain TeX 宏。

对于此类临时变量,通常最好使用\myNumA\myNumB方法来确保这些变量尚未被使用。

相关内容