\SetKwFunction{Func}{func}
algorithm2eのように関数を定義すると、次のように\Func{a, b}
出力されます。
関数(a, b)
デフォルトでは印刷されません。どうすれば印刷できますか?
関数[a, b]
Wolframや
関数ab
Haskellのように?
algorithm2e のドキュメントを調べましたが、それについては何も見つかりませんでした。しかし、これを変更できないのは、algorithm2e で利用できるすべてのカスタマイズを考慮すると奇妙な制限のように思えます。それが不可能な場合、\SetKwFunction
同様の動作とその機能を備えた簡単な代替手段は何ですか?
例
この文書の前文は次のとおりです。
\documentclass{article}
\usepackage[vlined]{algorithm2e}
\SetFuncSty{textsf}
\SetKwProg{Fn}{function}{}{}
最初のケースは次のコードによって生成され、他のケースは想定される外観を示していますが、回避策を使用して生成されています。
\begin{algorithm}[H]
\SetKwFunction{Factorial}{factorial}
\Fn{\Factorial{x}}{
\uIf{\(x \le 1\)}{
0
}\Else{
\(x \cdot \Factorial{\(x-1\)}\)
} }
\end{algorithm}
This here \Factorial{x} grows very rapidly with \(x\).
最後のケースfactorial x - 1
には括弧が必要ですが、間違いに気付いたのが遅すぎたため、デモンストレーションのためにそのままにしておくことにしました。
答え1
パッケージに何らかの修正を加えることを提案します。開始括弧と終了括弧はハードワイヤードされているため、マクロに置き換えます。また、区切り文字には別のスタイルが割り当てられます。
\documentclass{article}
\usepackage[vlined]{algorithm2e}
\makeatletter
\renewcommand{\SetKwFunction}[2]{%
\expandafter\gdef\csname @#1\endcsname##1{%
\FuncSty{#2}\FuncDelSty{\FuncOpen}\FuncArgSty{##1}\FuncDelSty{\FuncClose}%
}%
\expandafter\gdef\csname#1\endcsname{%
\@ifnextchar\bgroup{\csname @#1\endcsname}{\FuncSty{#2}\xspace}%
}%
}
\newcommand{\FuncDelSty}[1]{\textnormal{#1}\unskip}
\newcommand{\SetFuncDelSty}[1]{%
\renewcommand{\FuncDelSty}[1]{\textnormal{\csname#1\endcsname{##1}}\unskip}%
}
\providecommand{\gobblearg}[1]{}
\newcommand{\FuncOpen}{(}
\newcommand{\FuncClose}{)}
\newcommand{\matharg}[1]{\ensuremath{#1}}
\SetFuncArgSty{matharg} % function arguments are in math, not in text
\makeatother
\SetFuncSty{textsf}
\SetKwProg{Fn}{function}{}{}
\ExplSyntaxOn
\NewDocumentCommand{\haskellargs}{m}
{
\seq_set_from_clist:Nn \l_tmpa_seq { #1 }
\ensuremath{\;\seq_use:Nn \l_tmpa_seq { \; }}
}
\ExplSyntaxOff
\begin{document}
\section*{Standard}
\begin{algorithm}[H]
\SetKwFunction{Factorial}{factorial}
\Fn{\Factorial{x}}{
\uIf{\(x \le 1\)}{
0
}\Else{
\(x \cdot \Factorial{x-1}\)
} }
\end{algorithm}
\section*{Brackets}
\begin{algorithm}[H]
\SetKwFunction{Factorial}{factorial}
\renewcommand{\FuncOpen}{[}\renewcommand{\FuncClose}{]}
\Fn{\Factorial{x}}{
\uIf{\(x \le 1\)}{
0
}\Else{
\(x \cdot \Factorial{x-1}\)
} }
\end{algorithm}
\section*{Haskell}
\begin{algorithm}[H]
\SetKwFunction{Factorial}{factorial}
\SetFuncArgSty{haskellargs}
\renewcommand{\FuncOpen}{}\renewcommand{\FuncClose}{}
\Fn{\Factorial{x}}{
\uIf{\(x \le 1\)}{
0
}\Else{
\(x \cdot \Factorial{(x-1)}\)
} }
\end{algorithm}
\begin{algorithm}[H]
\SetKwFunction{Test}{test}
\SetFuncArgSty{haskellargs}
\renewcommand{\FuncOpen}{}\renewcommand{\FuncClose}{}
\Fn{\Test{x,y}}{
\uIf{\(x>y\)}{
$x-y$
}\Else{
$y-x$
} }
\end{algorithm}
\end{document}
もちろん、スタイルは一度で決定します。
答え2
これは、実装を次のように変更することで実現できますSetKwFunction
。
\documentclass{article}
\usepackage{algorithm2e}
\makeatletter
\renewcommand{\SetKwFunction}[2]{%
\expandafter\gdef\csname @#1\endcsname##1{\FuncSty{#2[}\FuncArgSty{##1}\FuncSty{]}}%
\expandafter\gdef\csname#1\endcsname{%
\@ifnextchar\bgroup{\csname @#1\endcsname}{\FuncSty{#2}\xspace}}%
}%
\makeatother
\begin{document}
\begin{algorithm}
\SetKwFunction{Func}{func}
\SetKwProg{Fn}{Function}{:}{\KwRet}
\Fn{\Func{$f$, $a$, $b$, $\varepsilon$}}{
a\;
b\;
}
\end{algorithm}
\end{document}