algorithm2e - why are some of my texts are italicized and some are not

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?


I agree with answer above. I will add the following: all styles can be redefined is algorithms, but you have to understand how are understood texts. Firts, if this math, this will be display as math, it means in italic Second, it depend if this is normal text or text of keyword, functions, argument of algorithm command, etc...

I enclose an exemple based on your code to show how could code your algorithm: 1) use math mode, as said above, when you write math 2) define your own variable macro, again as said above: you can use SetKwData macro of algorithm style and so SetDataSty to control the style of your variables 3) define functions of your algorithm (argmin and cost) with SetKwFunction 4) redefine style of algorithm text using macros provided by the algorithm

Here is my example based on yours (in particular, note differences between vertex that is typed as argument of ForEach and then in ArgSty, and edge that is defined as a Data (variable)

\documentclass[a4paper]{article}

\usepackage[lined,linesnumbered,boxed]{algorithm2e}
\usepackage{amsmath,amssymb,amstext}
\usepackage{xcolor,xcolor-material}

\SetKwFunction{argmin}{argmin$_k$}
\SetKwFunction{cost}{cost}
\SetKw{Of}{of}
\SetKwData{neighbors}{neighbors}
\SetKwData{edge}{edge}

\newcommand{\mykwsty}[1]{\textcolor{blue}{\emph{#1}}}
\newcommand{\myfuncsty}[1]{\textcolor{red}{\textbf{\texttt{#1}}}}
\newcommand{\myvarsty}[1]{\textcolor{GoogleGreen}{\texttt{#1}}}
\SetKwSty{mykwsty}
\SetArgSty{textbf}
\SetFuncSty{myfuncsty}
\SetDataSty{myvarsty}

\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{vertex $V_i\subset V$} % why is V_i italic but V not ?
        { 
            $j \leftarrow\argmin{\cost{$V_i, V_k$}},  k\subset\neighbors\ \Of\  i$\;
            $E_{ij}\leftarrow E_{ij} + 1$\;
        }
        \BlankLine
        \ForEach{\edge $E_{ij}\subset E$}
        { 
            $E_{ij}\leftarrow\argmin\left(E_{ij}\text{weight}\right),  j\subset N )$\;
            $E_{ij} \leftarrow E_{ij} + 1$\;
        }
    \end{algorithm}
\end{document}

that gives

enter image description here