Generalavoid numbering of an if statement in an algorithm

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
NELLLY
Posts: 113
Joined: Thu Nov 26, 2009 2:21 am

avoid numbering of an if statement in an algorithm

Post by NELLLY »

Hello,
Below a code containing an algorithm. I need to avoid numbering the line containing the if statement.

Code: Select all

\documentclass[12pt, a4paper]{report}

%%%%%%%%%%%%%% Debut du Rapport %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{minitoc}
\usepackage{glossaries}
\usepackage{subfigure}
\usepackage[pdftex]{graphicx}       % to insert PostScript figures
\usepackage{rotating}
\usepackage{footnote}       % for sideways tables/figures
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[chapter]{algorithm}
\usepackage{tabularx}
\usepackage{fancybox}
\usepackage{fancyhdr}
\usepackage{geometry}
\usepackage{float}
\usepackage[round]{natbib}

\usepackage{algorithmicx,tikz}
\usepackage{algcompatible}
\usepackage[compatible]{algpseudocode}

\floatstyle{boxed}
\renewcommand{\baselinestretch}{1.5}
\setlength{\evensidemargin}{3.5cm}
\setlength{\textwidth}{6in}
\setlength{\textheight}{8.9in}
\setlength{\topmargin}{-0.6in}
\setlength{\topmargin}{-0.2in}
\setlength{\headsep}{.4in}
\setlength{\footnotesep}{.2in}
\setlength{\parindent}{0.5cm}
%\nofiles

\citeindextrue

\pagestyle{empty}
\pagestyle{fancy}
\cfoot{}
\rfoot{\thepage}
\lhead{}
\renewcommand{\footrulewidth}{0.4pt}
\renewcommand{\headrulewidth}{0pt}
%%%%Moi
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{4}
\setcounter{minitocdepth}{4}

\begin{document}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{%
            \node[shape=circle,draw,inner sep=1pt] (char) {#1};}}
\algrenewcommand{\alglinenumber}[1]{\scriptsize\circled{#1}}% circled line numbers
\begin{algorithm}
\caption{My first algorithm}
\label{alg:algorithm1}
\begin{algorithmic}[1]
\Require
 If there are multiple lines here, line's number will be wrong.
 This error doesn't happen in package\{algorithmic\}
\Ensure
 If I write $\setminus\setminus$ here, line's number will be wrong too.
\State State 1
\State State 2
\Statex State 3
\If{first condition}
\State x=2y+z
\EndIf

\end{algorithmic}
\end{algorithm}


\end{document}
Thanks.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

avoid numbering of an if statement in an algorithm

Post by Stefan Kottwitz »

Statex is intended for skipping numbering. However, with this small hack you could achieve too it if needed:

In the preamble:

Code: Select all

\makeatletter
\newcommand*{\skipnumber}[2][1]{%
  {\renewcommand*{\alglinenumber}[1]{}\State #2}%
  \addtocounter{ALG@line}{-#1}}
\makeatother
Later use it in your code:

Code: Select all

\skipnumber[3]{\If{first condition}
\Statex x=2y+z
\EndIf}
The first argument of the new macro \skipnumber is optional, it is 1 by default. This stands for how many unnumbered lines will follow, for resetting the counter accordingly.

Stefan
LaTeX.org admin
Post Reply