4

To include code in my document I have following in my preamble:

 \usepackage[Code]{algorithm}
 \usepackage{listings}

and following in the document body to display the actual code:

begin{algorithm}
\caption{Description}
\label{alg:num}
\lstset{language=custom}
\begin{lstlisting}
 my code
\end{lstlisting}
\end{algorithm}

This works fine until there are no spaces in the name of the caption type, i.e. \usepackage[Code]{algorithm} works fine but \usepackage[Code Snippet]{algorithm}does not. I also tried adding the ~ symbol, but it failed too. How to add spaces?

1

You can redefine \ALG@name

\documentclass{article}
 \usepackage{algorithm}
 \usepackage{listings}
 \makeatletter
 \edef\ALG@name{Code snippets}
 \makeatother
 \begin{document}
   \begin{algorithm}
\caption{Description}
\label{alg:num}
%\lstset{language=custom}
\begin{lstlisting}
 my code
\end{lstlisting}
\end{algorithm}
 \end{document}

enter image description here

1

The method is explained in section 4.4 (Customization) of the package manual (pages 15–16):

\floatname{algorithm}{Code snippet}

Full example

\documentclass{article}
\usepackage{algorithm}
\usepackage{listings}
\floatname{algorithm}{Code snippet}

\begin{document}
\begin{algorithm}
\caption{Description}
\label{alg:num}
%\lstset{language=custom}
\begin{lstlisting}
my code
\end{lstlisting}
\end{algorithm}
\end{document}

enter image description here

The method of specifying a package option doesn't work, because LaTeX removes all spaces in options, unless the package uses some key-value interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.