1

This is a follow-up question from Solution (2) of this answer. I wanted to have a : after the algorithm number and before the algorithm caption. To do that, I added : with the solution. It (somehow) works. However, the problem is that this : appears everytime I refer to the algorithm.

See this minimal working example with the corresponding output (highlighted):

\documentclass[11pt]{book}
\usepackage{algorithm}

\makeatletter \renewcommand\thealgorithm{\thechapter.\arabic{algorithm}:} @addtoreset{algorithm}{chapter} \makeatother

\begin{document}

\chapter{chapter1} \begin{algorithm}[H] \centering test \caption{algo} \label{algo1} \end{algorithm}

This where we refer to Algorithm \ref{algo1}. \end{document}

So, I appreciate your help to remove this unwanted : from the place I refer to the algorithm.

hola
  • 4,026
  • 3
  • 35
  • 72

1 Answers1

1

Try with caption package as shown here:

\documentclass[11pt]{book}
\usepackage[ruled,vlined,linesnumbered,algo2e,resetcount,algochapter]{algorithm2e}
\usepackage{algorithm}
\usepackage{caption}
\captionsetup[algorithm]{labelsep=colon}

%Edit: \usepackage{chngcntr} %Comment that out for newer versions of LaTeX \counterwithin{algorithm}{chapter}

\begin{document}

\chapter{chapter1} \begin{algorithm}[H] \centering test \caption{algo} \label{algo1} \end{algorithm}

This is where we refer to Algorithm \ref{algo1}. \end{document}

EDITED: with Bernard's suggestion about counter within chapter modification

koleygr
  • 20,105
  • 2
    The numbering of the algorithms can be done in a simpler way with \counterwithin. – Bernard Jul 19 '20 at 11:23
  • Thanks @Bernard ... You may add a solution like this (I just tried to solve the question problem ... But I will upvote an solution that simplifies the rest of the settings too) – koleygr Jul 19 '20 at 11:26
  • 1
    I don't think it would be a good thing, as, concerning the question of the O.P., my method would be strictly the same as yours. But you may add it to your answer, if you wish (\counterwithin is now part of the basic latex). – Bernard Jul 19 '20 at 11:35
  • @Bernard You may wish to answer to the relevant question here: https://tex.stackexchange.com/questions/124902/algorithm-with-chapter-number/ – hola Jul 19 '20 at 11:48
  • @xxx: excellent idea, as some possibilities have changed since that post date. Thanks for pointing it! – Bernard Jul 19 '20 at 12:03