problem in not showing the statement after arrow inside if else statement - latex

I have written an algorithm in latex like the following.
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{algorithm}
\caption{My algorithm}\label{euclid}
...
\State $\vec{cle} \gets $\text{Mean($o1,o2$)}
\If{$\vec{clh[t]}$ is empty}
\State $\vec{clh[t]} \gets $\vec{cle}$
\Else
\State $\vec{$clh[t]} \gets \text{MeanCalc}($\vec{$cle}*0.1,clh[t]*0.5)$
\EndIf
\EndIf
My problem is, in the line after If statement, $\vec{cle}$ doesn't show up after arrow. Also after else, \State $\vec{$clh[t]} doesn't show up before arrow. I appreciate if anyone can help me with this

The usage of the math environment $...$ in your snippet was inconsistent ...
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{My Algorithm}\label{alg:euclid}
\begin{algorithmic}
\State $\vec{cle} \gets $\text{Mean($o1,o2$)}
\If{$\vec{clh[t]}$ is empty}
\State $\vec{clh[t]} \gets \vec{cle}$
\Else
\State $\vec{clh[t]} \gets $\text{MeanCalc}($\vec{cle}*0.1,clh[t]*0.5)$
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}
With the result:
Hope this helps

Related

Latex algorithm - IF-ELSE - IF shows end before begins ELSE [duplicate]

This question already has an answer here:
If-Else Statement in knitr/Sweave using R variable as conditional
(1 answer)
Closed 2 years ago.
As you see in my latex it shows end after If statement which supposes to end after Else. I tried removing the curly braces of the IF but it does not work.
\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
\usepackage[pdftex]{graphicx}
\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage[linesnumbered,ruled,vlined] {algorithm2e}
\usepackage{nomencl}
\usepackage{algpseudocode}
\usepackage{algorithm}
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\begin{algorithm}
\caption{PEC, \texttt{PEC}}
\SetAlgoLined
\DontPrintSemicolon
\textbf{Input:} I, P, C.
\textbf{Output:} Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$
//Initialization
\quad \quad \quad $PS^* \gets \emptyset$
\For {each, $i \in I$} {
// Calculate
$PS^{temp}_{i} \gets P$
$PS^{loss}_{i} \gets C)$
\If { $ C_{i}- P_{i} > 0$}{
$PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
}
\Else {
$PS_{i} \gets 0$
}
}
return ($PS^*$)
\label{alg:PoEG}
\end{algorithm}
\end{document}
The output for this problem is as follows, as you see the highlighted end, which I need to eliminate.
Your code does not compile due to two major problems:
you must not load the graphicx package multiple times with conflicting options. If your tex distribution has been updated at least once since the Stone Age, then best load it without option and let latex determine the necessary driver itself.
you must not load conflicting packages for algorithms. Decide which one you want to use and load this. Latex very clearly gives you error messages about commands already defined, DON'T ignore errors!!!!!
... and then there is minor inconvenience of the additional end: simply use the \eIf macro instead of \If if you have an in-else statement
And finally a suggestion: if you you wouldn't manually mess with \quads and use appropriate keywords instead, things would align automatically
\documentclass[journal]{IEEEtran}
\ifCLASSINFOpdf
%\usepackage[pdftex]{graphicx}
%\usepackage[dvips]{graphicx}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{amsfonts}
\usepackage{amssymb,mathtools}
\usepackage{algpseudocode}
%\usepackage{algorithm}
\usepackage[linesnumbered,ruled,vlined]{algorithm2e}
\usepackage{nomencl}
\hyphenation{op-tical net-works semi-conduc-tor}
\begin{document}
\begin{algorithm}
\caption{Proof of Energy Generation, \texttt{PoEG}}
\SetAlgoLined
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\DontPrintSemicolon
\Input{Prosumer Set, $\textit{I}=\{{\textit{I}_{1},\textit{I}_{2},...,\textit{I}_{n}}$\},\linebreak
Predicted energy production, $\textit{P}= \{{\textit{P}_{1},\textit{P}_{2},...,\textit{P}_{n}}$\}, \linebreak
Predicted energy consumption, $\textit{C}= \{{\textit{C}_{1},\textit{C}_{2},...,\textit{C}_{n}}.$\},\linebreak
Distance from DS, $\textit{D}= \{{\textit{d}_{1},\textit{d}_{2},...,\textit{d}_{n}}$\}.}
\Output{Set of $PS^*$= $\{{PS_{1},PS_{2}, ...,PS_{n}}\}$ }
//Initialization
\quad \quad \quad $PS^* \gets \emptyset$
\For {each prosumers, $i \in I$} {
// Calculate proof score for energy balance and loss,
$PS^{temp}_{i} \gets \log(1/e^{C_i-P_i})$
$PS^{loss}_{i} \gets \log(e^{E_{d_i}})$
\eIf { $ C_{i}- P_{i} > 0$}{
$PS_{i} \gets $($PS^{temp}_{i} - PS^{loss}_{i}$)
}{
$PS_{i} \gets 0$
}
$PS^* \gets$ $PS^* \cup \{PS_{i}\}$
}
return ($PS^*$)
\label{alg:PoEG}
\end{algorithm}
\end{document}

Latex algorithm line spacing

I have created an algorithm in latex but there is some extra line spacing being inserted.
I currently have the following code
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\begin{document}
This this this
\begin{algorithm}
\algblock[TryCatchFinally]{try}{endtry}
\algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
\algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
[1]{\textbf{catch} #1}
{\textbf{end try}}
\caption{Task}
\begin{algorithmic}[1]
\Procedure {Task Check}{}
\State \parbox[t]{313pt}{$aaaaaaaaa\textunderscore aaaaaaa = Thread(aaaaaa=call\textunderscore service,
args=(
"aaaaa\textunderscore aaaaaa\textunderscore generation\textunderscore aaaa",
Craaaaalan,
"load\textunderscore plan\textunderscore aaaa")
)$}
\State \parbox[t]{313pt}{$aaaa-aaaaaaaaa\textunderscore aaaaaaa = Thread(aaaaaa=call\textunderscore service,
args=(
"aaaaa\textunderscore aaaaaa\textunderscore generation\textunderscore aaaa",
Craaaaalan,
"load\textunderscore plan\textunderscore aaaa")
)$}
\State \parbox[t]{313pt}{$aaaaaa\textunderscore aaaaaaa = Thread(aaaaaa=call\textunderscore service,
args=(
"aaaaa\textunderscore aaaaaa\textunderscore generation\textunderscore aaaa",
Craaaaalan,
"load\textunderscore plan\textunderscore aaaa")
)$}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
and the output is somewhat like this:
The three states in the images are identical service calls with a minor change. The minor change is the use of diffent words and hence a difference length. The sample code attached contains the accurate length. The output of State 7 is what I need for the other states as well but due to some extra words it does not follow the format. Any idea how to correct State 5 and **State 6 ** so that it looks like State 7?
Color difference with and without math mode
Thanks
Math mode is totally unsuitable to set verbatim content like variable names. The missing line breaks should be the least of your worries. The kerning is messed up, the quotation marks are wrong, minus signs instead of hyphens...
You could try with raggedright normal text, but I would typeset such things as source code, e.g. using the listings package:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, breaklines}
\begin{document}
\begin{algorithm}
\algblock[TryCatchFinally]{try}{endtry}
\algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
\algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
[1]{\textbf{catch} #1}
{\textbf{end try}}
\caption{Task}
\begin{algorithmic}[1]
\Procedure {Task Check}{}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaaaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaa-aaaaaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
Or if you insist on italic font:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{listings}
\lstset{basicstyle=\itshape, breaklines}
\begin{document}
\begin{algorithm}
\algblock[TryCatchFinally]{try}{endtry}
\algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
\algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
[1]{\textbf{catch} #1}
{\textbf{end try}}
\caption{Task}
\begin{algorithmic}[1]
\Procedure {Task Check}{}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaaaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaa-aaaaaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\State \parbox[t]{313pt}{\raggedright\lstinline{aaaaaa_aaaaaaa = Thread(aaaaaa=call_service, args=("aaaaa_aaaaaa_generation_aaaa", Craaaaalan, "load_plan_aaaa"))}}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
Or maybe even with as real listings with linebreaks etc. to make things more readable:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{listings}
\lstset{basicstyle=\ttfamily, breaklines}
\begin{document}
\begin{algorithm}
\algblock[TryCatchFinally]{try}{endtry}
\algcblock[TryCatchFinally]{TryCatchFinally}{finally}{endtry}
\algcblockdefx[TryCatchFinally]{TryCatchFinally}{catch}{endtry}
[1]{\textbf{catch} #1}
{\textbf{end try}}
\caption{Task}
\begin{algorithmic}[1]
\Procedure {Task Check}{}
\State \vspace*{-1.46\baselineskip}\begin{lstlisting}
aaaaaaaaa_aaaaaaa = Thread(
aaaaaa=call_service,
args=(
"aaaaa_aaaaaa_generation_aaaa",
Craaaaalan,
"load_plan_aaaa"
)
)
\end{lstlisting}
\State \vspace*{-1.46\baselineskip}\begin{lstlisting}
aaaa-aaaaaaaaa_aaaaaaa = Thread(
aaaaaa=call_service,
args=(
"aaaaa_aaaaaa_generation_aaaa",
Craaaaalan,
"load_plan_aaaa"
)
)
\end{lstlisting}
\State \vspace*{-1.46\baselineskip}\begin{lstlisting}
aaaaaa_aaaaaaa = Thread(
aaaaaa=call_service,
args=(
"aaaaa_aaaaaa_generation_aaaa",
Craaaaalan,
"load_plan_aaaa"
)
)
\end{lstlisting}
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}

Latex algorithmic package error "a missings \item"

I'm trying to compile my text and getting an error from the following oart
\begin{algorithm}
\caption{$Point2PointCollision()$}
\label{P2P}
\begin{algorithmic}[1]
\State {\textbf{Input:} $bla bla$}
\State {\textbf{Output:} $bla bla$}
\State {calculate $blabla$}
\If {$bla bla$}
\State {$bla bla$}
\Else
\State {$bla bla$}
\EndIf
\end{algorithmic}
\end{algorithm}
error is
LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation. Type H
for immediate help. ...
l.383 \end{algorithmic}
Here is themain tex file:
\documentclass[twoside,10pt]{Latex/Classes/PhDthesisPSnPDF}
% put the packages you want to use here
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{paralist}
\usepackage{graphicx} % for pdf, bitmapped graphics files
\usepackage{epsfig} % for postscript graphics files
\usepackage{textcomp}
\usepackage{adjustbox}
\usepackage{epsf}
\usepackage{epstopdf} % for postscript graphics files
\usepackage{times} % assumes new font selection scheme installed
\usepackage{amsmath} % assumes amsmath package installed
\usepackage{amssymb} % assumes amsmath package installed
\let\proof\relax
\let\endproof\relax
\usepackage{cite}
\usepackage{graphicx}
\usepackage{psfrag}
\usepackage{algorithm} %for algorithm environment
%\usepackage{algorithmic}
\usepackage{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\usepackage{enumerate}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{dblfloatfix}
\usepackage{setspace}
\usepackage{paralist}
\usepackage{booktabs}
\theoremstyle{definition}
\newtheorem{exmp}{Definition}
\usepackage{bm,upgreek}
\usepackage{bmpsize}
\usepackage{tikz}
\newtheorem{definition}{Definition}
\usepackage[normalem]{ulem}
\include{3_chapter/chapter}
\end{document}
Can anyone help me how to solve this?
Thanks in advance

Making an algorithm more readable using LaTeX

I'm trying to make my algorithm more readable in LaTeX:
\documentclass{IEEEtran}
\usepackage{algpseudocode}
\usepackage{algorithm}
\begin{document}
\begin{algorithm}[H]
\caption{Detecta \textit{Slowloris}}
\begin{algorithmic}[1]
\Function{Divide\_slices\_1min }{Arquivo PCAP}
\State \Return \textit{sliceAtual}
\EndFunction
\Function{Separa\_IP\_Origem\_Destino}{sliceAtual}
\State \Return $ArquivoIPs$
\EndFunction
\Function{Calcula\_entropia\_IP\_Origem\_Destino}{ArquivoIP}
\State \Return $EntroSliceAtualIP$
\EndFunction
\Function{PVS}{sliceAtual}
\State \Return $PVS_SliceAtual$
\EndFunction
\Function{FCS}{sliceAtual}
\State \Return $FCS_SliceAtual$
\EndFunction
\If {$entropiaSliceAtual$ > $entropiaSemAtaq$ + 0,10*$entropiaSemAtaq$}
\If {$PVS_SliceAtual$ > $PVS_SemAtaq$ + 0.60*$PVS_SemAtaq$}
\If {$FCS_SliceAtual$ > $FCS_SemAtaq$ - 0.40*$FCS_SemAtaq$}
\State \Return $Ataque$
\Else
\State \Return $SemAtaques$
\EndIf
\EndIf
\EndIf
\end{algorithmic}
\end{algorithm}
\end{document}
The result was not so good:
I would like it to appear just the call to the functions without the lot of endif.
I would like something like this:
Any suggestion? I am trying hard!
The classe IEEEtran is : https://ctan.org/pkg/ieeetran?lang=en
— So you just want to remove the 3 end ifs?
— Yes, all "ends": end if , end function.
If so, you just need to replace
\usepackage{algpseudocode}
with
\usepackage[noend]{algpseudocode}
Sources:
paragraph 3.1.9 in algorithmics.pdf
lines 66–74 in algpseudocode.sty

How to fix alignment of algorithm in latex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I need to fix the alignment of this LaTeX code.
When I compile it, the output has the wrong alignment of if, for, else statements.
\begin{algorithm}[H]
\caption{:The $ASP\_Tree$ algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\Input{TV database and min\_sup }
\Output{Bla bla bla}
\KwData{bla bla bla}
\Procedure{Tree}{$TV_i, min\_sup$}
{
\State {$[Root] \leftarrow [NULL]$} \newline
\For {$i=1$ to $N$} {\label{forins} \newline
{
%\Statex \Comment {\%Comments: insert TV values into the tree based on rules}
\State {$Root \leftarrow TV_1$}
\State {$i \leftarrow i+1$} \newline
\If {TV_i \textgreater Root}
\State Root \gets TV_i
\ELSIF {TV_i\leq Root}
\State i\gets i+1
\ELSE
\State do something interesting
\ENDIF
}
\EndFor
}
\EndProcedure
\end{algorithm}
Your problem lies in the fact that you're mixing notations of different packages, which are inherently incompatible. You should choose either algorithm2e or algorithmicx (which provides algpseudocode and algcompatible), not both.
Here is an algorithm2e implementation of what you might be after:
\documentclass{article}
\usepackage{algorithm2e,amsmath}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output\,}
\SetKwInOut{Data}{Data}
\SetKwProg{Tree}{Tree}{}{EndTree}
\begin{document}
\begin{algorithm}[H]
\caption{The ASP\_Tree algorithm}
\Input{TV database and min\_sup}
\Output{Bla bla bla}
\Data{bla bla bla}
\Tree{$TV_i, min\_sup$}{
$[\text{Root}] \gets [\text{NULL}]$\;
\For {$i = 1$ to $N$} {
$\text{Root} \gets TV_1$\;
$i \gets i + 1$\;
\uIf {$TV_i > \textup{Root}$}{
$\text{Root} \gets TV_i$\;}
\uElseIf{$TV_i \leq \textup{Root}$}{
$i \gets i+1$\;}
\Else{
do something interesting\;
}
}
}
\end{algorithm}
\end{document}

Resources