そこで宿題として複素数を正しく表示するためのコマンドを書きました。例えば、次のように書くと\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}$
ため、 はエラーを生成します。また、マクロは として定義されていません。 では機能しない古くてわかりにくい 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