
내 문서에는 형식의 부분이 많이 있습니다 m/n
. 이제 m/n 형식의 모든 분수를 \dfrac{m}{n} 형식으로 변환하고 싶습니다.
변환할 방법을 찾을 수 없습니다. 독특한 방법은 손으로 하는 것입니다.
\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}
답변1
LuaLaTeX 기반 솔루션은 다음과 같습니다. 강제로 pdfLaTeX를 사용해야 한다면 운이 좋지 않을 수도 있습니다. 하지만 이 답변이 다른 독자들에게도 여전히 유용할 수 있기를 바랍니다.
a/b
및 와 같은 중위 분수 표기법의 주요 문제 1/2
는 TeX가 입력 스트림에서 기호의 존재를 알아차릴 기회가 있을 때 /
분자를 형성해야 하는 자료가 이미 사라졌다는 것입니다. 분자를 복구하기 위해 백스페이스를 사용하는 것은 매우 지루한 작업입니다(또는 불가능할 수도 있습니다). 전처리기 기반 접근 방식이 더 유망해 보입니다. 다행히 LuaTeX는 process_input_buffer
전처리에 매우 적합한 콜백을 제공합니다.
다음 예제 코드는 infix에서 dfrac로의 변환을 수행하는 Lua 함수를 제공합니다. 유일한 구문 요구 사항은 분자와 분모의 문자가 알파벳 문자와 아라비아 숫자로만 구성되어야 한다는 것입니다. (괄호는 허용되지 않습니다.) (슬래시) 기호 바로 왼쪽 또는 오른쪽에 공백이 /
허용됩니다.
/
를 로 대체하면 \dfrac
이 Lua 함수는 다음과 같습니다.절대텍스트 모드 자료는 물론이고 인라인 수학 모드 자료에서도 실행되어야 합니다. 그러면 어떤 /
문자에도 혼란을 주지 않을 것입니다. 따라서 코드는 Lua 기능을 활성화 및 비활성화하는 두 개의 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 기능의 범위를 전체 문서로 자동으로 확장하려면 디스플레이 수학 환경에 있는지 확인하고 그러한 경우에만 infix-to-frac 변환을 수행하는 몇 가지 테스트를 수행해야 합니다. 이를 수행하는 방법은 다음 예에 나와 있습니다. 6개의 디스플레이 수학 환경이 자동으로 인식됩니다("기본 LaTeX" \[
및 명령 외에 , , , , 및 \]
) . 이러한 환경의 "별표" 및 "일반" 변형이 모두 처리됩니다.equation
align
alignat
flalign
gather
multline
입력 구문과 관련된 요구 사항은 상당히 가볍습니다. 또는 \[ ... \]
와 같은 열기 및 닫기 문이 단독으로 한 줄에 있는 모든 디스플레이 수학 환경과 마찬가지로 식이 허용됩니다.\begin{equation}
\end{align}
아래 코드는 또한 매크로 \ReplaceOff
및 를 제공하는데, 예를 들어 디스플레이 수학 코드가 포함된 -like 자료 \ReplaceOn
의 경우 "수동으로" 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
기본적으로 유닉스 계열 시스템(linux, macos, cygwin...)에서 사용할 수 있으며 Windows의 경우 다양한 곳에서 사용할 수 있습니다.
명령줄 sed를 원하지 않는 경우 텍스트 편집기에서 동일한 정규식 교체를 수행할 수도 있습니다.