4

Can you help me on this one? The word min in the line \If{ $c (\mathbf{x}) < c^{\text{min}} $} is coming out in math mode, while in the others, in text mode.

\documentclass{article}
\usepackage{amsmath,amssymb}

\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}


\usepackage[ruled]{algorithm2e}
\begin{document}
\begin{algorithm}
    \DontPrintSemicolon 

    \KwIn{Blalalala.}
    \KwOut{Blalalala}


    \For{$t = 1$ \text{to} $T$}{

        Fit $\Psi_{t+1}$ to $\mathcal{H}_{t}$\\ 
        $\mathbf{x} \leftarrow \argmax_{\mathbf{x} \in \mathcal{X}} a(\mu (\Psi_{t+1} (\mathbf{x})) ,\sigma(\Psi_{t+1} (\mathbf{x})) , J^{\text{min}}  )$\\
        Check $c=J(\mathbf{x})$\\
        $\mathcal{H}_{t+1} \leftarrow \mathcal{H}_{t} \cup \left\lbrace (\mathbf{x},c) \right\rbrace $\\
        \If{ $c (\mathbf{x}) < c^{\text{min}} $}{


            $\mathbf{x}^{\text{min}},c^{\text{min}} \leftarrow \mathbf{x},c $


        }
    }
    \Return $\mathbf{x}^{\text{min}}$

    \caption{Testing}


\end{algorithm}

\end{document}
6
  • I assume that you need the changes from mathmode to textmode that often, don't you? Did you try to do this algorithm all in one mathmode (defining/substituting the necessary commands, …)?
    – Nepumuk
    Commented Sep 30, 2019 at 7:14
  • It cannot be in mathmode since the word “min” has to be in textmode
    – Stephen
    Commented Sep 30, 2019 at 7:26
  • What \begin{math} before \For and \end{math} after \caption gives me. Maybe this helps you further. This probably has something to do with the \If statement.
    – Nepumuk
    Commented Sep 30, 2019 at 7:33
  • 2
    @Delan, why dont' you replace your \text{min} with the already existing \min operator which seems to provide the desired result ?
    – BambOo
    Commented Sep 30, 2019 at 9:03
  • @BambOo I tried it and it prints min with an accent on i
    – Stephen
    Commented Sep 30, 2019 at 10:04

2 Answers 2

5

The correct command is \mathrm in every case.

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[ruled]{algorithm2e}

\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}

\begin{document}

\begin{algorithm}
\DontPrintSemicolon 

\KwIn{Blalalala.}
\KwOut{Blalalala}

\For{$t = 1$ to $T$}{
  Fit $\Psi_{t+1}$ to $\mathcal{H}_{t}$\\ 
  $\mathbf{x} \leftarrow \argmax_{\mathbf{x} \in \mathcal{X}} 
    a(\mu (\Psi_{t+1} (\mathbf{x})) , \sigma(\Psi_{t+1} (\mathbf{x})) , J^{\textup{min}})$\\
  Check $c=J(\mathbf{x})$\\
  $\mathcal{H}_{t+1} \leftarrow \mathcal{H}_{t} \cup \lbrace (\mathbf{x},c) \rbrace $\\
  \If{ $c (\mathbf{x}) < c^{\textup{min}} $}{
    $\mathbf{x}^{\textup{min}},c^{\textup{min}} \leftarrow \mathbf{x},c$
  }
}
\Return $\mathbf{x}^{\textup{min}}$

\caption{Testing}
\end{algorithm}

\end{document}

You'd get the same result with \textup. The problem is that \text picks up the font that's current when the formula starts, which is italic in the argument to \If.

enter image description here

Side note. Your MWE cannot produce “mín” from \min. That happens if you load babel-spanish, for instance.

2

At least, I got a workaround.

\documentclass{article}
\usepackage{amsmath,amssymb}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator*{\argmax}{arg\,max}
\usepackage[ruled]{algorithm2e}
\begin{document}
    \begin{algorithm}
        \DontPrintSemicolon
        \KwIn{Blalalala.}
        \KwOut{Blalalala}
        \For{$t = 1$ to $T$}{
            Fit $\Psi_{t+1}$ to $\mathcal{H}_{t}$\\
            $\mathbf{x}\leftarrow \argmax_{\mathbf{x}\in\mathcal{X}} a(\mu (\Psi_{t+1} (\mathbf{x})),\sigma(\Psi_{t+1} (\mathbf{x})) , J^\text{min})$\\
            Check $c=J(\mathbf{x})$\\
            $\mathcal{H}_{t+1} \leftarrow \mathcal{H}_{t} \cup \left\lbrace(\mathbf{x},c)\right\rbrace$\\
            \If{ $c (\mathbf{x}) < c^\mathrm{min} $}{
                $\mathbf{x}^\text{min},c^\text{min} \leftarrow \mathbf{x},c $
            }
        }
        \Return $\mathbf{x}^\text{min}$
        \caption{Testing}
    \end{algorithm}
\end{document}

Surely, there are fonts which make a difference between \text{} and \mathrm{} visible to the naked eye, but for this example, I cannot distinguish my output "min"s whether it was created with the further or the latter.

3
  • Great! Could you please add a screenshot?
    – Stephen
    Commented Sep 30, 2019 at 7:50
  • 1
    There is a problem and I want to find out what’s going on with my OP. Your workaround looks good but yet, one can tell the difference between \text and yours
    – Stephen
    Commented Sep 30, 2019 at 7:58
  • @Delan I used latexmk -pdf -synctex=1 mwe.tex for this.
    – Nepumuk
    Commented Sep 30, 2019 at 8:18

You must log in to answer this question.

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