# Creating keywords which have an associated **end** with algorithm2e

As a part of a homework assignment in a Latex course, I have been assigned to write this algorithm in Latex.

I have got most of the part done with this code:-

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx, blindtext}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}
\RestyleAlgo{boxed}
\begin{algorithm}[h!]
\DontPrintSemicolon
\SetKwInput{kwInput}{Input}
\SetKwInput{kwInit}{Initialization}
\SetKwInput{kwMain}{Forward Inclusion}
\SetKwInput{kwOutput}{Output}
\kwInput{
\par
\vspace{0.25cm}
Training examples $Z = \{(x_1 , y_1 ), . . . , (x_N , y_N )\}$, where $N = a + b$, of which $a$ examples have $y_i = 1$ and $b$ examples have $y_i = −1$\;
\vspace{0.25cm}
The number $M$ of weak classifiers to be combined.

}
\kwInit{
\par
\vspace{0.25cm}
\Indp{
$w_i^{(0)}= \begin{cases} \frac{1}{2a} & \text{$for\ those\ examples\ with\ y_i=1$} \\ \frac{1}{2b} & \text{$otherwise$} \end{cases}$
}
\vspace{0.25cm}
}
\kwMain{
\par
\vspace{0.25cm}
\For{m = 1, . . . , M}{
Choose optimal $h_m$ to minimize the weighted error\;
Choose $a_m$ according to (2.17)\;
Update $w_i^{(m)} \gets w_i^{(m-1)} e^{-y_ia_mh_m(x_i)}$ and normalize to $\sum_i{w_i^{(m)}} = 1$\;
}
\vspace{0.25cm}
}
\kwOutput{
\par
Classification Function: $H_M(x)$ as in (2.20)\;
Class Label Prediction: $\hat{y}(x) = sgn(H_M(x))$\;
}
\end{algorithm}

\end{document}


However, I am unable to create a new Keyword (Like \kwInit in the code above) which has its own associated end, and auto-indented code segment.

Being new to the Latex typesetting system, any help is highly welcome.

For reference, my code produces output that looks like this:-

Just replace the \SetKwInput{word} with \SetKwBlock{beginword}{endword}. See algorithm2emanual Section 11.2 page 35. This modification provides

\documentclass[12pt]{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx, blindtext}
\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}
\RestyleAlgo{boxed}
\begin{algorithm}[h!]
\DontPrintSemicolon
\SetKwInput{kwInput}{Input}
\SetKwBlock{kwInit}{Initialization}{end}
\SetKwBlock{kwMain}{Forward Inclusion}{end}
\SetKwInput{kwOutput}{Output}
\kwInput{
\par
\vspace{0.25cm}
Training examples $Z = \{(x_1 , y_1 ), . . . , (x_N , y_N )\}$, where $N = a + b$, of which $a$ examples have $y_i = 1$ and $b$ examples have $y_i = −1$\;
\vspace{0.25cm}
The number $M$ of weak classifiers to be combined.

}
\kwInit{
\par
\vspace{0.25cm}
\Indp{
$w_i^{(0)}= \begin{cases} \frac{1}{2a} & \text{$for\ those\ examples\ with\ y_i=1$} \\ \frac{1}{2b} & \text{$otherwise$} \end{cases}$
}
\vspace{0.25cm}
}
\kwMain{
\par
\vspace{0.25cm}
\For{m = 1, . . . , M}{
Choose optimal $h_m$ to minimize the weighted error\;
Choose $a_m$ according to (2.17)\;
Update $w_i^{(m)} \gets w_i^{(m-1)} e^{-y_ia_mh_m(x_i)}$ and normalize to $\sum_i{w_i^{(m)}} = 1$\;
}
\vspace{0.25cm}
}
\kwOutput{
\par
Classification Function: $H_M(x)$ as in (2.20)\;
Class Label Prediction: $\hat{y}(x) = sgn(H_M(x))$\;
}