# how to write the algorithm in latex

My code is below, I didn't find the error.

\begin{algorithm}[H]
\SetAlgoLined
\textbf{Input} :$x_{k-1}$,$P_{k-1}$,$u_{k}$,$z_{k}$\;
\textbf{Output} : $x_{k}^{-}$,$P_{k}^{-}$ \\~\\
$x_{k+1}={\Phi{x}}_{k-1}+{Bu}_{k}+{G\omega}_{k}$
$P_{k}={\Phi{P}}_{k-1}{\Phi}^{T}+Q$
$K=PH^{T}(HPH^{T}+R)^{-1})$
$z=(z_{k}-H_{k}x_{k})$
$x_{k}^{-}=x_{k}+kz$
$P_{k}^{-}=P-KHP$
\textbf{return}$x_{k}^{-}$,$P_{k}^{-}$
\caption{ \textsc{Kalman Filter} }
\end{algorithm}

• It might be helpful if you could complete your code snippet to a minimal working example: a short document that starts with \documentclass and ends with \end{document} and contains as few lines of code/text as are necessary to reproduce the problem. Feb 10 '19 at 18:36
• There are several LaTeX packages that are meant to assist with the typesetting of algorithms. Please tell us which package, or which packages, you employ.
– Mico
Feb 10 '19 at 18:39
• %Algorithm \usepackage[boxruled,linesnumbered]{algorithm2e} Feb 10 '19 at 18:40
• Please do not create for each question a new unregistred account (and delete it after getting an answer). You are not able to comment and vote and accept doing that. Register one account and ask the staff to merge the other accounts for you! As far as I remember this is your third question about algorithmn ... Feb 10 '19 at 18:41
• what can i do ? Feb 10 '19 at 18:43

You need to use \; to close every statement, otherwise content is consider part of the same line: \documentclass{article}

\usepackage{algorithm2e}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output\,}
\SetAlgoLined
\DontPrintSemicolon

\begin{document}

\begin{algorithm}[H]
\Input{$x_{k - 1}$, $P_{k - 1}$, $u_k$, $z_k$}
\Output{$x_{k}^{-}$, $P_k^{-}$}
\BlankLine
$x_{k + 1} = \Phi(x)_{k - 1} + Bu_k + G\omega_k$\;
$P_k = \Phi(P)_{k - 1} \Phi^T + Q$\;
$K = PH^T (HPH^T + R)^{-1})$\;
$z = (z_k - H_k x_k)$\;
$x_k^{-} = x_k + k z$\;
$P_k^{-} = P - KHP$\;
\textbf{return} $x_k^{-}$, $P_k^{-}$\;
\caption{\textsc{Kalman Filter}}
\end{algorithm}

\end{document}

• i want to put it in boxes Feb 10 '19 at 18:50
• @houssem - Then just change \usepackage{algorithm2e} to \usepackage[boxruled,linesnumbered]{algorithm2e}.
– Mico
Feb 10 '19 at 18:53
• 186 LaTeX Error: Environment algorithm undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.186 \begin{algorithm} [H] Feb 10 '19 at 19:00
• @houssem: You should be more clear in your descriptions. algorithm2e defines the algorithm environment. My guess is you're including something like algpseudocode or similar (from algorithmicx). Is that the case? Use your words to describe the problem... Feb 10 '19 at 19:06
• how , I don't understand Feb 10 '19 at 19:08