How to convert all fractions from the form m/n into the form\dfrac{m}{n}?

My document have many fractions of the form m/n. Now I want to convert all fractions from the form m/n into the form \dfrac{m}{n}. I can't find a way to convert. The unique way is by hand.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
$m/n + p/q = 2/3.$
I want
$\dfrac{m}{n} + \dfrac{p}{q} = \dfrac{2}{3}.$
\end{document}

• Is there always exactly one character (letter or numeral) before and after the slash character? Does the slash character occur in text mode too, or only in math mode?
– Mico
Commented Feb 1, 2017 at 4:35
• @Mico Not exactly one. Commented Feb 1, 2017 at 4:56
• @Mico I used Mathematica and Maple. There is always whitespace to set off the elements. Commented Feb 1, 2017 at 5:06
• @Mico I am sory about my question. You can understand simplify all frac m/n into the form \dfrac{m}{n}. Commented Feb 1, 2017 at 5:38
• This is a task for the editor --- find how to do a regular expression replace, and then change  ([a-zA-Z0-9])+/[a-zA-Z0-9])  to  \dfrac{\1}{\2}  --- be careful with the withespaces and quoting, but a serious editor should have a kind of "ask for confirmation" for each one of the matches. Commented Feb 1, 2017 at 11:00

Here's a LuaLaTeX-based solution. If you are forced to use pdfLaTeX you may be out of luck. Hopefully, though, the answer will still be useful to other readers.

A major issue with infix fraction notation, such as a/b and 1/2, is that by the time TeX has a chance to notice the presence of a / symbol in the input stream, the material that's supposed to form the numerator is already gone; backspacing to recover the numerator would be very tedious (or maybe even impossible). A preprocessor-based approach seems more promising. Fortunately, LuaTeX provides the process_input_buffer callback, which is very suitable for pre-processing.

The following example code provides a Lua function that performs the infix-to-dfrac conversion. The only syntactic requirement is that characters in the numerator and denominators consist solely of alphabetic characters and arabic numerals. (No parentheses allowed) Whitespace to the immediate left or right of the / (slash) symbol is permitted.

Given the replacement of / with \dfrac, this Lua function should never be run on inline-math mode material, let alone text-mode material, lest it play havoc with any / characters. The code therefore also provides two LaTeX macros -- \ReplaceOn and \ReplaceOff -- which activate and deactivate the Lua function. I suggest you insert a \ReplaceOn directive at the start of each group of equations that contain infix fractions and a \ReplaceOff directive at the end of each such group.

% !TEX TS-program = lualatex
\documentclass{article}

%% Lua-side code
\usepackage{luacode}
\begin{luacode*}
function sltodf ( s ) -- "slash to dfrac"
return ( string.gsub ( s , "(%w+)%s-/%s-(%w+)" , "\\dfrac{%1}{%2}" ) )
end
\end{luacode*}
%% TeX-side code
"process_input_buffer", sltodf, "sltodf" )}}
\newcommand\ReplaceOff{\directlua{luatexbase.remove_from_callback (
"process_input_buffer", "sltodf" )}}

\usepackage{amsmath} % for '\dfrac' macro

\begin{document}
\ReplaceOn
$m /n + pq / rs = 1 / 2 - 21/ 32 .$  % lots of whitespace

$m/n+pq/rs=1/2-21/32.$ % no whitespace at all

$2a /3b + pq/rs = a / 5 - 61c/41d .$ % mixtures of letters and numerals

\ReplaceOff  % no more infix to frac processing

$m /n + pq/rs = 1 / 2 - 21/32 .$
\end{document}


Addendum: To extend the scope of the Lua function to the entire document automatically, it's necessary to perform some tests to check if we're in a display-math environment, and to perform the infix-to-frac conversion only if that's the case. How to do this is shown in the following example. Six display math environments are recognized automatically (in addition to the "basic LaTeX" $ and $ commands): equation, align, alignat, flalign, gather, and multline. Both the "starred" and "regular" variants of these environments are handled.

The requirements regarding input syntax are fairly mild. Expressions with $...$ are permissible, as are all display math environments in which the opening and closing statements, such as or \end{align}, are on lines by themselves.

The code below also provides the macros \ReplaceOff and \ReplaceOn, as it may be necessary to suspend operation of the Lua function "by hand", say, in case of a verbatim-like material that contains displaymath code.

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for various displayed-equation environments

%% Lua-side code
\usepackage{luacode}
\begin{luacode*}
in_dispmath = false  -- set up a Boolean variable
function sltofr ( s ) -- "slash to frac"
-- if we find a '$...$' line, perform infix fraction replacements
if string.find ( s , "\\%[.*\\%]" ) then
return ( string.gsub ( s , "(%w+)%s-/%s-(%w+)" , "\\frac{%1}{%2}" ) )

-- switch 'in_dispmath' to 'true' if at start of a displaymath env.
elseif string.find ( s , "\\begin{equation%*?}" ) or
string.find ( s , "\\begin{align%*?}" ) or
string.find ( s , "\\begin{alignat%*?}" ) or
string.find ( s , "\\begin{flalign%*?}" ) or
string.find ( s , "\\begin{gather%*?}" ) or
string.find ( s , "\\begin{multline%*?}" ) or
string.find ( s , "\\%[" ) then
in_dispmath = true

-- switch 'in_dispmath' back to 'false' if at end of a displaymath env.
elseif string.find ( s , "\\end{equation%*?}" ) or
string.find ( s , "\\end{align%*?}" ) or
string.find ( s , "\\end{alignat%*?}" ) or
string.find ( s , "\\end{flalign%*?}" ) or
string.find ( s , "\\end{gather%*?}" ) or
string.find ( s , "\\end{multline%*?}" ) or
string.find ( s , "\\%]" ) then
in_dispmath = false

-- if in displaymath mode, replace infix fractions with \frac expressions
elseif in_dispmath == true then
return ( string.gsub ( s , "(%w+)%s-/%s-(%w+)" , "\\frac{%1}{%2}" ) )
end
end
\end{luacode*}

%% TeX-side code
% Assign the Lua function to the 'process_input_buffer' callback
"process_input_buffer", sltofr, "sltofr" )}}

% Macros to enable and disable the Lua function "by hand";
% this may be needed if the document contains verbatim sections
%   that feature displaymath-mode code
"process_input_buffer", sltofr, "sltofr" )}}
\newcommand\ReplaceOff{\directlua{luatexbase.remove_from_callback (
"process_input_buffer", "sltofr" )}}

\begin{document}
$2m /3n + pq / rs = 1 / 2 - 33/ 55 .$

$2m/3n+pq/rs=1/2-33/55.$

$1/2+1/2=1$$1/3+1/3+1/3=1$
\begin{align*}
1/2+1/2         &=1\\
1/3+1/3 + 1 / 3 &= 1
\end{align*}

When not in display math mode, no processing: and/or'', $a/b$, $1/2$

\ReplaceOff  % get ready for some verbatim material
\begin{verbatim}
\begin{align*}
1/2+1/2         &=1\\
1/3+1/3 + 1 / 3 &= 1
\end{align*}
\end{verbatim}

\ReplaceOn % back to 'normal' (replacement) mode
\begin{align*}
1/2+1/2         &=1\\
1/3+1/3 + 1 / 3 &= 1
\end{align*}
\end{document}

• Can I get the text file? Commented Feb 14 at 16:29
• @minhthien_2016 - Sorry, I don't understand your question. Which text file?
– Mico
Commented Feb 14 at 17:28
• I am sorry, text file like David Carlisle' answer. Commented Feb 16 at 7:01

If the supplied file is saved as f.tex

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
$m/n + p/q = 2/3 .$
I want
$\dfrac{m}{n} + \dfrac{p}{q} = \dfrac{2}{3}.$
\end{document}


then

sed -e "s@ $$[a-zA-Z0-9]\+$$/$$[a-zA-Z0-9]\+$$ @ \\\\dfrac{\\1}{\\2} @g"  f1.tex  > f2.tex


will produce

 \documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\begin{document}
$\dfrac{m}{n} + \dfrac{p}{q} = \dfrac{2}{3} .$
I want
$\dfrac{m}{n} + \dfrac{p}{q} = \dfrac{2}{3}.$
\end{document}


sed is available by default in unix-like systems (linux, macos, cygwin...) and available from lots of places for windows.

The same regular expression replace could also be done in any text editor if the commandline sed is not wanted.

• Where I put the line sed -e "s@ $$[a-zA-Z0-9]\+$$/$$[a-zA-Z0-9]\+$$ @ \\\\dfrac{\\1}{\\2} @g" f1.tex > f2.tex ? Commented Feb 5, 2018 at 0:38
• @toandhsp that is not a line to put into a file it is a command to type to the commandline to the editor sed. If you don't have sed or prefer to use a graphical editor select regular expression replace and replace ([a-zA-Z0-9]+)/([a-zA-Z0-9]+) by \\dfrac{\1}{\2}  which is what the above specifies Commented Feb 5, 2018 at 7:50