我做了一个复数命令

我做了一个复数命令

因此,我编写了一个命令作为家庭作业,以正确呈现复数。例如,当我写入\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}$生成错误,因为您的\complull宏应用于\IfStrEq其参数并\IfStrEq在处理过程中对其进行扩展\edef。 并且\dfrac宏未定义为\protected\def。 它使用一种\protect不适用于的旧且晦涩的 LaTeX 方法\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

相关内容