복소수 명령을 내렸어요

복소수 명령을 내렸어요

그래서 복소수를 올바르게 렌더링하기 위한 숙제로 명령을 작성했습니다. 예를 들어, 내가 쓸 때 \complfull{5}{-2}출력은 5-2i입니다.

\documentclass{scrartcl}

\usepackage{amsmath}
\usepackage{xstring}    %this package is needed for the if else commands like \IfStrEq

\newcommand{\complfull}[2]{     %This bracket covers the case for when the real part is zero
    \IfStrEq{#1}{0}{\IfStrEqCase{#2}{
        {0}         {0}                 %"If b is also zero, output zero"
        {1}         {i}     %"If b is just one, output 'i'(instead of '1i')"
        {-1}        {-i}}
    [#2\textit{i}]}{\IfStrEqCase{#2}{   %This bracket is the first command's "else" statement, so it covers the case when the real part is not zero
        {0}         {#1}                %"If the imaginary part is zero, output the real part"
        {1}         {#1+i}
        {-1}        {#1-i}}
    [\IfBeginWith{#2}{-}                %This covers the case when the imaginary part is negative and the real part is not zero. It is necessary because we can't have a number be displayed as "1+-4i", and doing it with brackets would necessitate every imaginary part to be written with brackets.
        {#1#2i}
        {#1+#2i}]}
}

\begin{document}
    
    \complfull{2}{-2}
    \complfull{0}{1}

\end{document}

이 코드는 다음과 같은 입력에 대한 오류 메시지를 제공합니다 $\complfull{\dfrac{-1}{12}}{-3}$ . 오류 메시지는 다음과 같습니다.

Undefined control sequence. $\complfull{\dfrac{-1}{12}}{-3}

TeX capacity exceeded, sorry [input stack size=10000]. $\complfull{\dfrac{-1}{12}}{-3}

그러나 내가 뭔가를 할 때 $\displaystyle\complfull{\frac{1}{2}}{-1}$그것은 잘 작동합니다.

문제의 원인은 무엇입니까?

답변1

다른 테스트 방법으로 전환하고 코드를 정리합니다( 수학 모드에서 이미 기울임꼴 \textit{i}로 표시되어 있으므로 여기서 사용할 이유가 없습니다 .i

나는 이것이 더 간단하게 이루어질 수 있다고 확신합니다. 버전은 이제 xstring테스트가 취약하기 때문에 더 이상 아무것도 사용하지 않습니다.

를 사용하면 허수 부분이 로 시작하는지 \NewDocumentCommand{\complfull}{m >{\TrimSpaces} m}{확인하는 #2테스트가 중단되므로 공백으로 시작하지 마십시오 -.

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{etoolbox}

% this assumes the arg never starts with spaces
\def\ProcessFirstChar#1#2\END{%
  \def\FirstChar{#1}
}
\def\DASHCHAR{-}

% input a,b, need to generate z = a+bi in a nice way
\NewDocumentCommand{\complfull}{m >{\TrimSpaces} m}{
  % if a =0
  \ifstrequal{#1}{0}{
      \ifstrequal{#2}{0}{
          0
        }{
          \ifstrequal{#2}{1}{
              i
            }{
              \ifstrequal{#2}{-1}{
                  -i
                }{ 
                  #2i% default
                }
            }
        }
    }{
      % a is not zero
      #1% just leave a
      \ifstrequal{#2}{0}{%
          % Im(z) = 0, so nothing
        }{
          \ifstrequal{#2}{1}{
              + i
            }{
              \ifstrequal{#2}{-1}{
                  -i
                }{
                  % still need the case when b is negative, as we should not add a plus in this case
                  \expandafter\ProcessFirstChar#2\END
                  \ifx\FirstChar\DASHCHAR\else+\fi
                  #2i
                }
              }
            }
          }
        }

\begin{document}


$\complfull{0}{0}$

$\complfull{0}{1}$

$\complfull{0}{-1}$

$\complfull{0}{5}$

$\complfull{1}{0}$

$\complfull{1}{1}$

$\complfull{1}{-1}$

$\complfull{1}{5}$

$\complfull{1}{-5}$

$\complfull{0}{3}$

$\complfull{a}{ - b}$

$\complfull{0}{3}$

$\complfull{\frac{-1}{12}}{-3}$

$\complfull{\dfrac{-1}{12}}{-\dfrac12}$

$\complfull{\dfrac{-1}{12}}{\dfrac12}$

\end{document}

답변2

(OP의 형식 문제를 보다 일반적으로 해결하기 위해 답변을 다시 작성했습니다.)

다음은 LuaLaTeX 기반 답변입니다. 에 대해 어떠한 가정도 하지 않습니다.내용물복소수의 실수 부분과 허수 부분 중 허수 부분은 기호로 시작할 수 있습니다 -. (반대로, 선행 +기호는 허용되지 않습니다. 그러나 필요한 경우 이 제한이 약화될 수 있습니다.)

매크로 \complfull는 텍스트 모드와 수학 모드 모두에서 사용할 수 있습니다. 매크로를 제공하는 패키지가 로드 \complfull{\dfrac{-1}{12}}{-3}되는 한 문제가 없습니다 .amsmath\dfrac

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

% !TEX TS-program = lualatex
\documentclass{scrartcl}
\usepackage{amsmath} % for '\ensuremath' macro
\usepackage{luacode} % for 'luacode' env. and '\luastringN' macro

\begin{luacode}
function complfull ( re , im ) -- inputs: real and imag. parts

  -- begin by stripping off any leading whitespace from 'im'
  im = im:gsub ( '^%s*' , '' ) 
  im = im:gsub ( '^%-%s*' , '-' )

  if im == '0' then -- real number
    return re 
  else 
    if re == '0' then -- imaginary number
      if im == '1' then 
        return 'i'
      elseif im == '-1' then 
        return '-i'
      else 
        return im..'i'
      end
    else -- complex number
      if im == '1' then 
        return re..'+i'
      elseif im == '-1' then 
        return re..'-i'
      else
        if im:sub(1,1)=='-' then 
          return re..im..'i'
        else 
          return re..'+'..im..'i'
        end
      end
    end
  end
end
\end{luacode}

\newcommand\complfull[2]{\ensuremath{\directlua{%
  tex.sprint(complfull(\luastringN{#1},\luastringN{#2}))}}}

\begin{document}
\complfull{1}{0}; 
\complfull{0}{1}, 
\complfull{0}{-1};  
\complfull{1}{1}, 
\complfull{1}{-1}; 
\complfull{\frac{1}{2}}{\frac{1}{2}},
\complfull{\frac{1}{2}}{-\frac{1}{2}};
\complfull{\dfrac{-1}{12}}{\exp(7)},
\complfull{\dfrac{-1}{12}}{-3}.
\end{document}

답변3

귀하의 질문에 대한 답변: 매크로가 인수에 적용되고 에서 처리하는 동안 인수를 확장하기 $\complfull{\dfrac{-1}{12}}{-3}$때문에 오류가 발생합니다 . 그리고 매크로는 . NET에서 작동하지 않는 오래되고 모호한 LaTeX 방법을 사용합니다 .\complull\IfStrEq\IfStrEq\edef\dfrac\protected\def\protect\edef

당신이 해결하고 있는 문제는 흥미롭습니다. OpTeX로 무엇을 할 수 있는지 보여드리겠습니다.

\def\complfull#1#2{%
   \isequal{#1}{0}\iftrue \printimpart\empty{#2}%
                  \else {#1}\printimpart+{#2}%
                  \fi
}
\def\printimpart#1#2{%
   \qcasesof {#2}
   {}      {\ifx#1\empty 0\fi}
   {0}     {\ifx#1\empty 0\fi}
   {1}     {#1\imu}
   {-1}    {-\imu}
   \_finc  {\isminus #2\iftrue \else #1\fi {#2}\imu}%
}
\def\imu{{\rm i}}
\def\isminus #1#2\iftrue{\ifx-#1}

Test:
$\complfull{1}{0}; 
\complfull{0}{};
\complfull{0}{1}; 
\complfull{0}{-1};  
\complfull{1}{1}, 
\complfull{1}{-1}; 
\complfull{1\over2}{1\over2},
\complfull{1\over2}{-{1\over2}};
\complfull{-1\over12}{\exp(7)},$

\bye

매크로 는 완전히 확장 가능하며 매크로 가 처리될 \complfull때 인수를 확장하지 않습니다 .\isequal\qcasesof

관련 정보