[Tex/LaTex] algorithm2e, function, procedure, \par, error

algorithm2e

This is fine:

\documentclass{article}
\usepackage[boxed]{algorithm2e}

\begin{document}

\begin{algorithm}
    $x \gets x$\;
    \caption{Identity}
\end{algorithm}

\end{document}

But this, which uses the predefined environment "function" (p. 4 of the manual)

\documentclass{article}
\usepackage[boxed]{algorithm2e}

\begin{document}

\begin{function}
    $x \gets x$\;
    \caption{Identity}
\end{function}

\end{document}

produces this error

Runaway argument?
Identity@\endcsname ##1{\FuncSty {\algocf@captname Identity@(}\ArgSty \ETC.
./sandbox.tex:10: Paragraph ended before \algocf@captname was complete.
<to be read again> 
                   \par 
l.10 

? r
OK, entering \nonstopmode...
./sandbox.tex:10: Missing \endcsname inserted.
<to be read again> 
                   \par 
l.10 

)
! Emergency stop.

I use TexShop which (I believe) compiles with

simpdftex latex --maxpfb --extratexopts "-file-line-error -synctex=1"

Any help would be appreciate, I am out of ideas.

Best Answer

This is explained in page 17 of the documentation (talking about the procedure environment, but the same remark applies to function):

the syntax of the \caption command is restricted as follow: you MUST put a name followed by 2 braces like this Name(). You can put arguments inside the braces and text after. If no argument is given, the braces will be removed in the title.

A complete example:

\documentclass{article}
\usepackage[boxed]{algorithm2e}

\begin{document}

\begin{function}
    $x \gets x$\;
    \caption{Identity()}
\end{function}

\end{document}

enter image description here