2

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!

4
  • 1
    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 :)
    – Elad Den
    Nov 18 '20 at 7:07
  • thank you for your answer!
    – Vy Huynh
    Nov 18 '20 at 7:35
  • 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
    – Vy Huynh
    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
    – Elad Den
    Nov 18 '20 at 8:55
1

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}
2
  • 1
    Thanks DG'! Your guide helped me fix the error. tex.stackexchange.com/ is number 1
    – Vy Huynh
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.