연산자가 포함된 경우 괄호 수학 표현식

연산자가 포함된 경우 괄호 수학 표현식

양식의 여러 부분을 작성 $\frac{V+W}{U}$하고 나중에 대신 슬래시 부분으로 표시하기로 결정했다고 상상해 보십시오. 내가 원하는 그룹에서는 언제나 할 수 있다 \renewcommand\frac[2]{#1/#2}. 그런데 분자(또는 분모)에 괄호를 붙이고 싶습니다.경우에만. mathop​적어도 이것은 분자(분모)를 괄호 안에 넣을지 여부를 결정하는 데 적합한 첫 번째 경험적 방법인 것 같습니다.

누구든지 이것을 달성하는 방법을 알고 있습니까?

답변1

+여기서는 , -, \bullet및 을 검색 \cdot하지만 \setsepchar목록에 더 많은 연산자를 추가할 수 있습니다.

또한 와 같이 연산자가 선행하는 경우에는 -3괄호를 추가하지 않습니다. 줄에 주석을 달아 이를 변경할 수 있습니다 \ignoreemptyitems.

\sim에 대한 인수로 자체적으로 나타날 때 이상한 오류를 해결하기 위해 편집되었습니다 \inlinefrac.

\documentclass{article}
\usepackage{listofitems}
\newcommand\inlinefrac[2]{
  \setsepchar{+||-||\bullet||\cdot||\oplus}
  \ignoreemptyitems
  \readlist*\xator{ #1}
  \ifnum\listlen\xator[]>1\relax(#1)\else{#1}\fi
  /
  \readlist*\xator{ #2}
  \ifnum\listlen\xator[]>1\relax(#2)\else{#2}\fi
}
\begin{document}
\[
  \frac{2}{3}\quad\let\frac\inlinefrac\frac{2}{3}
\]
\[
  \frac{2}{-3}\quad\let\frac\inlinefrac\frac{2}{-3}
\]
\[
  \frac{2+x}{3}\quad\let\frac\inlinefrac\frac{2+x}{3}
\]
\[
  \frac{2\bullet x}{3\cdot y}\quad\inlinefrac{2\bullet x}{3\cdot y}
\]
\centering$\inlinefrac{M\oplus N}{\sim}$
\end{document}

여기에 이미지 설명을 입력하세요

답변2

나는 \ifrac명령을 제안합니다. 와 함께 제공되면 *을 호출 하지만 (로컬 설정) \inlinefrac에 의해 수행된 설정도 따릅니다 .\fractionsinline

이렇게 하면 를 추가하거나 제거하여 한 양식에서 다른 양식으로 쉽게 전환할 수 있습니다 *. 직접 사용할 수도 있습니다 \inlinefrac.

괄호를 실행하는 기호는 분자와 분모에서 동일할 필요는 없습니다. 곱하기 기호는 분모에서만 괄호를 실행해야 합니다.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\ifrac}{smm}
 {
  \IfBooleanTF { #1 }
   { \inlinefrac { #2 } { #3 } }
   {
    \bool_if:NTF \l_bubaya_frac_inline_bool
     {
      \inlinefrac { #2 } { #3 }
     }
     {
      \frac { #2 } { #3 }
     }
   }
 }

\bool_new:N \l_bubaya_frac_inline_bool

% extend the regex at will; \+ and \- stand for + and -,
% \c{command name} for a control sequence
\regex_const:Nn \c_bubaya_frac_num_regex { \+ | \- | \c{sin} | \c{cos} }
\regex_const:Nn \c_bubaya_frac_den_regex { \c{cdot} | \c{times} | \c{bullet} }

\NewDocumentCommand{\fractionsinline}{}
 {
  \bool_set_true:N \l_bubaya_frac_inline_bool
 }

\NewDocumentCommand{\inlinefrac}{mm}
 {
  \regex_match:NnTF \c_bubaya_frac_num_regex { #1 } { (#1) } { #1 }
  /
  \regex_match:NnTF \c_bubaya_frac_num_regex { #2 }
   {
    (#2)
   }
   {
    \regex_match:NnTF \c_bubaya_frac_den_regex { #2 } { (#2) } { #2 }
   }
 }

\ExplSyntaxOff

\begin{document}
\begin{gather*}
  \ifrac{2}{3}\qquad\ifrac*{2}{3}
\\
  \ifrac{2}{-3}\qquad\ifrac*{2}{-3}
\\
  \fractionsinline
  \ifrac{2}{-3}\qquad\ifrac{2}{-3}
\\
  \inlinefrac{2+x}{3}
\\
  \ifrac{\sin x}{x}\qquad\ifrac*{\sin x}{x}
\\
\end{gather*}

\fractionsinline
\begin{gather*}
  \ifrac{2+x}{3}
\\
  \ifrac{2\bullet x}{3\cdot y}
\\
  \ifrac{\sin x}{x}
\end{gather*}

\end{document}

여기에 이미지 설명을 입력하세요

답변3

@Steven의 솔루션과 동일한 아이디어를 가지고 있지만 패키지를 사용하는 대안입니다 xstring.

\documentclass{article}
\usepackage{xstring}
\renewcommand\frac[2]{\IfSubStr{#1}{+}{(#1)}{\IfSubStr{#1}{-}{(#1)}{#1}}/\IfSubStr{#2}{+}{(#2)}{\IfSubStr{#2}{-}{(#2)}{#2}}}
\begin{document}
\(\frac{5}{10}=0.5\) but \(\frac{5+1}{10}=0.6\) and \(\frac{7}{10+4}=0.5\) but \(\frac{14+1}{6+1.5}=2\)
\end{document}

여기에 이미지 설명을 입력하세요

추신: 나는 Steven의 솔루션이 더 많은 경우에 대해 확장하기 더 쉽다고 제안하지만 그의 답변을 보기 전에 이미 이 코드를 작성하여 게시했습니다.

관련 정보