1

I wrote this code:

\begin{algorithm}
    \caption{SBX Operator}\label{alg:sbx}
    \begin{algorithmic}
        \State $\eta_{SBX} \gets 5$
        \For{i=1:L}
        \State sample $u \in  [0,1]$
        \If{$u$ < 0.5}
            \State $\beta = 2u^{\frac{1}{\eta_{SBX}+1}}$
        \Else
            \State $\beta = (2-2u)^{\frac{-1}{\eta_{SBX}+1}}$
        \EndIf
        \State $o_i^1 = \frac{p_i^1+p_i^2-\beta|p_i^1-p_i^2|}{2}$
        \State $o_i^2 = \frac{p_i^1+p_i^2+\beta|p_i^1-p_i^2|}{2}$
        \EndFor
    \end{algorithmic}
\end{algorithm}

and the algorithm looks like this after the compilation process:

enter image description here

but I need the traditional form with the lines and the caption on the upper side.

Please help

4
  • 4
    Welcome. // Can you please add the missing code, which makes this example compile? I.e. the preamble frees us from guessing what you did and how ;-) Thank you Commented May 26 at 17:05
  • 1
    Just to give you an idea about our guess-working to do ... ctan.org/topic/pseudocode // ctan.org/topic/listing Commented May 26 at 17:07
  • Does your code by any chance load the float package? Please advise. Commented May 26 at 18:15
  • 1
    Can you describe your problem succinctly in a better-worded title? That would help get better traction for people looking to help, or have similar problems. Commented May 26 at 21:10

1 Answer 1

1

I need the traditional form with the lines and the caption on the upper side.

You've given exactly zero information about your document setup, such as which algorithm-related packages you load. Moreover, you haven't mentioned if you encounter error and/or warning messages when you try compile your code snippet. Given your write-up, though, I'm assuming that what you should be doing is to employ the algorithm and algpseudocode packages.

I would also employ inline-fraction notation instead of \frac notation in four of the lines of code. And you should definitely (a) replace $u$ < 0.5 with $u < 0.5$ and (b) render i=1:L in math mode. (If you don't want whitespace padding around the : symbol, just encase it in a pair of curly braces.) Finally, the SBX subscripts should be typeset in a Roman (upright) font instead of in italics.

enter image description here

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\usepackage{mathtools} % for '\DeclarePairedDelimiter' macro
\DeclarePairedDelimiter\abs\lvert\rvert
\newcommand\etaSBX{\eta^{}_{\mathrm{SBX}}}

\begin{document}

\begin{algorithm}
    \caption{SBX Operator}\label{alg:sbx}
    \begin{algorithmic}
        \State $\etaSBX \gets 5$
        \For {$i=1:L$}
        \State sample $u \in  [0,1]$
        \If{$u < 0.5$}
            \State $\beta = 2u^{1/(\etaSBX+1)}$
        \Else
            \State $\beta = (2-2u)^{-1/(\etaSBX+1)}$
        \EndIf
        \State $o_i^1 = (p_i^1+p_i^2-\beta\abs{p_i^1-p_i^2})/2$
        \State $o_i^2 = (p_i^1+p_i^2+\beta\abs{p_i^1-p_i^2})/2$
        \EndFor
    \end{algorithmic}
\end{algorithm}

\end{document}
3
  • 1
    Thank you for your help, and I'm sorry about the lack of information. I copied and pasted your code, but maybe the template has some modifications I can't see because it looks identical, but has valuable improvements like the frac modification. Commented May 26 at 17:32
  • 1
    @DavidPeña - It's not too late to provide some very welcome information about your document setup. In particular, which algorithm-related packages are you loading? And, which TeX distribution do you employ, and when was it last updated? Commented May 26 at 17:34
  • 1
    @DavidPeña - Please also indicate if your (preamble) code modifies the behavior of various algorithm packages. Commented May 26 at 17:41

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.