
答え1
これは LuaLaTeX ベースのソリューションです。pdfLaTeX を使用せざるを得ない場合は、うまくいかないかもしれません。しかし、この回答が他の読者にとって役立つことを願っています。
a/b
やなどの中置分数表記法の主な問題1/2
は、TeX が入力ストリーム内の記号の存在に気付く頃には/
、分子を形成するはずの要素がすでになくなっていることです。分子を復元するためにバックスペースするのは非常に面倒です (あるいは不可能かもしれません)。プリプロセッサ ベースのアプローチの方が有望に思えます。幸い、LuaTeX はprocess_input_buffer
コールバックを提供しており、これはプリプロセスに非常に適しています。
次のサンプル コードは、インフィックスから dfrac への変換を実行する Lua 関数を提供します。唯一の構文上の要件は、分子と分母の文字がアルファベット文字とアラビア数字のみで構成されていることです (括弧は使用できません)。/
(スラッシュ) 記号のすぐ左または右の空白は許可されます。
/
を に置き換えると\dfrac
、このLua関数は一度もないテキスト モードの素材はもちろん、インライン数式モードの素材でも実行できないと、文字が台無しになります。したがって、このコードでは、Lua 関数を有効または無効にする 2 つの LaTeX マクロ (と/
) も提供されています。中置分数を含む各方程式グループの先頭にディレクティブを挿入し、各グループの末尾にもディレクティブを挿入することをお勧めします。\ReplaceOn
\ReplaceOff
\ReplaceOn
\ReplaceOff
% !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}
補遺: Lua 関数のスコープをドキュメント全体に自動的に拡張するには、display-math 環境であるかどうかを確認するためのテストをいくつか実行し、その場合にのみ infix-to-frac 変換を実行する必要があります。これを行う方法を次の例に示します。6 つの display-math 環境が自動的に認識されます (「基本的な LaTeX」および\[
コマンド\]
に加えて)。equation
、、、、、、、および。これらの環境の「align
星印」と「通常」のalignat
両方のバリアントが処理されます。flalign
gather
multline
入力構文に関する要件はかなり緩やかです。 を含む式は許可されます。また、や\[ ... \]
などの開始ステートメントと終了ステートメントがそれぞれ 1 行ずつあるすべての表示数式環境も許可されます。\begin{equation}
\end{align}
以下のコードでは、マクロとも提供されています。\ReplaceOff
これ\ReplaceOn
は、displaymath コードを含む のようなマテリアルの場合など、Lua 関数の操作を「手動で」一時停止する必要がある場合があるためですverbatim
。
% !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}
答え2
提供されたファイルが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}
それから
sed -e "s@ \([a-zA-Z0-9]\+\)/\([a-zA-Z0-9]\+\) @ \\\\dfrac{\\1}{\\2} @g" f1.tex > f2.tex
生産する
\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
Unix 系システム (Linux、MacOS、Cygwin など) ではデフォルトで使用可能であり、Windows では多くの場所から入手できます。
コマンドライン sed が不要な場合は、同じ正規表現の置換を任意のテキスト エディターで実行することもできます。