[Tex/LaTex] Amplifying fractions

formattingfractionsmacrosmath-mode

I want to make myself a math portofolio (as a quick reference) and I have a part where I have to 'amplify' a fraction like here:

But I don't know how can I do this thing, can you help me? Thank you.

Edit: The "4)" in the upper left-hand corner means "amplify", the process of expanding both numerator and denominator by explicitly stating the value indicated as a multiplication factor. Similarly, a "(4" in the upper right-hand corner means "simplify", or merge the separate values in the numerator and denominator to a single value.

Best Answer

You should define a set of custom markup commands (see [1], [2]) for those fraction types, where you employ the xcolor package and the \prescript command from mathtools to do the needed formatting:

\documentclass{article}
\usepackage{mathtools,xcolor}

\newcommand{\coloredfrac}[3][red]{%
  \frac{\color{#1}#2}{\color{#1}#3}
}

\newcommand{\ampfrac}[3]{%
  \prescript{#1}{}{\frac{#2}{#3}}
}

\newcommand{\simpfrac}[3]{%
  \frac{#2}{#3}^{#1}
}

\begin{document}
\[ \ampfrac{4)}{1}{2}=\coloredfrac{1\cdot 4}{2\cdot 4}=\simpfrac{(4}{4}{8} \]
\end{document}

output


Regarding your request for a more sophisticated coloring macro that is able to color the numerator and the denominator indipendantly you could define:

\newcommand\coloredfrac{\kernel@ifnextchar[{\coloredfrac@}{\coloredfrac@[red]}}
\def\coloredfrac@[#1]{\kernel@ifnextchar[{\coloredfrac@@[#1]}{\coloredfrac@@[#1][#1]}}
\def\coloredfrac@@[#1][#2]#3#4{%
  \frac{\color{#1}#3}{\color{#2}#4}
}

\coloredfrac can now be called

  • without optional arguments to get a colored fraction in the default color, which is red in this example
  • with one optional argument that sets the color for both, the numerator and the denominator
  • with two optional arguments; the first one for the color of the numerator and the second one for the color of the denominator

    \documentclass{article}
    \usepackage{mathtools,xcolor}
    
    \makeatletter
    \newcommand\coloredfrac{\kernel@ifnextchar[{\coloredfrac@}{\coloredfrac@[red]}}% default value: red
    \def\coloredfrac@[#1]{\kernel@ifnextchar[{\coloredfrac@@[#1]}{\coloredfrac@@[#1][#1]}}
    \def\coloredfrac@@[#1][#2]#3#4{%
      \frac{\color{#1}#3}{\color{#2}#4}
    }
    \makeatother
    
    %\newcommand{\ampfrac}[3]{%
    %  \prescript{#1}{}{\frac{#2}{#3}}
    %}
    
    %\newcommand{\simpfrac}[3]{%
    %  \frac{#2}{#3}^{#1}
    %}
    
    \begin{document}
    \[ \coloredfrac{1}{2}=\coloredfrac[green]{2}{4}=\coloredfrac[green][blue]{4}{8} \]
    \end{document}
    

output_add