Using $\frac{x + 5}{y - 4}$ can make equations very long and difficult to read. I'd prefer to be able to write $(x + 5)/(y - 4)$ and have it interpreted as $\frac{x + 5}{y - 4}$. How could I write a command that does this?

  • 1
    Welcome to TeX.SX! \renewcommand\frac[2]{(#1)/(#2)} ... But I don't really suggest this... (And it seems longer except if you mean "very tall").
    – koleygr
    Nov 10, 2019 at 9:46
  • 5
    @koleygr: I think OP is asking for the opposite of that: the usual output of \frac, but with just a slash in the TeX source. Nov 10, 2019 at 10:35

2 Answers 2


Here's a LuaLaTeX-based solution. It consists of two LaTeX macros, called \InlineToDispOn and \InlineToDispOff, and a Lua function called inline_to_disp. When activated via \InlineToDispOn, the Lua function scans all lines of input and replaces all instances of (...)/(...) with \frac{...}{...}. (The parentheses that encase the numerator and denominator terms are stripped off.)

Some additional comments:

  • Use the macro \InlineToDispOff to deactivate the Lua function. This may be needed if your document has passages which much be reproduced verbatim.

  • Nested matching parentheses in the numerator or denominator are ok, but only the outermost ones are processed. Hence, (2(x+5))/(y-4) will be transformed to \frac{2(x+5)}{y-4}.

  • Whitespace to the left and right of the / division symbol is allowed.

  • Per your write-up, the replacement operation requires both the numerator and denominator to be encased in pairs of matching parentheses.

    • By design, the Lua function inline_to_disp does not operate on expressions such as x/y, (a)/b, or c/(d).

    • If you have an expression of the form (x+5)/(y-4) but do not want it to be changed to \frac{x+5}{y-4}, change it to, say, (x+5){}/(y-4). The empty math group, {}, will assure that the gsub function inside inline_to_disp does not perform a successful match.

  • Any number of instances of (...)/(...) may occur on a given input line. (See below for examples.)

  • No input sanity checking of any kind is performed.

    • If the numerator and denominator terms are not syntactically valid from a LaTeX point of view and/or if the parentheses that encase the numerator and denominator terms are not matched, Latex will generate various mysterious warning and error messages.

    • The Lua function also doesn't check whether (...)/(...) occurs in (display or inline) math mode; if it occurs in text mode, Latex will crash with a comment that, you guessed it, \frac is not allowed to occur in text mode. This is, I trust, not much of a constraint since you're looking, in part, to replace various existing \frac expressions.

In the following screenshot, the upper row is typeset in \textstyle, while the lower one is typeset in \displaystyle:

enter image description here

% !TEX TS-program = lualatex
\usepackage{luacode} % for 'luacode' environment
function inline_to_disp ( s ) 
  s = s:gsub ( "(%b())%s-/%s-(%b())" , function (u,v)
               u = u:sub(2,-2)  -- strip off the parentheses
               v = v:sub(2,-2)
               return "\\frac{"..u.."}{"..v.."}" 
           end )
  return s

%% LaTeX-side code:
  inline_to_disp, "InlineToDisp" )}}
  "InlineToDisp" )}}
\AtBeginDocument{\InlineToDispOn} %activate by default

$(x+5)/(y-4) \quad (x) / (y) \quad (2(x+5))/(y-4) \quad 2(x+5)/ (y-4) \quad x/y$

$\displaystyle % same as above, but now with \displaystyle instead of \textstyle
 (x+5)/(y-4) \quad (x) / (y) \quad (2(x+5))/(y-4) \quad 2(x+5)/ (y-4) \quad x/y$

I don't really understand the OP's problem. The OP wants to substitute $\frac{a}{b}$ with ${a}/{b}$ because the\frac` macro is "difficut to read" (for the author, not the reader). Why not just use the forward slash?

% fracprob2.tex  SE 515788 Forward slash for fractions



Inline math:
Some text $\frac{a}{b}$ more text $a/b$ that's it.

\frac{a}{b} , a/b 

  • 2
    I think OP wants to write $a/b$ in the source, but have TeX interpret that as $\frac{a}{b}$.
    – Teepeemm
    Nov 11, 2019 at 21:07
  • @Teepeemm Maybe, but in general the forward slash rather than \frac gives a typographically better result. Nov 13, 2019 at 19:12

You must log in to answer this question.

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