Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This is my MWE for an algorithm however its throwing an error and I don't know how to get inputs and initialize. I want it to look like the attached image.

\documentclass[a4paper,10pt,twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs,lipsum}
\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}
\begin{algorithm}
        \caption{Algorithm 2: Boosted $K$ Nearest Neighbor}
        \begin{algorithmic}[1]
%         \qinput{Input:} \\
%    \State $S={s_i}={\left(x_i,y_i\right)$
         \For{t = 1 to T} \do
        \State $S_t \gets S_{t-1}$
        \For{$s_q \in S_t$} \do
          \State $N_q \gets$ k nearest neighbors 
          \State of $s_q$ using $D(s_q,s_i)$
          \State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
        \If{label($s_q$)$\ne y_q$}
            \For{$s_i \in N_i$} \do
            \If{$y_i \ne y_q$}
              \State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
            \Else
              \State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
            \EndIf
            \EndFor
        \EndIf
        \EndFor
        \If{label($s_q$)$=y_q \forall_{s_q}$}
          \State break
        \EndIf
     \EndFor
        \end{algorithmic}
\end{algorithm}
\end{document}

share|improve this question
    
Please add the error messages you get in your question. On an other computer it must not be that the same error occurs ... –  Kurt Nov 29 '12 at 0:00
add comment

1 Answer

Here's an option that provides feasible output. I've removed the physical numbering in the caption:

enter image description here

\documentclass[10pt,twocolumn]{article}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmx}
\algnewcommand{\Inputs}[1]{%
  \State \textbf{Inputs:}
  \Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\algnewcommand{\Initialize}[1]{%
  \State \textbf{Initialize:}
  \Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}

\begin{document}
\begin{algorithm}
  \caption{Boosted $K$ Nearest Neighbour}
  \begin{algorithmic}[1]
    \Inputs{$S={s_i}=\left(x_i,y_i\right)$}
    \Initialize{\strut$w_i^0 \gets 0$, $i=1,\ldots,n$ \\ $S_0 \gets S$}
    \For{t = 1 to T}
      \State $S_t \gets S_{t-1}$
      \For{$s_q \in S_t$}
        \State $N_q \gets$ k nearest neighbors 
        \State of $s_q$ using $D(s_q,s_i)$
        \State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
        \If{label($s_q$)$\ne y_q$}
          \For{$s_i \in N_i$}
            \If{$y_i \ne y_q$}
              \State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
            \Else
              \State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
            \EndIf
          \EndFor
        \EndIf
      \EndFor
      \If{label($s_q$)$=y_q \forall_{s_q}$}
        \State break
      \EndIf
    \EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

A newly-defined \Inputs and (equivalent) \Initialize takes one argument that sets its contents in a \Statex with an indent of \algorithmicindent - the default - to align with the other algorithm elements. The contents is stored in a top-aligned \parbox that fits across 80% of the line. I'm sure you won't need more, but in case you do, that can also be fixed to not cause overfull boxes.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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