[Tex/LaTex] How to convert all fractions from the form m/n into the form\dfrac{m}{n}

amsmathfractions

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}. enter image description here
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}

Best Answer

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.

enter image description here

% !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
\newcommand\ReplaceOn{\directlua{luatexbase.add_to_callback (
    "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 \begin{equation} 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.

enter image description here

% !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 
\AtBeginDocument{\directlua{luatexbase.add_to_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
\newcommand\ReplaceOn{\directlua{luatexbase.add_to_callback (
    "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}