我正在尝试在宏中自动求解一些方程

我正在尝试在宏中自动求解一些方程

我正在尝试编写一个macro可以解决simple linear equation并显示所有步骤的程序,但我在编写时遇到了麻烦macro。我只能在每个部分进行复制和粘贴。这是我目前正在做的事情。但我确信有更好的方法。:

    \documentclass[11pt,fleqn]{examdesign}
\usepackage{savesym}
\usepackage{amsmath}
\usepackage{pifont}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{xspace}
\SectionFont{\large\sffamily}
\usepackage[shortlabels,inline]{enumitem}  %%shortlabels here for matchign
\usepackage{cancel}
\usepackage{tkz-euclide} %%%%%%%%%%%for marking angles
\usetkzobj{all}  %needed because Overleaf uses old version
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,calc,matrix}
\graphicspath{ {./dir1/} }
\usepackage{hyperref}
\Fullpages
\ContinuousNumbering
\ShortKey
%%\NoKey
\DefineAnswerWrapper{}{}
\NumberOfVersions{1}

%%%%%%%%%%%%%%%%%%%%%%%%%%macro to round numbers
\newcommand*{\MyNum}[1]{%
    \pgfmathprintnumber[
        precision=1,
        fixed zerofill=false,
        ]{#1}}%
%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Macro for solvemyslope


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\class{needed for examdesign}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%BEGIN DOCUMENT
%%%%%
\begin{document}

\begin{shortanswer}[title={Solving equations test},rearrange=no,resetcounter=yes]
\begin{question} Consider this question
\begin{answer}
\noindent
\begin{enumerate}
\begin{minipage}[t]{0.33\linewidth}
\vspace{0pt}


    \newcommand{\firstx}{3}
     \newcommand{\bconstant}{4}   
         \newcommand{\firsty}{10}
    \item $mx+b=y$\\ $x=\firstx,b=\bconstant, y=\firsty$
%%%%%below is what I want automated.
         \pgfmathsetmacro{\bminusy}{\firsty-\bconstant}
        \pgfmathsetmacro{\myslope}{(\firsty-\bconstant)/\firstx} 
    \begin{align*}
        m(\firstx)+\bconstant&=\firsty\\
        -\bconstant&=-\bconstant\\
        \firstx m&=\MyNum{\bminusy}\\
        \frac{\cancel{\firstx}m}{\cancel{\firstx}}&=\frac{\MyNum{\bminusy}}{\firstx}\\
    m&=\boxed{\MyNum{\myslope}}
   \end{align*}
    \end{minipage}
\end{enumerate}
\end{answer}
\end{question}
\end{shortanswer}


\end{document}

有人能教我如何编写一个macro可以使用值并让它自动执行上述代码的地方吗?我想象它看起来像这样\solvemyslope{x=3,b=4,y=10},它会写入align环境并填写所有内容。
现在b只有积极的,直到我能弄清楚如何编写这个宏,我猜我正在使用pgfkeys或其他东西,但我不知道如何设置macro。有人帮帮我。
此代码将重复用于零件a, b, c等,但我没有任何问题,因为它们newcommandsminipages。也许macro应该使用提供命令?无论如何,任何帮助都会受到赞赏,一如既往,任何改进我的代码的建议都会受到欢迎。

答案1

像这样?(仅适用于整数系数,我希望它有效)

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{tkz-fct}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand*{\fracReducedTkz}[2]{\tkzReducFrac{#1}{#2}\ensuremath{\ifnum\tkzMathSecondResult=1
    \tkzMathFirstResult
    \else\ifnum\tkzMathSecondResult=-1
    \number\numexpr-1*\tkzMathFirstResult
    \else\ifnum\tkzMathSecondResult<0
    \frac{\number\numexpr-1*\tkzMathFirstResult}{\number\numexpr-1*\tkzMathSecondResult}
    \else
    \frac{\tkzMathFirstResult}{\tkzMathSecondResult}\fi\fi\fi}}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\newcommand{\solvemyslope}[3]{%
    \ifnum\pdfstrcmp{#1}{0}=1
    \begin{align*}
    #1 m+#2&=#3\\
    #1 m&=\number\numexpr #3-#2\\
    \frac{#1 m}{#1}&=\frac{\number\numexpr #3-#2}{#1}\\
    m&=\ifnum\number\numexpr (#3-#2)=0
    0\else\fracReducedTkz{\number\numexpr (#3-#2)}{#1}\fi
    \end{align*}
    \else
    Sorry it is Impossible!!
    \fi
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

    \solvemyslope{2}{2}{2}
    \solvemyslope{0}{2}{2}
    \solvemyslope{5}{2}{-2}


\end{document}

在此处输入图片描述

相关内容