How to fix alignment of algorithm in latex [closed] - latex

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}

Related

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

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

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}

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

latex caption language (2) [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
Referring to this question (Change LaTeX caption language), I got the same problem and it does not get solved by the use of the correct language package.
Here is a MWE:
\documentclass[a4paper,twoside]{article}
\usepackage[a4paper,top=2cm,bottom=2cm,inner=6cm,outer=2cm]{geometry}
\geometry{twoside}
\usepackage{lmodern}%font
\usepackage[francais]{babel}%language corrector and punctuation rules
\usepackage[utf8]{inputenc}%encoding
\usepackage[T1]{fontenc}
\usepackage[font=small]{caption}%titres des figures
\usepackage{color}
\usepackage{array}
\usepackage{colortbl}
\usepackage[pdftex]{graphicx}
\begin{document}
\begin{table}[h!]
\caption{Calendrier approximatif du processus législatif.}
\begin{tabular}{>{\raggedleft}p{0.65\textwidth}
p{0.1\textwidth}
}
Analyse du problème, fixation des objectifs et définition des moyens d'action possibles & 1 an\\
\end{tabular}
\end{table}
\end{document}
What do I do wrong? Thank you!
The standard tag for a table defined in frenchb.ldf is the word “Table”.
If you want it to become “Tableau”, for instance, you have to tell babel to.
\documentclass[a4paper,twoside]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}%encoding
\usepackage[francais]{babel}%language corrector and punctuation rules
\usepackage[a4paper,top=2cm,bottom=2cm,inner=6cm,outer=2cm]{geometry}
\geometry{twoside}
\usepackage{lmodern}%font
\usepackage[font=small]{caption}%titres des figures
\usepackage{color}
\usepackage{array}
\usepackage{colortbl}
\usepackage{graphicx}
\addto\captionsfrench{\renewcommand\frenchtablename{{\FBfigtabshape Tableau}}}
\begin{document}
\begin{table}[h!]
\caption{Calendrier approximatif du processus législatif.}
\begin{tabular}{>{\raggedleft}p{0.65\textwidth}
p{0.1\textwidth}
}
Analyse du problème, fixation des objectifs et définition des moyens d'action possibles & 1 an\\
\end{tabular}
\end{table}
\end{document}
I have also changed the package loading order to a better one. Note that you don't need to pass the pdftex option to graphicx (unless you have a very old TeX distribution).

Resources