1

In the code below, when I use the \For statement, the code does not compile and gives the message:

! Missing \endcsname inserted. <to be read again> \ALG@currentbkock@0

Can you help me get the code to compile? Thanks!

Here is the code:

\documentclass{article}
\usepackage{amsmath, amsfonts, amssymb, amsthm, bm}
\usepackage{commath}
\usepackage{xcolor}
\usepackage{algpseudocode}
\usepackage{pifont}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

\begin{document}

\begin{algorithm}[H]
\DontPrintSemicolon
%  \KwData{Training set $x$}
%  $\Delta_{ji}^l := 0$ \tcp*{will be used to compute $\partial x$}
%  \tcc{iterate over all training examples}
{\bfseries Input:} {a matrix of training samples $A = [A_{1}, A_{2}...,A_{k} ] \in \mathbb{R}^{m\times n}$ for $k$ classes, a test sample $\mathbf{y}\in \mathbb{R}^{m}$, (and an optional error tolerance $\varepsilon > 0$).}\\
Normalize the columns of $A$ to have unit $\ell^{2}$-norm.\\
Solve the $\ell^{1}$-minimization problem:
\begin{equation}
\hat{\bm{x}}_{1} = \arg \min_{x}\norm{\bm{x}}_{1}\quad \text{subject to}\quad A\bm{x} = \bm{y}
\end{equation}
(Or alternatively, solve
\begin{equation}
\hat{\bm{x}}_{1} = \arg \min_{x}\norm{\bm{x}}_{1}\quad \text{subject to}\quad \norm{A\bm{x} = \bm{y}}_{2}\leqslant \varepsilon).
\end{equation}\\
Compute the residuals $r_{i}(\bm{y}) = \norm{\bm{y} - A \delta_{i} (\hat{\bm{x}}_{1})}_{2}$\\
\For{$i = 1,\ldots,k$.}
\EndFor\\
{\bfseries Output:} identity$(\bm{y}) = \arg \min_{i}r_{i}$
\caption{Sparse Representation-based Classification (SRC)}
\end{algorithm}

\end{document} 
4
  • This is because you're mixing two incompatible algorithm packages. algorithm2e provides both the algorithm environment as well as the syntax macros for writing pseudocode. algpseudocode provides only syntax macros. You should use either algorithm2e or algpseudocode (and algorithm), but not both.
    – Werner
    Apr 23, 2016 at 15:17
  • @Werner, Thanks for your input. I removed algpseudocode but the code still does not compile.
    – Joe
    Apr 23, 2016 at 15:23
  • That should be obvious as you're intermixing the syntaxes provided by the two packages. I'm guessing you're after an algorithm2e style look in the end?
    – Werner
    Apr 23, 2016 at 15:31
  • Yes, I am looking for the algorithm2e look. Thanks.
    – Joe
    Apr 23, 2016 at 15:33

1 Answer 1

2

The problem is as a result of you mixing two incompatible algorithm packages. algorithm2e provides both the algorithm environment as well as the syntax macros for writing pseudocode. algpseudocode provides only syntax macros. You should use either algorithm2e or algpseudocode (and algorithm), but not both.

Here is a rough algorithm2e implementation of what you might be after:

enter image description here

\documentclass{article}
\usepackage{amsmath,amssymb,commath,bm}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}

\newcommand\mycommfont[1]{\footnotesize\ttfamily\textcolor{blue}{#1}}
\SetCommentSty{mycommfont}

% http://tex.stackexchange.com/a/153906/5764
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line

\begin{document}

\begin{algorithm}[H]
  \DontPrintSemicolon
  \caption{Sparse Representation-based Classification (SRC)}
  \KwIn{a matrix of training samples $A = [A_{1}, A_{2}, \dots ,A_{k}] \in \mathbb{R}^{m \times n}$ 
    for $k$ classes, a test sample $\mathbf{y} \in \mathbb{R}^{m}$, (and an optional error tolerance $\varepsilon > 0$).}

  Normalize the columns of $A$ to have unit $\ell^{2}$-norm.\;
  Solve the $\ell^{1}$-minimization problem:
  $\hat{\bm{x}}_{1} = \arg \min_{x}\norm{\bm{x}}_{1}\quad \text{subject to}\quad A\bm{x} = \bm{y}$ \;
  \nonl (Or alternatively, solve
  $\hat{\bm{x}}_{1} = \arg \min_{x}\norm{\bm{x}}_{1}\quad \text{subject to}\quad \norm{A\bm{x} = \bm{y}}_{2} \leqslant \varepsilon$).\;
  Compute the residuals $r_{i}(\bm{y}) = \norm{\bm{y} - A \delta_{i}(\hat{\bm{x}}_{1})}_{2}$\;
  \For{$i = 1,\dots,k$}{something}
\end{algorithm}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .