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}
Related
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
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}
I've written the code, but line number are not proper, the code looks like this:
\begin{algorithm}[H]
\caption{Algorithm}
\begin{algorithmic}[1]
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\REQUIRE $Graph\ G(V, E)$
\STATE $\textbf{\textit{function}}\ $
\Do
\State Something
\doWhile
\end{algorithmic}
\label{algo1}
\end{algorithm}
The output of above code looks like this:
How I can get the proper numbering? Or is there any simpler way to write do while loop in latex?
Try the algorithm2e package. ;)
\documentclass[a4paper, 11pt]{article}
\usepackage[norelsize, linesnumbered, ruled, lined, boxed, commentsnumbered]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\SetAlgoLined
\LinesNumbered
\SetKwInOut{Input}{Input}
\Input{$Graph\ G(V, E)$}
\SetKwProg{Function}{function}{}{end}
\SetKwRepeat{Do}{do}{while}
\Function{function(param: int) : int}{
\Do{done = false}{ something }
}
\caption{Algorithm}
\end{algorithm}
\end{document}
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
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