TransWikia.com

renewcommand{frac}{mfrac} but in subscript/superscript

TeX - LaTeX Asked on June 3, 2021

Can I renewcommand conditionally if displaystyle, superscript/subscript?

I have:

documentclass[11pt]{article}
usepackage{amsmath}
pagestyle{empty}
usepackage{nccmath}

begin{document}

Equation:
{renewcommand{frac}{mfrac}begin{equation}
   x^{frac{1}{2}}
 +frac{1}{2}
end{equation}}relax

end{document}

enter image description here

I need something like:

begin{equation}
   x^{frac{1}{2}}
 +mfrac{1}{2}
end{equation}

enter image description here

2 Answers

I'm not a fan of mfrac. If you want to use it, do, but use mfrac when you really want it. Whether using mfrac or frac depends on several factors usually involving the formula you want to typeset. For instance, mfrac in front of int would possiblye appear out of place.

Here's a way to cope with the problem and also keep care of not doing wrong things with commands defined via DeclareRobustCommand, for which let is quite dangerous.

documentclass[11pt,twocolumn]{article}
usepackage{amsmath}
usepackage{nccmath}

newififsmallfractions
newcommand{smallfractions}{smallfractionstrue}
newcommand{normalfractions}{smallfractionsfalse}
NewCommandCopy{latexfrac}{frac}

DeclareRobustCommand{frac}[2]{%
  ifsmallfractions
    mathchoice{mfrac{#1}{#2}}%
      {latexfrac{#1}{#2}}%
      {latexfrac{#1}{#2}}%
      {latexfrac{#1}{#2}}%
  else
    latexfrac{#1}{#2}%
  fi
}

begin{document}

Equation:
begin{equation}smallfractions
   x^{frac{1}{2}}
 +frac{1}{2}
end{equation}
Normal equation:
begin{equation}
   x^{frac{1}{2}}
 +frac{1}{2}
end{equation}
Better typesetting:
begin{equation}
   x^{1/2} +tfrac{1}{2}
end{equation}

end{document}

You can declare smallfractions anywhere and it will obey the normal grouping rules.

enter image description here

Correct answer by egreg on June 3, 2021

This retains the original size of frac in scriptstyle and scriptscriptstyle, while using mfrac in text and display styles. Original frac can be recovered using lfrac.

EDITED, per David's suggestion, to account for frac's new-found robustness...using LetLtxMacro instead of a simple let.

documentclass[11pt]{article}
usepackage{amsmath,letltxmacro}
pagestyle{empty}
usepackage{nccmath}
LetLtxMacrolfracfrac
renewcommandfrac[2]{mathchoice
  {mfrac{#1}{#2}}
  {mfrac{#1}{#2}}
  {lfrac{#1}{#2}}
  {lfrac{#1}{#2}}
}
begin{document}

Equation:
begin{equation}
   x^{frac{1}{2}}
 +frac{1}{2} +lfrac{1}{2}
end{equation}
end{document}

enter image description here

Answered by Steven B. Segletes on June 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP