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$?

Yes
  • 1,639
  • 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 '15 at 05:18
  • @Werner: Yeah, for now I want no exclusion. Thank you so much. – Yes Jan 17 '15 at 06:00
  • Have a look at \sfrac{1}{2} from the xfrac package – MaxNoe Jan 17 '15 at 08:19
  • 1
    don't use \over in latex! – David Carlisle Jan 17 '15 at 10:16
  • @DavidCarlisle: Pardon me, may I know why? What are the possible consequences? – Yes Jan 17 '15 at 10:21
  • 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. – David Carlisle Jan 17 '15 at 10:25
  • @Chou - The LaTeX macro \frac is a carefully designed "wrapper" around the "primitive" TeX directive \over. See the posting What is the difference between \over and \frac? for more information on this subject, incl. why there's no good reason for using \over directly in a LaTeX document. – Mico Jan 17 '15 at 12:44

1 Answers1

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

karlkoeller
  • 124,410