You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LaTeX引入对应包后仍无法使用int、mod、floor等函数的问题

LaTeX中使用int/mod/floor/Mod函数时出现“未定义控制序列”错误

在LaTeX中使用int、mod、floor及Mod函数时,持续触发undefined control sequence error(未定义控制序列)错误。使用环境为Tex Live,编辑器试过Texifier和IntelliJ IDEA的TeXiFy插件。

已引入的包

\usepackage{pgfmath}
\usepackage{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{xparse}
\usepackage{amsmath}
\usepackage{xstring}
\usepackage{tikz}
\usepackage{ifthen}

尝试添加的额外包

\usepackage{pgfmath-xfp}

包加载无报错(若引入不存在的包如foobar会提示文件未找到)。

问题代码

% This command is used to display a new job on the timeline. The job is represented by a trapezium.
% #1 - start date (month/year)
% #2 - end date (month/year)
% #3 - level (used in case you want to change the distance from the timeline)
% #4 - trapezium color
% #5 - label (role and company name)
% #6 - opacity of the trapezium
\newcommand\job[6]{
    \setcounter{jobID}{1}

    \StrBefore{#1}{/}[\startMonth] % month in which the job started
    \StrBehind{#1}{/}[\startYear]  % year in which the job started
    \StrBefore{#2}{/}[\endMonth]   % month in which the job ended
    \StrBehind{#2}{/}[\endYear]    % year in which the job ended

    % The start and end dates are expressed as a fraction of the total number of months between the first and last years.
    \pgfmathsetmacro\startAt{(\startYear-\firstYear)+(1/12*(\startMonth-1))}
    \pgfmathsetmacro\endAt{(\endYear-\firstYear)+(1/12*(\endMonth))}

    \pgfmathsetmacro\trapAngle{80}
    \pgfmathsetmacro\maxTrapHeight{35}

    \pgfmathsetmacro\jobDurationMonths{(\endYear - \startYear) * 12 + (\endMonth - \startMonth)}

    % Define the threshold duration (in months) for the two linear segments.
    \pgfmathsetmacro\thresholdDuration{120}

    % Define the scaling factors for the two linear segments.
    \pgfmathsetmacro\shortDurationScale{0.8}
    \pgfmathsetmacro\longDurationScale{0.2}

    % Calculate the scaling factor using the piecewise function.
    \pgfmathsetmacro\scalingFactor{(\jobDurationMonths <= \thresholdDuration) * (\shortDurationScale - (\jobDurationMonths / \thresholdDuration) * (\shortDurationScale - \longDurationScale)) + (\jobDurationMonths > \thresholdDuration) * \longDurationScale}

    % Calculate the trapezium height based on the trapezium width and the duration scaling factor.
    %\pgfmathsetmacro\trapHeight{max(\trapWidth * \scalingFactor, 5)}
    % We set the trapezium width based on the number of months between the start and end dates of the job.
    % The trapezium is rotated 270 degrees, so the width is the height and vice versa.
    % We use a scaling factor to calculate the height of the trapezium. This factor is calculated using a piecewise
    % function, so that the trapeziums never get too wide. I tried using a logarithmic function, and a decay exponential
    % functions but the result wasn't good enough.
    \pgfmathsetmacro\trapWidth{(((\endYear-\startYear)*12)+(\endMonth-\startMonth)+1)}
    \pgfmathsetmacro\trapHeight{max(\trapWidth * \scalingFactor, 5)}

    % Trapezium origin. The trapezium is rotated 270 degrees, so the origin is at the top left corner.
    \pgfmathsetmacro\trapX{(#3)+(\trapHeight/10)}
    \pgfmathsetmacro\trapY{(\startAt+(\endAt-\startAt)/2))}

    % Trapezium middle point. The trapezium is rotated 270 degrees, so the middle point is at the top right corner.
    \pgfmathsetmacro\middleX{\trapX+(\trapHeight/20)-0.1+0.07}
    \pgfmathsetmacro\middleY{\trapY}

    % Draw the trapezium.
    \draw (\trapX,\trapY) node[trapezium, trapezium angle=\trapAngle, trapezium stretches=true, minimum height={\mmtopt{\trapHeight}}, minimum width={\mmtopt{\trapWidth}}, font=\scriptsize, fill=#4!50, opacity={#6}, rotate=270] {};
    \draw[double, fill] (\middleX,\middleY) circle [radius=2pt];

    % Determines the position of the label based on the trapezium height.

    % Calculate the angle based on trapezium height, jobID, and the maximum trapezium height.
    \pgfmathsetmacro\angleForLabelPlacement{70 - 40 * (\trapHeight / \maxTrapHeight) + 20 * (fpeval(int(\jobID) mod 2))}

    % Calculate the length of the first line based on the trapezium height and the maximum trapezium height.
    \pgfmathsetmacro\lineLength{1.5 - (\trapHeight / \maxTrapHeight)}

    % Calculate the offset for the label based on the trapezium height and the maximum trapezium height.
    \pgfmathsetmacro\labelOffset{0.3 * (\trapHeight / \maxTrapHeight)}

    % Calculate the end of the first line based on the adjusted angle, length, and offset.
    \coordinate (startLine) at (\middleX,\middleY);
    \coordinate (endFirstLine) at ([shift=(\angleForLabelPlacement:\lineLength+\labelOffset)]startLine);

    % Draw the label.
    \pgfmathsetmacro\labelPaddingBottom{2pt} % padding between the label and the line
    \node[anchor=south west, font=\tiny,inner sep=0pt, yshift=\labelPaddingBottom] (label) at (endFirstLine) {#5};

    \coordinate (endSecondLine) at (label.east |- endFirstLine);

    \draw (startLine) -- (endFirstLine);
    \draw (endFirstLine) -- (endSecondLine);

    \stepcounter{jobID}
}

调用示例

\job{12/1995}{9/1997}{0}{NavyBlue}{Software Developer}{0.8}

问题分析与解决方法

错误出现在这一行:

\pgfmathsetmacro\angleForLabelPlacement{70 - 40 * (\trapHeight / \maxTrapHeight) + 20 * (fpeval(int(\jobID) mod 2))}
  1. 计数器值引用错误:\jobID是计数器名称,直接使用不会返回数值,需用\value{jobID}获取其当前值。
  2. 函数环境不匹配:fpeval属于xfp包的命令,虽然加载了pgfmath-xfp,但显式加载xfp包更稳妥;同时int和mod是xfp支持的函数,需在fpeval的上下文正确使用。
  3. 改用pgfmath原生函数:如果想全程使用pgfmath的语法,可替换为pgfmath的int()和mod()函数,无需依赖fpeval。

修正方案一(使用xfp)

  • 首先添加xfp包:
\usepackage{xfp}
  • 修正问题行:
\pgfmathsetmacro\angleForLabelPlacement{70 - 40 * (\trapHeight / \maxTrapHeight) + 20 * (\fpeval{int(\value{jobID}) mod 2})}

修正方案二(使用pgfmath原生函数)

无需依赖fpeval,直接用pgfmath的语法计算取模结果:

% 先计算jobID模2的结果
\pgfmathsetmacro\idModTwo{int(mod(\value{jobID}, 2))}
% 再代入角度计算
\pgfmathsetmacro\angleForLabelPlacement{70 - 40 * (\trapHeight / \maxTrapHeight) + 20 * \idModTwo}

另外,代码中还存在一个小问题:\setcounter{jobID}{1}放在\job命令内部会导致每次调用\job时计数器都重置为1,若需要跨命令累计计数,应将这一行移到命令外部,或者直接去掉(如果jobID是全局计数器)。

内容的提问来源于stack exchange,提问作者noun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:02:06