[Tex/LaTex] Numbering in algorithmicx

algorithmicxalgorithmsnumbering

  1. I have simple code:

    \documentclass{article}
    
    \usepackage{algorithm} 
    \usepackage{algpseudocode}
    
    \begin{document}
    
    \begin{algorithm}
      \caption{Examples}\label{alg:Examples}
      \begin{algorithmic}[1]
      \State $X=45$
      \State $X=45$
      \State $X=45$
      \State $X=45$
      \State $X=45$
    
      \end{algorithmic}
    \end{algorithm}        
    
    \end{document}
    

    enter image description here

    How to put a point after the number of the algorithm?

  2. I use chapters in report:

    \documentclass[a4paper,12pt]{report}
    
    \usepackage{algorithm} 
    \usepackage{algpseudocode}
    
    \begin{document}
    
    \chapter{Chapter 1}
    
    \begin{equation}
    X_2=4
    \end{equation}
    
    \begin{algorithm}
      \caption{Examples}\label{alg:Examples}
      \begin{algorithmic}[1]
      \State $X=45$
      \State $X=45$
      \State $X=45$
      \State $X=45$
      \State $X=45$
    
      \end{algorithmic}
    \end{algorithm}
    
    \end{document}
    

    enter image description here

    How to change the numbering to take into account the chapters?

Best Answer

It seems like the end goal here is to have a <chapter>.<algorithm>. numbering within the algorithm caption, yet have <chapter>.<algorithm> numbering for the reference. The caption package can help here with setting labelsep to period:

enter image description here

\documentclass{report}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmicx}
\usepackage{caption}% http://ctan.org/pkg/caption
\captionsetup[ruled]{labelsep=period}
\makeatletter
\@addtoreset{algorithm}{chapter}% algorithm counter resets every chapter
\makeatother
\renewcommand{\thealgorithm}{\thechapter.\arabic{algorithm}}% Algorithm # is <chapter>.<algorithm>
\begin{document}
\chapter{A chapter}
\begin{algorithm}
  \caption{Examples}\label{alg:Examples}
  \begin{algorithmic}[1]
  \State $X=45$
  \State $X=45$
  \State $X=45$
  \State $X=45$
  \State $X=45$
  \end{algorithmic}
\end{algorithm}
See Algorithm~\ref{alg:Examples}.
\end{document}

The reason for not just redefining \thealgorithm to including the trailing period is that it would otherwise then be included when referencing an algorithm, which may cause problems. Algorithm number resetting is scheduled with every chapter.

Related Question