Latex 如何通过新命令定义简单的数学减法

Latex how to define a simple math subtraction by new command

在 Latex 中,我想定义一个简单的数学运算。比如我有很多数1,2,3,4,5,如何减1:

喜欢

\newcommand\myMath[1]%
{%
  \pgfmathparse{#1-1}%
}

您需要 \pgfmathresult 或类似的东西来打印您的计算结果:

\documentclass{article}

\usepackage{tikz}

\newcommand\myMath[1]{%
  \pgfmathparse{#1-1}\pgfmathresult%
}


\begin{document}

\myMath{1}

\myMath{2}

\myMath{3}

\myMath{4}

\end{document}

如果你不想使用重型 TikZ 武器只是为了做一些计算,你可以使用像 xfp:

这样的包
\documentclass{article}

\usepackage{xfp}

\newcommand\myMath[1]{%
  \fpeval{#1-1}%
}


\begin{document}

\myMath{1}

\myMath{2}

\myMath{3}

\myMath{4}

\end{document}

或者如果你的计算不太复杂,你可以让 latex 来做:

\documentclass{article}

\newcommand\myMath[1]{%
  \number\numexpr#1-1\relax%
}


\begin{document}

\myMath{1}

\myMath{2}

\myMath{3}

\myMath{4}

\end{document}