如何创建以 +(加号)或 - (减号)分隔参数作为输入并将它们存储到变量(例如多项式的系数)中的宏?

如何创建以 +(加号)或 - (减号)分隔参数作为输入并将它们存储到变量(例如多项式的系数)中的宏?

如何创建以 +(加号)或 - (减号)分隔参数作为输入并将它们存储到变量(例如多项式的系数)中的宏?

我想定义可以访问和选择由加号+或减号分隔的参数部分的宏-并将它们存储到变量中。

例如,我想要创建类似以下的宏:

\documentclass[oneside,DIV=12]{scrbook}
\usepackage{mathtools, amssymb}
\usepackage{xparse}

\newcommand{\coeffZero}{} % variable corresponding to the coefficient of x^0, i.e., the constant term a_0
\newcommand{\coeffOne}{} % a_1 of a_1 x
\newcommand{\coeffTwo}{} % a_2 of a_2 x^2
\newcommand{\coeffThree}{} % a_3 of a_3 x^3
\newcommand{\coeffFour}{} % a_4 of a_4 x^4
\newcommand{\coeffFive}{} % a_5 of a_5 x^5

\newcommand{\polycoeff}[1]{<...>} % or \NewDocumentCommand{\polycoeff}{ m }{<...>} using xparse
% This macro should take a + or - separated arguments of polynomials as input (ignoring spaces) 
% and will store the coefficients to the corresponding variables defined above

\begin{document}
    \polycoeff{6x^3 + 5x^2 − 7}
    \[a_0 = \coeffZero,\, a_1 = \coeffOne,\, a_2 = \coeffTwo,\, a_3 = \coeffThree\]
\end{document}

应该排版a_0 = -7, a_1 = 0, a_2 = 5, a_3 = 6

我该如何定义这样的宏?

谢谢。

答案1

您可以将数字读取到寄存器\coefnum\expnum使用运行下一个宏\afterassignment

\newcount\coefnum  
\newcount\expnum
\def\polycoeff#1{\polyA#1\relax}
\def\polyA{\afterassignment\polyB \coefnum=}
\def\polyB#1{\ifx#1\relax \sxdef{polyco:0}{\the\coefnum}%
   \else \expandafter\polyC \fi
}
\def\polyC#1{\afterassignment\polyD \expnum=\ifx^#1\else 1 #1\fi}
\def\polyD{\sxdef{polyco:\the\expnum}{\the\coefnum}\polyA}
\def\coeff#1{\ifcsname polyco:#1\endcsname \csname polyco:#1\endcsname
   \else 0\fi
}
\def\sxdef#1{\expandafter\xdef\csname#1\endcsname}


\polycoeff{6x^3 + 5x^2 - 7}

$$
  a_0 = \coeff0, a_1 = \coeff1, a_2 = \coeff2, a_3 = \coeff3.
$$

\bye

答案2

修改wipet的解决方案,解决下面前两个问题:

  1. \polycoeff无法存储和的系数+x^n-x^n因为1 -1它需要输入在每个后面都有一个明确的数字x^n。换句话说,\polycoeff{+1x}\polycoeff{-1x}都有效\polycoeff{+x},而\polycoeff{-x}和都返回错误。(解决了)

  2. \polycoeff不能将常数项的系数存储为,a_0 = 0除非在输入中明确说明。换句话说,在返回错误\polycoeff{2x + 0}时工作。\polycoeff{2x}(解决了)

  3. \polycoeff无法存储有度数的项的系数n > 9(仍未解决)

这是我使用宏\PolyCoeff(包含wipet \polycoeff)的最终解决方案。

在此处输入图片描述

\documentclass[oneside,DIV=12]{scrbook}

\usepackage{scrhack}
\usepackage{mathtools, amsthm, amssymb}
\usepackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math}
    \setmathfont{Latin Modern Math}
\usepackage{xparse}
\usepackage{xpatch}
\usepackage{xstring}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % \ExpandArg (https://tex.stackexchange.com/a/515425)
\ExplSyntaxOn
\cs_new_protected:Nn \__user_expand_arg:n
 {
  \tl_set:No \ProcessedArgument { #1 }
 }
\cs_set_eq:NN \ExpandArg \__user_expand_arg:n
\ExplSyntaxOff

    % \ZapSpaces (https://tex.stackexchange.com/a/652826)
\ExplSyntaxOn
\cs_new_protected:Npn \__user_arg_zap_spaces:n #1
  {
    \tl_set:Nn \ProcessedArgument {#1}
    \tl_remove_all:Nn \ProcessedArgument { ~ }
  }
\cs_set_eq:NN \ZapSpaces \__user_arg_zap_spaces:n
\ExplSyntaxOff

    % \polycoeff{<polynomial>} and \coeff (n < 10) (https://tex.stackexchange.com/a/649027)
\newcount\coefnum  
\newcount\expnum
\def\polycoeff#1{\polyA#1\relax}
\def\polyA{\afterassignment\polyB \coefnum=}
\def\polyB#1{\ifx#1\relax \sxdef{polyco:0}{\the\coefnum}%
    \else \expandafter\polyC \fi
}
\def\polyC#1{\afterassignment\polyD \expnum=\ifx^#1\else 1 #1\fi}
\def\polyD{\sxdef{polyco:\the\expnum}{\the\coefnum}\polyA}
\def\coeff#1{\ifcsname polyco:#1\endcsname \csname polyco:#1\endcsname
    \else 0\fi
}
\def\sxdef#1{\expandafter\xdef\csname#1\endcsname}

    % \PolyCoeff(<polynomial>), \Coeff (n < 10), and \Coefficients
\newcommand{\Polynomial}{}
\NewDocumentCommand{\PolyCoeff}{>{\ZapSpaces} >{\ExpandArg} m}{%
    \renewcommand{\Polynomial}{+#1.}
    % x^5
    \IfSubStr{\Polynomial}{++x^5}{\xpatchcmd{\Polynomial}{++x^5}{1x^5}{}{}}{}
    \IfSubStr{\Polynomial}{+x^5}{\xpatchcmd{\Polynomial}{+x^5}{1x^5}{}{}}{}
    \IfSubStr{\Polynomial}{+-x^5}{\xpatchcmd{\Polynomial}{+-x^5}{-1x^5}{}{}}{}
    \IfSubStr{\Polynomial}{-x^5}{\xpatchcmd{\Polynomial}{-x^5}{-1x^5}{}{}}{}
    \IfSubStr{\Polynomial}{x^5.}{\xpatchcmd{\Polynomial}{x^5.}{1x^5+0}{}{}}{}
    % x^4
    \IfSubStr{\Polynomial}{++x^4}{\xpatchcmd{\Polynomial}{++x^4}{1x^4}{}{}}{}
    \IfSubStr{\Polynomial}{+x^4}{\xpatchcmd{\Polynomial}{+x^4}{+1x^4}{}{}}{}
    \IfSubStr{\Polynomial}{+-x^4}{\xpatchcmd{\Polynomial}{+-x^4}{-1x^4}{}{}}{}
    \IfSubStr{\Polynomial}{-x^4}{\xpatchcmd{\Polynomial}{-x^4}{-1x^4}{}{}}{}
    \IfSubStr{\Polynomial}{x^4.}{\xpatchcmd{\Polynomial}{x^4.}{1x^4+0}{}{}}{}
    % x^3
    \IfSubStr{\Polynomial}{++x^3}{\xpatchcmd{\Polynomial}{++x^3}{1x^3}{}{}}{}
    \IfSubStr{\Polynomial}{+x^3}{\xpatchcmd{\Polynomial}{+x^3}{+1x^3}{}{}}{}
    \IfSubStr{\Polynomial}{+-x^3}{\xpatchcmd{\Polynomial}{+-x^3}{-1x^3}{}{}}{}
    \IfSubStr{\Polynomial}{-x^3}{\xpatchcmd{\Polynomial}{-x^3}{-1x^3}{}{}}{}
    \IfSubStr{\Polynomial}{x^3.}{\xpatchcmd{\Polynomial}{x^3.}{1x^3+0}{}{}}{}
    % x^2
    \IfSubStr{\Polynomial}{++x^2}{\xpatchcmd{\Polynomial}{++x^2}{1x^2}{}{}}{}
    \IfSubStr{\Polynomial}{+x^2}{\xpatchcmd{\Polynomial}{+x^2}{+1x^2}{}{}}{}
    \IfSubStr{\Polynomial}{+-x^2}{\xpatchcmd{\Polynomial}{+-x^2}{-1x^2}{}{}}{}
    \IfSubStr{\Polynomial}{-x^2}{\xpatchcmd{\Polynomial}{-x^2}{-1x^2}{}{}}{}
    \IfSubStr{\Polynomial}{x^2.}{\xpatchcmd{\Polynomial}{x^2.}{1x^2+0}{}{}}{}
    % x^1
    \IfSubStr{\Polynomial}{++x+}{\xpatchcmd{\Polynomial}{++x+}{1x+}{}{}}{}
    \IfSubStr{\Polynomial}{++x-}{\xpatchcmd{\Polynomial}{++x-}{1x-}{}{}}{}
    \IfSubStr{\Polynomial}{++x.}{\xpatchcmd{\Polynomial}{++x.}{1x+0}{}{}}{}
    \IfSubStr{\Polynomial}{+x+}{\xpatchcmd{\Polynomial}{+x+}{+1x+}{}{}}{}
    \IfSubStr{\Polynomial}{+x-}{\xpatchcmd{\Polynomial}{+x-}{+1x-}{}{}}{}
    \IfSubStr{\Polynomial}{+x.}{\xpatchcmd{\Polynomial}{+x.}{+1x+0}{}{}}{}
    \IfSubStr{\Polynomial}{-x+}{\xpatchcmd{\Polynomial}{-x+}{-1x+}{}{}}{}
    \IfSubStr{\Polynomial}{-x-}{\xpatchcmd{\Polynomial}{-x-}{-1x-}{}{}}{}
    \IfSubStr{\Polynomial}{-x.}{\xpatchcmd{\Polynomial}{-x.}{-1x+0}{}{}}{}
    % x^0
    \IfSubStr{\Polynomial}{.}{\xpatchcmd{\Polynomial}{.}{}{}{}}{}
    % store coefficients
    \polycoeff{\Polynomial}
    \renewcommand{\CoeffZero}{\coeff{0}}
    \renewcommand{\CoeffOne}{\coeff{1}}
    \renewcommand{\CoeffTwo}{\coeff{2}}
    \renewcommand{\CoeffThree}{\coeff{3}}
    \renewcommand{\CoeffFour}{\coeff{4}}
    \renewcommand{\CoeffFive}{\coeff{5}}
}

\newcommand{\CoeffZero}{} % variable corresponding to the coefficient of x^0, i.e., the constant term a_0
\newcommand{\CoeffOne}{} % a_1 of a_1 x
\newcommand{\CoeffTwo}{} % a_2 of a_2 x^2
\newcommand{\CoeffThree}{} % a_3 of a_3 x^3
\newcommand{\CoeffFour}{} % a_4 of a_4 x^4
\newcommand{\CoeffFive}{} % a_5 of a_5 x^5

\newcommand{\Coefficients}{a_5 = \CoeffFive,\, a_4 = \CoeffFour,\, a_3 = \CoeffThree,\, a_2 = \CoeffTwo,\, a_1 = \CoeffOne,\, a_0 = \CoeffZero.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}\KOMAoptions{DIV=current}\Large%
    \newcommand{\quintic}{ 5x^5 - 4x^4 + 3x^3 - 2x^2 + x }
    \[f(x) = \quintic.\]
    \PolyCoeff{\quintic}
    \[\Coefficients\]
\end{document}

相关内容