例子

例子

如果我定義一個函數如\SetKwFunction{Func}{func}Algorithm2e 那樣,\Func{a, b}則列印

函數(a,b)

預設情況下。我怎麼才能讓它列印

函數[a,b]

就像 Wolfram 或

函數 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}

在此輸入影像描述

相關內容