5

Imagine I have written several fractions of the form $\frac{V+W}{U}$ and decide later that I want them to be displayed as a slashed fraction instead. In the group where I want this to happen, I can always do \renewcommand\frac[2]{#1/#2}. However, I would like to add parenthesis to the numerator (or the denominator) only if it contains a mathop. At least, this seems to me a suitable first heuristic for deciding whether the numerator (denominator) is to be parenthesised or not.

Does anyone know how to achieve this?

3 Answers 3

4

Here, I search for +, -, \bullet, and \cdot, but you can add more operators to the \setsepchar list.

Also, if the operator is leading, such as -3, I do not add parens. You could change that by commenting the line \ignoreemptyitems.

EDITED to workaround a strange error when \sim appears by itself as an argument to \inlinefrac.

\documentclass{article}
\usepackage{listofitems}
\newcommand\inlinefrac[2]{
  \setsepchar{+||-||\bullet||\cdot||\oplus}
  \ignoreemptyitems
  \readlist*\xator{ #1}
  \ifnum\listlen\xator[]>1\relax(#1)\else{#1}\fi
  /
  \readlist*\xator{ #2}
  \ifnum\listlen\xator[]>1\relax(#2)\else{#2}\fi
}
\begin{document}
\[
  \frac{2}{3}\quad\let\frac\inlinefrac\frac{2}{3}
\]
\[
  \frac{2}{-3}\quad\let\frac\inlinefrac\frac{2}{-3}
\]
\[
  \frac{2+x}{3}\quad\let\frac\inlinefrac\frac{2+x}{3}
\]
\[
  \frac{2\bullet x}{3\cdot y}\quad\inlinefrac{2\bullet x}{3\cdot y}
\]
\centering$\inlinefrac{M\oplus N}{\sim}$
\end{document}

enter image description here

6
  • This looks very good to me, in particular because it is clear what is happening. However, the document won't compile if it contains e.g. $\ifrac{M\oplus N}{\sim}$. Can you see why?
    – Bubaya
    Feb 28, 2019 at 12:06
  • @Bubaya It appears to be something with the \sim. I'm looking further... Feb 28, 2019 at 12:30
  • @Bubaya I have provided an edit to handle this case. Not sure why the prior code had an issue. Feb 28, 2019 at 12:36
  • Indeed this works; do you know why you workaround works by the original solution did not?
    – Bubaya
    Feb 28, 2019 at 16:38
  • @Bubaya For some reason, the \readlist{\sim} provokes an error, while adding a prespace \readlist{ \sim} does not. So, I just used instead \readlist* which strips leading and trailing spaces from the list items. I am conferring with my co-author of listofitems to seek out a better understanding. Feb 28, 2019 at 17:17
3

I propose an \ifrac command. If given with * it will call \inlinefrac, but it will also obey the setting done by \fractionsinline (local setting).

This way it's easy to switch from one form to the other, by adding or removing *. You can also use \inlinefrac directly.

Symbols that trigger the parentheses need not be the same in the numerator and the denominator: multiplication symbols should trigger parentheses in the denominator only.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\ifrac}{smm}
 {
  \IfBooleanTF { #1 }
   { \inlinefrac { #2 } { #3 } }
   {
    \bool_if:NTF \l_bubaya_frac_inline_bool
     {
      \inlinefrac { #2 } { #3 }
     }
     {
      \frac { #2 } { #3 }
     }
   }
 }

\bool_new:N \l_bubaya_frac_inline_bool

% extend the regex at will; \+ and \- stand for + and -,
% \c{command name} for a control sequence
\regex_const:Nn \c_bubaya_frac_num_regex { \+ | \- | \c{sin} | \c{cos} }
\regex_const:Nn \c_bubaya_frac_den_regex { \c{cdot} | \c{times} | \c{bullet} }

\NewDocumentCommand{\fractionsinline}{}
 {
  \bool_set_true:N \l_bubaya_frac_inline_bool
 }

\NewDocumentCommand{\inlinefrac}{mm}
 {
  \regex_match:NnTF \c_bubaya_frac_num_regex { #1 } { (#1) } { #1 }
  /
  \regex_match:NnTF \c_bubaya_frac_num_regex { #2 }
   {
    (#2)
   }
   {
    \regex_match:NnTF \c_bubaya_frac_den_regex { #2 } { (#2) } { #2 }
   }
 }

\ExplSyntaxOff

\begin{document}
\begin{gather*}
  \ifrac{2}{3}\qquad\ifrac*{2}{3}
\\
  \ifrac{2}{-3}\qquad\ifrac*{2}{-3}
\\
  \fractionsinline
  \ifrac{2}{-3}\qquad\ifrac{2}{-3}
\\
  \inlinefrac{2+x}{3}
\\
  \ifrac{\sin x}{x}\qquad\ifrac*{\sin x}{x}
\\
\end{gather*}

\fractionsinline
\begin{gather*}
  \ifrac{2+x}{3}
\\
  \ifrac{2\bullet x}{3\cdot y}
\\
  \ifrac{\sin x}{x}
\end{gather*}

\end{document}

enter image description here

7
  • (+1)... Commenting to tell you that you made the same mistake with me: In a first "math thought" you don't really need parentheses for multiplication, but only for addition/transaction. In a second thought... it is really needed because : "\ifrac{2\bullet x}{3\cdot y}" can be read only like: "2\bullet x / (3\cdot y)" or "2\bullet x/ 3/ y" and in no case like 2\bullet x/ 3\cdot y....
    – koleygr
    Feb 28, 2019 at 15:52
  • @koleygr I leave the choice to add what's needed to the OP. I'd simply type my fractions with the slash or in \frac notation, without resorting to automatic (and fallible) methods.
    – egreg
    Feb 28, 2019 at 15:55
  • Right, but I still think that the denominator needs its own checks and has to be in parentheses in more cases than the numerator has... And this makes the question somehow more difficult in order of real checks needed for an automated accepted result.
    – koleygr
    Feb 28, 2019 at 16:01
  • @koleygr Agreed and edited accordingly.
    – egreg
    Feb 28, 2019 at 16:20
  • 1
    @koleygr That's a different cup of tea and unrelated to the problem at hand. In my opinion \sin3x doesn't need parentheses, but that's just opinion: multiplication by an explicit number binds more than other multiplications, so there is no danger of confusion.
    – egreg
    Feb 28, 2019 at 16:35
1

An alternative with the same idea as @Steven's solution but by using xstring package.

\documentclass{article}
\usepackage{xstring}
\renewcommand\frac[2]{\IfSubStr{#1}{+}{(#1)}{\IfSubStr{#1}{-}{(#1)}{#1}}/\IfSubStr{#2}{+}{(#2)}{\IfSubStr{#2}{-}{(#2)}{#2}}}
\begin{document}
\(\frac{5}{10}=0.5\) but \(\frac{5+1}{10}=0.6\) and \(\frac{7}{10+4}=0.5\) but \(\frac{14+1}{6+1.5}=2\)
\end{document}

enter image description here

PS: I suggest Steven's solution as easier to expanded for more cases, but had already created this code before see his answer and thus, posted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .