当我使用 tkz-fct 简化分数时,我希望格式化以便负数位于分数前面

当我使用 tkz-fct 简化分数时,我希望格式化以便负数位于分数前面

我找到了一种使用以下代码简化分数的方法。我想修改它,以便如果结果分数为负数,则将负数显示在分数前面。

\documentclass{exam}
   \usepackage{mathtools}%includes amsmath
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \usepackage{tkz-fct}%%must be loaded before tkz
   \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}}%
   
   \usepackage{tikz}
   \begin{document}
   \[\fracReducedTkz{-3}{4}\]
   
   \[\fracReducedTkz{3}{-4}\]
   
   %%This is how I would like to see it displayed
   \[-\frac{3}{4}\]
   \end{document}

答案1

我不确定我的数学是否完全正确,但我对你的代码做了三处修改:

  1. \tkzMathSecondResult<0对于我-在前面放置 a\frac并从第一个参数中删除与 -1 的乘法的情况\frac
  2. 我添加了一个,\else用于表示在之前\tkzMathFirstResult<0带有一个的情况,并将第一个参数乘以 -1(第二个参数保持原样)-\frac
  3. \fi在最后添加了一个额外内容来结束\else引入的额外内容
\documentclass{exam}
   \usepackage{mathtools}%includes amsmath
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \usepackage{tkz-fct}%%must be loaded before tkz
   \newcommand*{\fracReducedTkz}[2]{\tkzReducFrac{#1}{#2}\ensuremath{\ifnum\tkzMathSecondResult=1
       \tkzMathFirstResult
       \else\ifnum\tkzMathSecondResult=-1
       \number\numexpr-1*\tkzMathFirstResult
       \else\ifnum\tkzMathSecondResult<0
       -\frac{\tkzMathFirstResult}{\number\numexpr-1*\tkzMathSecondResult}
       \else\ifnum\tkzMathFirstResult<0
       -\frac{\number\numexpr-1*\tkzMathFirstResult}{\tkzMathSecondResult}
       \else
       \frac{\tkzMathFirstResult}{\tkzMathSecondResult}\fi\fi\fi\fi}}%
   
   \usepackage{tikz}
   \begin{document}
   \[\fracReducedTkz{-3}{4}\]
   
   \[\fracReducedTkz{3}{-4}\]
   
   %%This is how I would like to see it displayed
   \[-\frac{3}{4}\]
   \end{document}

结果:

在此处输入图片描述

相关内容