如何使以下工作产生输出
5 x V^3 + 7 x V + 9
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\mypower}{3, 2, 1, 0}
\newcommand{\mycoeff}{5, 0, 7, 9}
$
\foreach \c/\p in \mycoeff/\mypower{
\c \times V^{\p}
}
$
\end{document}
答案1
在这里,我使用listofitems
来完成任务。我还构建了额外的逻辑来处理负系数,排除具有零系数的项,并正确呈现 的幂V^0
(作为无\times 1
乘数)和V^1
(作为V
)。
\documentclass{article}
\usepackage{listofitems}
\newcommand\mypoly{%
\foreachitem \z \in \mycoeff[]{%
\ifnum\zcnt=1\else\ifnum\z>0+\fi\fi
\ifnum\z=0 \else\z
\ifnum\mypower[\zcnt]=0\else\times V
\ifnum\mypower[\zcnt]=1\else^{\mypower[\zcnt]}%
\fi\fi\fi
}%
}
\begin{document}
\readlist*\mypower{3, 2, 1, 0}
\readlist*\mycoeff{5, 0, -7, 9}
$\mypoly$
\readlist*\mycoeff{5, 2, 7, 9}
$\mypoly$
\readlist*\mycoeff{5, 2, 7, 0}
$\mypoly$
\end{document}