[Tex/LaTex] algorithm2e – why are some of the texts are italicized and some are not

algorithm2eitalic

I am using algorithm2e. These are my declarations:

\usepackage{cvpr}
\usepackage{times}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}

% For tables
\usepackage{tabu}
%\usepackage{caption} %\captionsetup[table]{skip=10pt}

\bibliographystyle{unsrtnat}
\usepackage[numbers,sort&compress]{natbib}
\usepackage[linesnumbered,boxed]{algorithm2e}
\usepackage{algpseudocode}

% Include other packages here, before hyperref.

% If you comment hyperref and then uncomment it, you should delete
% egpaper.aux before re-running latex.  (Or just hit 'q' on the first latex
% run, let it finish, and you should be clear).
\usepackage[breaklinks=true,bookmarks=false]{hyperref}

This is my code. Why are some of the texts italicized and some are not ? What is the function to make a text italicized and to not make it italicized ?

 \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \Input{a graph G = (V,E)}
        \Output{a hierarchical tree}  
        \BlankLine
        Initialize edge weights\\
        \ForEach{\normalfont{vertex} V_i \subset V } % why is V_i italic but V not ?
        { 
            j \leftarrow \text{argmin}_k(\text{cost}(V_i, V_k)), \ k \subset \text{neighbors} \ \text{of} \ i  \\
            E_{ij} \leftarrow E_{ij} + 1 \\
        }
        \BlankLine
        \ForEach{\normalfont{edge} E_{ij} \subset E }
        { 
            E_{ij} \leftarrow argmin(E_{ij} \normalfont{.weight}), \ j \subset N ) \\
            E_{ij} \leftarrow E_{ij} + 1 \\
        }
    \end{algorithm}

enter image description here

Best Answer

There are a number of things wrong with your current approach/usage of algorithm2e. In no particular order:

  1. Use \; as line endings, not \\. If you don't want the semi-colons to be printed, add \DontPrintSemicolon to your preamble.

  2. Surround your math content by $...$.

  3. Define \argmin as an operator.

  4. For consistency, define commands that do stuff. For example, formatting a "variable" in your pseudocode, one could define

    \newcommand{\var}{\texttt}
    

    and use \var for every variable.

  5. The first argument of \ForEach (and other conditional clauses in algorithm2e) is set using \itshape. If you want it to not be italics, then set it using {\upshape ...} or \textup{...}.

  6. Use mathptmx rather than the obsolete times.

enter image description here

\documentclass{article}

\usepackage{mathptmx,amsmath}
\usepackage[linesnumbered,boxed]{algorithm2e}
\DontPrintSemicolon

\DeclareMathOperator{\argmin}{argmin}% https://tex.stackexchange.com/q/5223/5764

\newcommand{\var}{\texttt}

\begin{document}

 \begin{algorithm}
  \SetKwInOut{Input}{Input}
  \SetKwInOut{Output}{Output}
  \Input{a graph $G = (V,E)$}
  \Output{a hierarchical tree}  
  \BlankLine
  Initialize edge weights\;
  \ForEach{ \textup{vertex} $V_i \subset V$ }
  { 
    $j \leftarrow \argmin_k (\var{cost}(V_i, V_k))$ where $k \subset \text{neighbours of $i$}$\;
    $E_{ij} \leftarrow E_{ij} + 1$\;
  }
  \BlankLine
  \ForEach{ \textup{edge} $E_{ij} \subset E$ }
  { 
    $E_{ij} \leftarrow \argmin (E_{ij} \text{.weight})$ where $j \subset N$\;
    $E_{ij} \leftarrow E_{ij} + 1$\;
  }
\end{algorithm}

\end{document}

As a side-note, I find the use of

$E_{ij} \leftarrow E_{ij} + 1$\;

superfluous, as the pseudocode construction already indicates that you're going through each E_{ij}. Moreover, what does E_{ij} + 1 refer to?

Related Question