3

in algorithm2e one can define some function names as keywords and algorithm2e then provides a certain font in which they are printed. Now, I want to use the font even outside the algorithm, e.g. when discussing it. This only works sometimes by using \function name.

How do I replicate the function font manually?

\documentclass{report}
\usepackage{algorithm2e}

\begin{document}
    
    \begin{algorithm}
        \SetKwFunction{default}{default}

        \default($ a $).  
    \end{algorithm}
    Here, \default still works, but it not always does.
\end{document}

1 Answer 1

3

Define the new function type in the preamble instead of in the algorithm environment. Since the algorithm environment is a floating object, it will sometimes end up after the text using the command, and since the use of the command precedes its definition, the command will be undefined.

\documentclass{report}
\usepackage{algorithm2e}
\SetKwFunction{default}{default}
\begin{document}
\begin{algorithm}
  \default($ a $).  
\end{algorithm}
\default always works, no matter whether this part is typeset before or after the algorithm.
\end{document}

If you just want to replicate the style of certain elements in the algorithm, it might be easier to use the corresponding style definitions.

enter image description here

\documentclass{article}
\usepackage{algorithm2e}
\advance\textwidth1.5cm
\begin{document}
To use the same style for program elements outside of the
\texttt{algorithm} environment of the package \texttt{algorithm2e},
use the following commands.
\begin{center}
  \begin{tabular}{llll}
    element type & command & \multicolumn{2}{c}{example} \\
    \hline
    keywords & \verb"\KwSty" & \verb"\KwSty{else}" & \KwSty{else} \\
    arguments & \verb"\ArgSty" & \verb"\ArgSty{arg}" & \ArgSty{arg} \\
    function args &  \verb"\FuncArgSty" & \verb"\FuncArgSty{farg}" & \FuncArgSty{farg} \\
    function names &  \verb"\FuncSty" & \verb"\FuncSty{fnc}" & \FuncSty{fnc} \\
    program names &  \verb"\ProgSty" & \verb"\ProgSty{prg}" & \ProgSty{prg} \\
    data &  \verb"\DataSty" & \verb"\DataSty{data}" & \DataSty{data} \\
    comments & \verb"\CommentSty" & \verb"\CommentSty{comment}" & \CommentSty{comment} \\
    titles & \verb"\TitleSty" & \verb"\TitleSty{title}" & \TitleSty{title} \\
    block markers & \verb"\BlockMarkersSty" & \verb"\BlockMarkersSty{newblock}" & \BlockMarkersSty{newblock}
  \end{tabular}
\end{center}
\end{document}
1
  • Always. . Great 😃😃😃
    – Sebastiano
    Commented Nov 24, 2021 at 10:07

You must log in to answer this question.

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