Make procedure name with underscore using `algorithm2e`

algorithm2ecaptions

Using answer given in Procedure name with small caps in algorithm2e shows how to make procedure name all small caps in algorithm2e.

But I want to add _ between the names, as it is long name. Adding \_ in the name gives error.

What other options are there? Here is MWE that works, and below that I show the one that does not work

\documentclass[12pt]{article}
\usepackage[algosection, boxruled]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{amsmath}
\begin{document}

\SetProcNameSty{textsc}
\SetProcArgSty{textsc}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\begin{procedure}
\caption{secondorderseriessolver()}
\Input{ode $y''=f(x,y,y')$}
\Output{solution}

\If{$f(x,y,y')$ analytic at $x=0$}
{
   process\;

   \textbf{return}
}
\end{procedure}
\end{document}

enter image description here

Changing the name of the procedure to \caption{second_order_series_solver()} or \caption{second\_order\_series\_solver()} both fail.

Then I tried the trick given in Can't use underscore in algorithm2e caption? but this produced the following

\usepackage{amsmath}
\begin{document}
\SetProcNameSty{textsc}
\SetProcArgSty{textsc}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}

\begin{procedure}
\caption{(second\_order\_series\_solver)}
\Input{ode $y''=f(x,y,y')$}
\Output{solution}

\If{$f(x,y,y')$ analytic at $x=0$}
{
   process\;

   \textbf{return}
}
\end{procedure}
\end{document}

Which produced this

enter image description here

Which is close to what I want, but not exactly. I wanted it to look like the first but with underscores like the second version. The second version has (..) around it.

How to make name of procedure contain underscore? Btw, having small caps is not important at all for me. I just used it from the example given. I simply want the add _ between the names of the proc itself (not its arguments as given in the second answer below).

TL 2022

Best Answer

For unclear reasons, algorithm2e stores the argument of \caption as a \csname and obviously this fails when \_ is used.

The code is found in \SetKwFunction and \SetKwArray. What this is for is really unclear.

At least you can remove the error by making \_ legal inside \csname.

\documentclass[12pt]{article}
\usepackage[algosection, boxruled]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{amsmath}

\NewCommandCopy{\legacyunderscore}{\_}
\renewcommand{\_}{\ifincsname_\else\legacyunderscore\fi}

\begin{document}

\begin{procedure}
% up to %%% should be either inside procedure or in the preamble
\SetProcNameSty{textsc}
\SetProcArgSty{textsc}
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
%%%
\caption{second\_order\_series\_solver()}
\Input{ode $y''=f(x,y,y')$}
\Output{solution}

\If{$f(x,y,y')$ analytic at $x=0$}
{
   process\;

   \textbf{return}
}
\end{procedure}

\end{document}