2

I want to add the word "Algorithm" in the LOA. Using this approach: Algorithm in LOA
adds the term to the LOA. However, because I want to further structure my LOA (acc. to "Parts") I aim at also adding the Part- number as headlines in the TOC/LOF/LOA. However, using this code also writes "Algorithm" in front of the "Part-number"

The following MWE illustrates the issue:

\documentclass{scrreprt}
\usepackage{algorithm}
\usepackage{algorithmic}

% Adding "Algorithm" in LOA

\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \renewcommand{\numberline}{Algorithm~\oldnumberline}%
  \oldlistofalgorithms%
  \endgroup}
\begin{document}

% Adding headlines in LOA
\newcommand{\addtocentrylistof}[3]{%
  \ifstr{#2}{}{%
   \addcontentsline{lof}{#1}{#3}%
   \addcontentsline{lot}{#1}{#3}%
    \addcontentsline{loa}{#1}{#3}%
  }{%
    \addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
    \addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
   \addcontentsline{loa}{#1}{\protect\numberline{#2}#3}%
  }%
}

\renewcommand*{\addparttocentry}[2]{%
 \addtocentrydefault{part}{#1}{#2}%
 \addtocentrylistof{part}{#1}{#2}%
}


\listofalgorithms
\part{A}

% Outline
\begin{algorithm}[!hbt]

  \centering
  \caption{Optimization}\label{alg:pso}
  \begin{algorithmic}[1]
    \STATE \textit{GenerateInitialPopulation}(pop)
    \FOR {particle $ \leftarrow $ 1 \textit{to} numParticles}
      \STATE \textit{Evaluate}(particle)
    \ENDFOR
  \end{algorithmic}
\end{algorithm}

\end{document}

What I want: Just the Part-number (here: "I) and then the part-Title (here: "A") as headline above all algorithms that are also called "Algorithm 1..."

1 Answers1

2

One way to achieve what you're after is to intercept the printing of \numberline in \l@part - the macro responsible for printing a \part-like entry in a ToC-related file:

enter image description here

\documentclass{scrreprt}
\usepackage{algorithm,etoolbox}

% Adding "Algorithm" in LOA

\makeatletter
\newcommand{\partnumberline}[1]{Part~#1\quad\ignorespaces}
\patchcmd{\l@part}% <cmd>
  {\leavevmode}% <search>
  {\leavevmode\let\numberline\partnumberline}% <replace>
  {}{}% <success><failure>
\let\oldlistofalgorithms\listofalgorithms
\renewcommand{\listofalgorithms}{%
  \begingroup%
  \let\oldnumberline\numberline%
  \renewcommand{\numberline}{Algorithm~\oldnumberline}%
  \oldlistofalgorithms%
  \endgroup}
\makeatother
\begin{document}

% Adding headlines in LOA
\newcommand{\addtocentrylistof}[3]{%
  \ifstr{#2}{}{%
   \addcontentsline{lof}{#1}{#3}%
   \addcontentsline{lot}{#1}{#3}%
    \addcontentsline{loa}{#1}{#3}%
  }{%
    \addcontentsline{lof}{#1}{\protect\numberline{#2}#3}%
    \addcontentsline{lot}{#1}{\protect\numberline{#2}#3}%
   \addcontentsline{loa}{#1}{\protect\numberline{#2}#3}%
  }%
}

\renewcommand*{\addparttocentry}[2]{%
 \addtocentrydefault{part}{#1}{#2}%
 \addtocentrylistof{part}{#1}{#2}%
}


\listofalgorithms
\part{A}

% Outline
\begin{algorithm}[!hbt]
  \centering
  \caption{Optimization}
\end{algorithm}

\end{document}

The above patch (thanks to etoolbox) replaces \numberline in \l@part with \partnumberline, and we defined this to just print Part~#1\quad.

Werner
  • 603,163