1

I am totally new to LaTeX. Let me tell my problem.

I have installed TexStudio, after that I took thesis template and editing it using textstudio.

If I want to write pseudo code in my template I have downloaded algpseudocode.sty and copied to template, where min.tex is located. But I am getting the "can be used only in preamble error" in editor. Do I need to copy this file somewhere in textstudio or anywhere else?

I am using windows7.

I am testing with the following code

\usepackage{algpseudocode}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. 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{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}

The Error:

Can be used only in preamble. \usepackage
Environment algorithm undefined. \begin{algorithm}
Package caption Error: \caption outside float \caption
Environment algorithmic undefined. \begin{algorithmic}
Undefined control sequence \Procedure
Undefined control sequence \Procedure{Euclid}{$a,b$}\Comment
Undefined control sequence \State
Undefined control sequence \While
Undefined control sequence \While{$r\not=0$}\Comment
Undefined control sequence \State
Undefined control sequence \State
Undefined control sequence \State
Undefined control sequence \EndWhile
Undefined control sequence \State
Undefined control sequence \State \textbf{return} $b$\Comment
Undefined control sequence \EndProcedure
\begin{document} ended by \end{algorithmic}. \end{algorithmic}
\begin{document} ended by \end{algorithm}. \end{algorithm}
Underfull \hbox (badness 10000) in paragraph
Difference (3) between bookmark levels is greater (hyperref) than one, level fixed
  • Post some code instead. Plus, you should no download packages like algpseudocode by hand, have your package manager install it for you. – daleif Nov 6 '13 at 12:20
  • That is not a LaTeX document. Where is the document class and preamble? – daleif Nov 6 '13 at 14:18
0

This compiles just fine. Note that the algorithm package is needed as well as it provides the algorithm environment

\documentclass[a4paper]{article}

\usepackage{algpseudocode,algorithm}

\begin{document}

\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. 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{euclidendwhile}
\State \textbf{return} $b$\Comment{The gcd is b}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
  • It works fine after adding the following packages in main.tex. "\usepackage{algpseudocode}" "\DeclareCaptionType{algorithm}" – user2814979 Nov 6 '13 at 14:41

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.