因此,我編寫了一個命令作為家庭作業,以正確呈現複數。就像,當我寫的時候\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