![Exemplo](https://rvso.com/image/461896/Exemplo.png)
Se eu definir uma função como \SetKwFunction{Func}{func}
em algoritmo2e, \Func{a, b}
imprime
função(a,b)
por padrão. Como posso imprimir
função[a, b]
como em Wolfram ou
função ab
como em Haskell?
Procurei na documentação do algoritmo2e e não encontrei nada para isso, mas a impossibilidade de alterar isso parece uma limitação estranha com toda a customização disponível no algoritmo2e. Se não for possível, quais são algumas alternativas fáceis com \SetKwFunction
comportamento semelhante e com essa habilidade?
Exemplo
O preâmbulo do documento é este:
\documentclass{article}
\usepackage[vlined]{algorithm2e}
\SetFuncSty{textsf}
\SetKwProg{Fn}{function}{}{}
O primeiro caso é produzido pelo código a seguir e outros casos mostram como deveria ser, mas são produzidos com soluções alternativas.
\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\).
O factorial x - 1
último caso deveria ter parênteses, mas percebi o erro tarde demais e decidi mantê-lo para fins de demonstração.
Responder1
Sugiro alguma cirurgia na embalagem. Os parênteses de abertura e fechamento são programados, então os substituímos por macros. Além disso, um estilo separado é atribuído aos delimitadores.
\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}
Claro que você escolherá o estilo de uma vez por todas.
Responder2
Isto pode ser conseguido alterando SetKwFunction
a implementação da seguinte forma:
\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}