# Parenthesise math expression if it contains operator

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{#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?

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{
\setsepchar{+||-||\bullet||\cdot||\oplus}
\ignoreemptyitems
\ifnum\listlen\xator[]>1\relax(#1)\else{#1}\fi
/
\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} • 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 '19 at 12:06
• @Bubaya It appears to be something with the \sim. I'm looking further... – Steven B. Segletes Feb 28 '19 at 12:30
• @Bubaya I have provided an edit to handle this case. Not sure why the prior code had an issue. – Steven B. Segletes Feb 28 '19 at 12:36
• Indeed this works; do you know why you workaround works by the original solution did not? – Bubaya Feb 28 '19 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. – Steven B. Segletes Feb 28 '19 at 17:17

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*}
\\
\\
\fractionsinline
\\
\inlinefrac{2+x}{3}
\\
\\
\end{gather*}

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

\end{document} • (+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 '19 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 '19 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 '19 at 16:01
• @koleygr Agreed and edited accordingly. – egreg Feb 28 '19 at 16:20
• @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 '19 at 16:35

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

\documentclass{article}
\usepackage{xstring}
\renewcommand\frac{\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} 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.