# writing an algorithm using algorithm2e with the Comjnl.cls file of The Computer Journal?

How to write an algorithm in Latex using the Comjnl.cls file of The Computer Journal?

The comjnl.cls defines algorithm environment such(line 347):

\newtheorem{algorithm}{Algorithm}


This clashes with algorithm2e environment. Minimal (not) Working Example:

\documentclass{comjnl}

\usepackage{amsmath}
\usepackage{algorithm2e}

\begin{document}

\begin{algorithm}
\SetAlgoLined
\KwResult{Write here the result }
initialization
\While{While condition}{
instructions
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}


Thanks!

• Hi and welcome to TeX.SX. The most popular packages for algorithms are algorithm2e and algorithmic (see overleaf.com/learn/latex/algorithms). Have you tried any of those with that class? Was there a problem? Could you recreate the problem in a Minimal Working Example (MWE) and show us? Right now your question requires a bit more focus :) Nov 18 '20 at 7:07
• I am using the algorithmm2e and write the command \ begin {algorithm} ... \ end {algorithm} "latex error command algorithm already defined. or name end ... illegal see p.192 of the manual" appears when compiling. If you delete the command % \ newtheorem {algorithm} {Algorithm} in the comjnl.cls file, and error will not occur. So is there any other way? Because doing so will fail to transfer the source file to the journal Nov 18 '20 at 7:46
• I took the liberty of editing the question to make it clearer. This way we are more likely to get an answer from the community Nov 18 '20 at 8:55

You can remove the algorithm environment before loading algorithm2e:

\documentclass{comjnl}

\makeatletter
\let\algorithm\@undefined
\let\endalgorithm\@undefined
\makeatother

\usepackage{algorithm2e}

\begin{document}

\begin{algorithm}
\SetAlgoLined
\KwResult{Write here the result }
initialization
\While{While condition}{
instructions
}
\caption{How to write algorithms}
\end{algorithm}

\end{document}

• Thanks DG'! Your guide helped me fix the error. tex.stackexchange.com/ is number 1 Nov 19 '20 at 0:45
• @VyHuynh - If this answer solved your problem, then please accept it, so the question can be colsed. See: How do you accept an answer?
– DG'
Nov 19 '20 at 8:15