Using a package with and without an option in latex

package-optionspackages

I need to use a specific package in two different ways, once with an option and another time without that option. More specifically, I want to use algpseudocode package once with [noend] option and once without it for two algorithms in a document. As far as I read on the web, calling a package twice with different options is not possible. I could not find a clear answer to this problem. Any suggestions would be greatly appreciated.

I found a similar question: algorithm2e – override defaults but it uses algorithm2e package while I am using algorithm.

Edit: The package I need to use with and without [noend] option is algpseudocode not algorithm.

Best Answer

You're probably referring to algpseudocode or algorithmic, rather than algorithm.

Package algpseudocode

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

\newcommand{\algorithmwithoutend}{%
  \algtext*{EndWhile}%
  \algtext*{EndFor}%
  \algtext*{EndLoop}%
  \algtext*{EndIf}%
  \algtext*{EndProcedure}%
  \algtext*{EndFunction}%
}


\begin{document}

\begin{algorithm}
\algorithmwithoutend

\caption{Euclid’s algorithm without end tags}\label{euclidwithout}

\begin{algorithmic}[1]
\algorithmwithoutend
\Procedure{Euclid}{$a,b$}\Comment{The gcd of $a$ and $b$}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if $r$ is $0$}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
   \EndWhile\label{euclidendwhilewithout}
   \State \textbf{return} $b$\Comment{The gcd is $b$}
\EndProcedure
\end{algorithmic}

\end{algorithm}

\begin{algorithm}

\caption{Euclid’s algorithm with end tags}\label{euclidwith}

\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The gcd of $a$ and $b$}
\State $r\gets a\bmod b$
\While{$r\not=0$}\Comment{We have the answer if $r$ is $0$}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
   \EndWhile\label{euclidendwhilewith}
   \State \textbf{return} $b$\Comment{The gcd is $b$}
\EndProcedure
\end{algorithmic}

\end{algorithm}

\end{document}

enter image description here

Package algorithmic

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\makeatletter
\newcommand{\algorithmwithoutend}{%
  \renewcommand{\ENDIF}{\end{ALC@if}}%
  \renewcommand{\ENDFOR}{\end{ALC@for}}%
  \renewcommand{\ENDWHILE}{\end{ALC@whl}}%
  \renewcommand{\ENDLOOP}{\end{ALC@loop}}%
}
\makeatother

\begin{document}

\begin{algorithm}

\caption{Example algorithm with end tags}

\begin{algorithmic}
\REQUIRE $n \geq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$ \WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$ \STATE $N \leftarrow N / 2$ \ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$ \STATE $N \leftarrow N - 1$ \ENDIF
\ENDWHILE
\end{algorithmic}

\end{algorithm}

\begin{algorithm}

\caption{Example algorithm without end tags}

\begin{algorithmic}
\algorithmwithoutend
\REQUIRE $n \geq 0$
\ENSURE $y = x^n$
\STATE $y \leftarrow 1$
\STATE $X \leftarrow x$
\STATE $N \leftarrow n$ \WHILE{$N \neq 0$}
\IF{$N$ is even}
\STATE $X \leftarrow X \times X$ \STATE $N \leftarrow N / 2$ \ELSE[$N$ is odd]
\STATE $y \leftarrow y \times X$ \STATE $N \leftarrow N - 1$ \ENDIF
\ENDWHILE
\end{algorithmic}

\end{algorithm}

\end{document}

enter image description here

Related Question