1

while writing my thesis I became aware of a problem related to the algorithm2e package regarding the label referencing of subsubsections following an algorithm. Here is a simple example:

\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} 
\usepackage[utf8]{inputenc}
\usepackage[linesnumbered,ruled,commentsnumbered]{algorithm2e}
\usepackage[hidelinks]{hyperref} 
\begin{document}
\section{Test}\label{section:test}
This is a test. 
\subsection{Test Subsection}\label{subsection:test}
We ref \ref{subsection:test}
Now follows in Alg. \ref{alg:testalgo}:
\newline 
\SetAlFnt{\small\sffamily}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}
\begin{algorithm}[H]
\caption{test}  
\label{alg:testalgo}

\DontPrintSemicolon
\SetNoFillComment
i = 1\;
i = i + 1\;
i = 2\;
\end{algorithm}
\subsubsection{Subsubsection Test}\label{subsubsection}
Lets ref the subsubsection \ref{subsubsection}
\end{document}

Example As you can see, instead of the chapter number, the last linenumber of the algorithm is displayed. If I add more lines, this number changes too. If you remove the algorithm, the cross referencing works without problems, see here:

\documentclass[headsepline,footsepline,footinclude=false,oneside,fontsize=11pt,paper=a4,listof=totoc,bibliography=totoc]{scrbook} 
\usepackage[utf8]{inputenc}
\usepackage[linesnumbered,ruled,commentsnumbered]{algorithm2e}
\usepackage[hidelinks]{hyperref} 
\begin{document}
\section{Test}\label{section:test}
This is a test. 
\subsection{Test Subsection}\label{subsection:test}
We ref \ref{subsection:test}
\subsubsection{Subsubsection Test}\label{subsubsection}
Lets ref the subsubsection \ref{subsubsection}
\end{document}

Example2

Does anyone have a solution or fix for this?

EDIT: If an algoritm is followed by a chapter, a section, or a subsection, this does not happen in the above example. Only with a subsubsection that follows the algorithm

1 Answer 1

1

Since subsubsections are not numbered, you cannot really refer to them with \ref. It's only a bit surprising that the label survives \end{algorithm}, but that's all.

Without the algorithm, the label would refer to the last numbered title, so 0.1.1. Nothing meaningful either.

If you want to number also subsubsections, add

\setcounter{secnumdepth}{\subsubsectionnumdepth}

to your document preamble.

It would be

\setcounter{secnumdepth}{3}

with other document classes.

Full example, with some changes that I invite you to analyze.

\documentclass[
  headsepline,
  footsepline,
  footinclude=false,
  oneside,
  fontsize=11pt,
  paper=a4,
  listof=totoc,
  bibliography=totoc
]{scrbook} 

\usepackage[linesnumbered,ruled,commentsnumbered]{algorithm2e}

\usepackage[hidelinks]{hyperref} 

\setcounter{secnumdepth}{\subsubsectionnumdepth}

\begin{document}

\chapter{Test}

\section{Test}\label{section:test}

This is a test. 

\subsection{Test Subsection}\label{subsection:test}

We ref \ref{subsection:test}
Now follows in Alg. \ref{alg:testalgo}:

\begin{algorithm}[H]
\SetAlFnt{\small\sffamily}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

\caption{test}  
\label{alg:testalgo}

\DontPrintSemicolon
\SetNoFillComment
i = 1\;
i = i + 1\;
i = 2\;
\end{algorithm}

\subsubsection{Subsubsection Test}\label{subsubsection}

Lets ref the subsubsection \ref{subsubsection}

\end{document}

enter image description here

You must log in to answer this question.

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