我的序言中的某些内容破坏了这个 tikz 新命令,但仅在 foreach 内部使用时才会发生?

我的序言中的某些内容破坏了这个 tikz 新命令,但仅在 foreach 内部使用时才会发生?

为了节省编译时间,我在独立文档中使用它来修改 Tikz 图片,然后再在主文档中进行编译,我使用了以下表达式

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,intersections,arrows.meta}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{pgfmath}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\tikzset{declare function={randomfixed(\a,\b) = int(random(0,int(\b-\a))+\a);}}
\newcommand\randomint[2]{\bgroup%
  \pgfmathsetmacro\myval{randomfixed(#1,#2)}%
  \myval\egroup%
}

\foreach \i in {0,0.5,1,1.5,2}
  \draw (\i, 0) -- (\i, -1.5);
  
\foreach \i in {0,0.5,1,1.5}
  \draw (0,-\i) -- (2, -\i);

\foreach \i in {0.25, 0.75, 1.25, 1.75}
  \foreach \j in {-0.25, -0.75, -1.25}{
    \node[align=center] at (\i, \j) {\randomint{-1}{1}};
    }
    
\node[align=center] at (1, -2) {$A$};
\node[align=center] at (2.5, -2) {$*$};
\node[align=center] at (3.5, -2) {$B$};
\node[align=center] at (5.75, -2) {$A*B$};
\foreach \i in {3,3.5,4}
  \draw (\i, -0.25) -- (\i, -1.25);
\foreach \i in {-0.25, -0.75, -1.25}
  \draw (3,\i) -- (4, \i);
\foreach \i in {3.25,3.75}
  \foreach \j in {-0.5, -1}
    \node[align=center] at (\i, \j) {\randomint{-1}{1}};

\foreach \i in {3,4}
  \foreach \j in {-0.25, -1.25}
    \draw[blue] (\i, \j) -- (\i-2.5,\j-0.25);
  
\draw [fill=blue, opacity=0.2, draw=blue] (0.5,-0.5) rectangle (1.5,-1.5);

\foreach \i in {5, 5.5, 6, 6.5}
  \draw (\i, -0.5) -- (\i, -1.5);
\foreach \i in {-0.5, -1, -1.5}
  \draw (5, \i) -- (6.5, \i);

\draw[red] (3, -0.25) -- (5.5,-1);
\draw[red] (3, -1.25) -- (5.5,-1.5);
\draw[red] (4, -0.25) -- (6,-1);
\draw[red] (4, -1.25) -- (6,-1.5);

\draw [fill=red, opacity=0.2, draw=red] (5.5,-1) rectangle (6,-1.5);

\node[align=center] at (5.25, -0.75) {-1};
\node[align=center] at (5.75, -0.75) {1};
\node[align=center] at (6.25, -0.75) {-2};
\node[align=center] at (5.25, -1.25) {-2};
\node[align=center] at (5.75, -1.25) {0};
\node[align=center] at (6.25, -1.25) {1};

\end{tikzpicture}
\end{document}

成功按照我想要的方式定义我的图像。但是,当我尝试在主文档中运行相同的代码时,我遇到了以下错误消息:

\pgfmath@expression 定义中的参数数量非法。

\pgfmath@bgroup@strip@last 定义中的参数数量非法。

\pgfmath@next 定义中的参数数量非法。

\pgfmath@bgroup@stripped 定义中的参数数量非法。

\pgfmath@token 定义中的参数数量非法。

包 PGF 数学错误:未知运算符##' or##1'(在 'randomfixed(##1,##2)' 中)。

\pgfmath@bgroup@strip@last 定义中的参数数量非法。

==> 发生致命错误,未生成输出 PDF 文件!

然而,我自然感到困惑,因为上面的例子中,这段代码编译没有问题。因此,我推断我的序言有问题,并确认发现,在具有以下序言的文档中,代码再次编译没有问题

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,arrows.meta}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usepackage{pgfmath}
\pgfplotsset{compat=1.18}
\begin{document}
.....

当我使用主文档中的序言时,问题再次出现:

\documentclass[11pt, openany]{book}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\graphicspath{ {images/} }
\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm,bindingoffset=6mm]{geometry}
\usepackage{amsmath, amsthm, amsfonts}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE,RO]{\itshape \nouppercase \rightmark}
\fancyhead[LO,RE]{\itshape \nouppercase Chapter \arabic{chapter}}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{positioning}
\usetikzlibrary{datavisualization}
\usetikzlibrary{calc,intersections,arrows.meta}
\usepackage{pgfplots}
\usepackage{pgfmath}
\pgfplotsset{compat=1.18}
\usepackage{mathtools}
\usepackage{tcolorbox}
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz/]
\usepackage{comment}

\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\usepackage{xcolor}
\definecolor{lightgreen}{rgb}{0.56, 0.93, 0.56}
\definecolor{darkseagreen}{rgb}{0.56, 0.74, 0.56}
\definecolor{indianred}{rgb}{0.8, 0.36, 0.36}
\definecolor{airforceblue}{rgb}{0.36, 0.54, 0.66}
\definecolor{LightGray}{gray}{0.9}
\definecolor{amber}{rgb}{1.0, 0.75, 0.0}
\usepackage[sorting=none]{biblatex}
\bibliography{refs}
\usepackage{listings}
\usepackage{minted}
\usepackage{physics}
\usepackage{makecell}
\usepackage{array}
\usepackage{tabularx}
\usepackage{enumitem}
\usepackage{booktabs}



\makeatletter

% define a macro \Autoref to allow multiple references to be passed to \autoref
\newcommand\Autoref[1]{\@first@ref#1,@}
\def\@throw@dot#1.#2@{#1}% discard everything after the dot
\def\@set@refname#1{%    % set \@refname to autoefname+s using \getrefbykeydefault
    \edef\@tmp{\getrefbykeydefault{#1}{anchor}{}}%
    \xdef\@tmp{\expandafter\@throw@dot\@tmp.@}%
    \ltx@IfUndefined{\@tmp autorefnameplural}%
         {\def\@refname{\@nameuse{\@tmp autorefname}s}}%
         {\def\@refname{\@nameuse{\@tmp autorefnameplural}}}%
}
\def\@first@ref#1,#2{%
  \ifx#2@\autoref{#1}\let\@nextref\@gobble% only one ref, revert to normal \autoref
  \else%
    \@set@refname{#1}%  set \@refname to autoref name
    \@refname~\ref{#1}% add autoefname and first reference
    \let\@nextref\@next@ref% push processing to \@next@ref
  \fi%
  \@nextref#2%
}
\def\@next@ref#1,#2{%
   \ifx#2@ and~\ref{#1}\let\@nextref\@gobble% at end: print and+\ref and stop
   \else, \ref{#1}% print  ,+\ref and continue
   \fi%
   \@nextref#2%
}

\makeatother

\usepackage[nottoc,numbib]{tocbibind}

因此显然问题出在这个序言中的某些内容,最后如果在主文档中的 \foreach 循环之外调用 \randomint 函数,则不会发生错误和问题。

如果有人能帮助我诊断问题的根源,那将会非常有帮助。

谢谢

相关内容