4

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

2
  • I guess the issue is the longer underscore? I can't replicate that issue with the code you provide (output).
    – Werner
    Mar 27 at 19:31
  • @Werner I think I did not explain myself well. Yes, I get same output as show in the link you gave. But I want the name of the procedure to have _ and not the names inside (....). i.e. I want same output as the first example shows, but with underscores. Your solution gives that but it also adds (...) to the name as you can see.
    – Nasser
    Mar 27 at 19:35

1 Answer 1

5

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .