0

I am using ShareLatex. I have an .tex containing some pseudo code using the algorithm2e package. It is as follows:

\documentclass[11pt, a4paper]{article}
\usepackage[portrait, margin=1in]{geometry}
\usepackage{amsmath}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}
\begin{algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

\underline{function AlgoTest} $(G)$\;
\Input{bla}
\Output{blubb}
Let $v$ be a vertice $\in V$\;

\ForEach{Vertex $v \in G$}
{
    \tcp{doSth}
}
return $G$\;
\caption{blaBlubb}
\end{algorithm}
\end{document}

It compiles fine and looks like what I want. I now have several of such files containing their own algorithms. I now want to include this code into my work/ prosa text and I want it there to exactly look like the one above. Note that the one above has its own commands like usepackage which is good for doing an error free compile of the single code-file (I mean the above one).

So I am doing like in this tex-file:

\documentclass{article}
\usepackage[subpreambles=true]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{import}

\begin{document}

\import{./}{algotest.tex}

\end{document}

When compiling it also looks fine, no problem but theres an compilation error given by ShareLatex:

/usr/local/texlive/2014/texmf-dist/tex/latex/algorithm2e/algorithm2e.sty, line 1177

Undefined control sequence.

The compiler is having trouble understanding a command you have used. Check that the command is spelled correctly. If the command is part of a package, make sure you have included the package in your preamble using \usepackage{...}. Learn more Was this hint helpful?Yes / No

\SetAlgoLined

l.1177 ...etcount,titlenotnumbered,lined,shortend} % The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

! Undefined control sequence. \SetAlgoShortEnd

Part of the given log output is as follows:

LaTeX Info: Redefining [ on input line 2665. LaTeX Info: Redefining ] on input line 2666. ) (/usr/local/texlive/2014/texmf-dist/tex/latex/algorithm2e/algorithm2e.sty Package: algorithm2e 2013/01/06 v5.00 algorithms environments \c@AlgoLine=\count107 (/usr/local/texlive/2014/texmf-dist/tex/latex/base/ifthen.sty Package: ifthen 2014/09/29 v1.1c Standard LaTeX ifthen package (DPC) ) ! Undefined control sequence. \SetAlgoLined

l.1177 ...etcount,titlenotnumbered,lined,shortend} % The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

! Undefined control sequence. \SetAlgoShortEnd

l.1177 ...etcount,titlenotnumbered,lined,shortend} % The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., `I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

(/usr/local/texlive/2014/texmf-dist/tex/latex/tools/xspace.sty Package: xspace 2014/10/28 v1.13 Space after command names (DPC,MH) ) (/usr/local/texlive/2014/texmf-dist/tex/latex/relsize/relsize.sty Package: relsize 2013/03/29 ver 4.1 )


Package `algorithm2e' Release 5.0 -- january 06 2013 -- - algorithm2e-announce@lirmm.fr mailing list for announcement about releases - algorithm2e-discussion@lirmm.fr mailing list for discussion about package subscribe by emailing sympa@lirmm.fr with 'subscribe ' - Author: Christophe Fiorio (cfiorio@um2.fr)


Hopefully theres a way to solve the compilation error. If there is any information missing, just request for it. Thanks in advance.

  • while it is possible to input files while skipping their preamble it's always fragile and doesn't really have any benefits over the standard \input mechanism which is much more robust. If your algorithm environment was in a separate file you could \input it into the main document and also into a document that just has the one algorithm – David Carlisle Dec 24 '16 at 0:26
  • I still tried this – Kaspatoo Dec 24 '16 at 1:19
  • I still tried this using this code in prosa-file: \documentclass[11pt, a4paper]{article} \usepackage[portrait, margin=1in]{geometry} \usepackage{amsmath} \usepackage[linesnumbered,ruled]{algorithm2e} \begin{document} \input{./algotest.tex} \end{document} leads to errors and a wrong displaying, the follown then is shown as text but not as algorithm-code: [11pt, a4paper]article [portrait, margin=1in]geometry amsmath [linesnumbered,ruled]algorithm2e errors are: LaTeX Error: Can be used only in preamble. – Kaspatoo Dec 24 '16 at 1:22
  • currently the only workaround, which is very bad i think, is to - make algorithm2e code without preamble and without begindocument tag - import the file into prosa using \import{./}{algotest.tex} - include includes of algorithm2e etc. into prosa preamble - when whished to only compile the algorithm create an independent "wrapper" file also importing the code and also having its own includes in its preamble this way one need three files for inserting code into prosa and for beeing able to compile the code separated on its own – Kaspatoo Dec 24 '16 at 1:47
  • yes that's is the normal way, although no need for \import or any package to do that, you can just use \input – David Carlisle Dec 24 '16 at 10:33
0

I now am using the following workaround (i call it that way because it causes circumstances).

  • ever algorithm has its own tex file but without any preamble and without includes etc.
  • this way I cannot compile these files directly but need another tex using \input and all neccessary includes to compile this file
    • this is cumbersome because I always need to change the tex for compiling when just changed a little on the pseudo-code

this way my code-tex is looking like this:

\begin{algorithm}
...
\end{algorithm

these "wrapper-tex" is looking like this:

\documentclass[12pt, letterpaper]{article}
\usepackage[utf8]{inputenc}
\usepackage{import}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}

\import{Algo}{MyAlgo.tex}

\end{document}

For not getting the line pitch of the surrounding document I use \renewcommand before unsing input on the code and after it again to setting it back. When the code does not fit into the line latex display text, which actually occurs after the input, before the code itself (on the previous page if theres enough space left for text but not for code). I got it by adding a \newpage after the input of the code.

Using include does not show my pseudo code at all. My include that way looks like:

\begin{flushleft}

some text
...
again some text

\renewcommand{\baselinestretch}{1.0}

\input{Algo/MyAlog.tex}

\renewcommand{\baselinestretch}{1.5}


\newpage

againagainsometext
...
u know
\end{flushleft}
...

I had also problems because in the tex with my texts I defined 12pt fontsize while I didn at first on my code-wrapper. The pseudocode that way was enlarged after inputting it. Resizing it using recommendations of these Reduce pseudocode font size (not global) or othere did not help very much, I surrendered after over an hour trying so I reduced my pseudocode by dividing it into multiple/ separate algorithms and by deleting code commentary.

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.