我找到了一种使用以下代码简化分数的方法。我想修改它,以便如果结果分数为负数,则将负数显示在分数前面。
\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
我不确定我的数学是否完全正确,但我对你的代码做了三处修改:
\tkzMathSecondResult<0
对于我-
在前面放置 a\frac
并从第一个参数中删除与 -1 的乘法的情况\frac
- 我添加了一个,
\else
用于表示在之前\tkzMathFirstResult<0
带有一个的情况,并将第一个参数乘以 -1(第二个参数保持原样)-
\frac
- 我
\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}
结果: