Ich habe einen komplexen Zahlenbefehl erstellt

Ich habe einen komplexen Zahlenbefehl erstellt

Also habe ich als Hausaufgabe einen Befehl geschrieben, um komplexe Zahlen korrekt darzustellen. Wenn ich beispielsweise schreibe, \complfull{5}{-2}ist die Ausgabe 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}

Dieser Code gibt mir eine Fehlermeldung für Eingaben wie $\complfull{\dfrac{-1}{12}}{-3}$ Dies sind die Fehlermeldungen:

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

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

Wenn ich jedoch so etwas mache, $\displaystyle\complfull{\frac{1}{2}}{-1}$funktioniert es einwandfrei.

Was verursacht das Problem?

Antwort1

Wechseln Sie zu einer anderen Testmethode und bereinigen Sie den Code (ich sehe \textit{i}hier keinen Grund, es zu verwenden, da ies im Mathematikmodus bereits kursiv ist).

Ich bin sicher, dass es noch einfacher geht. Version verwendet jetzt nichts mehr von, xstringda die Tests ziemlich fehleranfällig sind.

Bei der Verwendung von \NewDocumentCommand{\complfull}{m >{\TrimSpaces} m}{ist darauf zu achten, dass #2niemals mit einem Leerzeichen beginnt, da sonst der Test, ob der Imaginärteil mit beginnt, unterbrochen würde -.

\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}

Antwort2

(Ich habe die Antwort umgeschrieben, um das Formatierungsproblem des OP allgemeiner zu lösen.)

Hier ist eine LuaLaTeX-basierte Antwort. Sie macht keine Annahmen über dieInhaltdes Real- und Imaginärteils der komplexen Zahl, außer dass der Imaginärteil mit einem -Vorzeichen beginnen darf. (Ein Vorzeichen ist dagegen +nicht erlaubt; diese Einschränkung könnte jedoch bei Bedarf abgeschwächt werden.)

Das \complfullMakro kann sowohl im Text- als auch im Mathematikmodus verwendet werden. \complfull{\dfrac{-1}{12}}{-3}Dies stellt kein Problem dar – solange das amsmathPaket, das das Makro bereitstellt \dfrac, geladen ist.

Bildbeschreibung hier eingeben

% !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}

Antwort3

Die Antwort auf Ihre Frage: Der $\complfull{\dfrac{-1}{12}}{-3}$erzeugt einen Fehler, weil Ihr Makro auf seine Argumente \complullzugreift und sie während der Verarbeitung um erweitert . Und das Makro ist nicht als definiert . Es verwendet eine alte und obskure LaTeX-Methode, die mit nicht funktioniert .\IfStrEq\IfStrEq\edef\dfrac\protected\def\protect\edef

Das Problem, das Sie lösen, ist interessant. Ich zeige, was wir mit OpTeX machen können:

\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

Das \complfullMakro ist vollständig erweiterbar und erweitert seine Argumente nicht, wenn \isequalMakros \qcasesofverarbeitet werden.

verwandte Informationen