3

Let's say we have a procedure in which we want to iterate over an object whose data type we want to specify by writing it in texttt font. I tried

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}

\begin{procedure}
      \For{$x\in\texttt{myArray}$}{
        $\texttt{MyArray}\gets\texttt{MyArray}.\text{append}(x)$} 
    \caption{MyProcedure()}
\end{procedure}\DecMargin{1em}

\end{document}

Output of the Code

However, \texttt{MyArray} looks different in the for loop and in the body of the loop. How can we make it look like in the body of the loop both times? I already tried to put it outside of math mode, but it did not change.

2 Answers 2

7

You could use \mathtt:

enter image description here

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}

\begin{procedure}
      \For{$x\in\mathtt{myArray}$}{
        $\mathtt{MyArray} \gets \mathtt{MyArray}.\text{append}(x)$} 
    \caption{MyProcedure()}
\end{procedure}\DecMargin{1em}

\end{document}
2
  • 1
    I'm going to accept this answer as it is the more elegant one. Jan 6 at 20:12
  • @ItoroJóska It's not only more elegant. It's the most correct.
    – egreg
    Jan 6 at 20:39
5

You could switch to upright font:

\documentclass{article}

\usepackage{algorithm2e}
\usepackage{amsmath}

\begin{document}

\begin{procedure}
      \For{$x\in$ \upshape\texttt{myArray}}{
        \texttt{MyArray} $\gets$ \texttt{MyArray}.append(x)} 
    \caption{MyProcedure()}
\end{procedure}\DecMargin{1em}

\end{document}

enter image description here

1
  • 1
    No, it should be \mathtt.
    – egreg
    Jan 6 at 17:06

You must log in to answer this question.

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