7

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

1
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See minimal working example (MWE). Jul 18, 2017 at 4:52

2 Answers 2

12

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?

1
  • Hi, thanks for your answer. I think \DontPrintSemicolons should be \DontPrintSemicolon instead.
    – aerdna91
    Aug 4, 2018 at 14:41
3

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

1
  • Exactly looked for the macro \SetArgSty :) Thanks!
    – Arne
    Nov 8, 2022 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .