3

I would like to know how to make code of fractions in article class more easy to handle.

Usually, if I type ${a \over b}$ then LaTeX does not display $a/b$, which is what I want. So I have to say $a/b$ and $${a \over b}$$ separately, which is to me annoying.

Is there something I can do so that whenever I type ${a \over b}$ in article class then LaTeX knows to automatically display $a/b$?

7
  • 1
    So you want ${a \over b}$ to always be ${a/b}$? Without exclusion? Note that you should really use $\frac{a}{b}$ and not \over`...
    – Werner
    Jan 17, 2015 at 5:18
  • @Werner: Yeah, for now I want no exclusion. Thank you so much.
    – Yes
    Jan 17, 2015 at 6:00
  • Have a look at \sfrac{1}{2} from the xfrac package
    – MaxNoe
    Jan 17, 2015 at 8:19
  • 1
    don't use \over in latex! Jan 17, 2015 at 10:16
  • 1
    \over is not a latex command and its syntax is completely different to all latex commands, that is why latex defines \frac. Its syntax makes it very hard to customise the behaviour, for example if a document is marked up using \frac \frac could easily be redefined as in the answers here to use different styles. If a document is marked up with \over then changing the style means editing the document. Also the amsmath package makes \over make a warning to strongly discourage its use. Jan 17, 2015 at 10:25

1 Answer 1

5

Something like this?

I've defined a \myfrac command that outputs a/b when in inline math, \frac{a}{b} for display mode.

Code

\documentclass{article}

\newcommand{\myfrac}[2]{%
    \ifinner#1/#2%
    \else\frac{#1}{#2}%
    \fi%
}

\begin{document}

this is inline mode $\myfrac{a}{b}$

\bigskip

and this is display mode

\[\myfrac{a}{b}\]

\end{document} 

Output

enter image description here

This has,however, the drawback of printing a/b for \scriptstyle and \scriptscriptstyle, too.

If you don't like this behavior, define \myfrac as

\newcommand{\myfrac}[2]{%
  \mathchoice%
  {\frac{#1}{#2}}% \displaystyle
  {#1/#2}%         \textstyle
  {\frac{#1}{#2}}% \scriptstyle
  {\frac{#1}{#2}}% \scriptscriptstyle
}

MWE

\documentclass{article}

\newcommand{\myfrac}[2]{%
  \mathchoice%
  {\frac{#1}{#2}}% \displaystyle
  {#1/#2}%         \textstyle
  {\frac{#1}{#2}}% \scriptstyle
  {\frac{#1}{#2}}% \scriptscriptstyle
}

\begin{document}

this is inline mode $\myfrac{a}{b}$

\bigskip

and this is display mode

\[\myfrac{a}{b}c^{\myfrac{d}{e}}\]

\end{document} 

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.