I got myself in big trouble and I can't get out of it.

I use standalone package to combine different latex files into one. Each Latex file compiles OK on its own. When I combined them, I found a conflict, since I used algorithm2e in one file and used algorithm in another to type different algorithms. With standalone one must include the union of all packages used in all the files. So I have to include both packages now. I am stuck now. I do not want to rewrite the algorithm I did, as it took long time.

Here is a MWE

\If {$i\geq 1$}
    \State $i\gets 0$
    \State $i\gets 2$

And the error is

pdflatex foo.tex
Document Style algorithmicx 1.2 - a greatly improved `algorithmic' style
) (/usr/local/texlive/2015/texmf-dist/tex/latex/algorithmicx/algpseudocode.sty
Document Style - pseudocode environments for use with the `algorithmicx' style
) (./foo.aux)
! You can't use `\prevdepth' in horizontal mode.
\nointerlineskip ->\prevdepth 
                              -\@m \p@ 
l.13     \State $
                 i\gets 0$

if I comment out algorithm2e it compiles OK. But again, I have to include both in the master document since it using standalone to combine many different latex files and one of the other files (not shown here) needs algorithm2e.

Any way to handle this situation? I am using TL 2015. note that I also have to use pdflatex and not lualatex, since algorithm2e does not compile with lualatex.

*File List*
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size11.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
 amsmath.sty    2013/01/14 v2.14 AMS math features
 amstext.sty    2000/06/29 v2.01
  amsgen.sty    1999/11/30 v2.0
  amsbsy.sty    1999/11/29 v1.2d
  amsopn.sty    1999/12/14 v2.01 operator names
 algorithm.sty    2009/08/24 v0.1 Document Style `algorithm' - floating     
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
  ifthen.sty    2014/09/29 v1.1c Standard LaTeX ifthen package (DPC)
algorithmicx.sty    2005/04/27 v1.2 Algorithmicx
algorithm2e.sty    2013/01/06 v5.00 algorithms environments
xspace.sty    2014/10/28 v1.13 Space after command names (DPC,MH)
relsize.sty    2013/03/29 ver 4.1

related algorithm2e-command-algorithm-already-defined

  • You say that removing the 2e variant compiles fine, but this doesn't help in your case because you have some algs in both. Can you post an example where this simple fix doesn't work? – Mark Apr 27 '16 at 7:41