예

\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}

여기에 이미지 설명을 입력하세요

관련 정보