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?
2 Answers
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 asx/y
,(a)/b
, orc/(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 thegsub
function insideinline_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
:
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for 'luacode' environment
\begin{luacode}
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
end
\end{luacode}
%% LaTeX-side code:
\newcommand\InlineToDispOn{\directlua{%
luatexbase.add_to_callback("process_input_buffer",
inline_to_disp, "InlineToDisp" )}}
\newcommand\InlineToDispOff{\directlua{%
luatexbase.remove_from_callback("process_input_buffer",
"InlineToDisp" )}}
\AtBeginDocument{\InlineToDispOn} %activate by default
\begin{document}
$(x+5)/(y-4) \quad (x) / (y) \quad (2(x+5))/(y-4) \quad 2(x+5)/ (y-4) \quad x/y$
\bigskip
$\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$
\end{document}
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
\documentclass{article}
\begin{document}
Inline math:
Some text $\frac{a}{b}$ more text $a/b$ that's it.
Displaymath:
\[
\frac{a}{b} , a/b
\]
\end{document}
-
2I think OP wants to write $a/b$ in the source, but have TeX interpret that as $\frac{a}{b}$.– TeepeemmNov 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
\renewcommand\frac[2]{(#1)/(#2)}
... But I don't really suggest this... (And it seems longer except if you mean "very tall").\frac
, but with just a slash in the TeX source.