![Пример](https://rvso.com/image/461896/%D0%9F%D1%80%D0%B8%D0%BC%D0%B5%D1%80.png)
Если я определю функцию как \SetKwFunction{Func}{func}
в algorithm2e, \Func{a, b}
выведет
функция(а, б)
по умолчанию. Как мне заставить его печатать
функция[a, b]
как в Вольфраме или
функция ab
как в Хаскелле?
Я посмотрел документацию 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}