3

How to make a "normal" looking ampersand symbol within the algorithm environment?

I'm using \& but this results in a strange looking character as shown below:

enter image description here

Here is a minimal example how I use the ampersand

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ruled,linesnumbered]{algorithm2e}

\begin{document}
    \begin{algorithm}[H]
      \If{$b$ \& $0x01$} {
        $p \leftarrow p \oplus a$\;  
      }
    \end{algorithm}
\end{document}
Razer
  • 133
  • 2
    Welcome to TeX.SE. Without seeing the code that produced the screenshot you've posted, it's not going to be easy to offer a clear diagnosis, let alone offer a fix. At any rate, what you call a "strange looking character" is simply an italic-mode ampersand symbol. Please show us the code that generated the line in question. – Mico Mar 10 '16 at 16:55
  • That's a fancy algorithm! ;) – Paulo Cereda Mar 10 '16 at 17:01
  • Does the line in question look like if $b$ \emph{\&} $0x01$ then? If so, remove the \emph instruction. – Mico Mar 10 '16 at 17:06
  • I added a minimal example how I use the amptersand – Razer Mar 10 '16 at 20:04

2 Answers2

5

That's just how an italic ampersand looks in Computer Modern, the font you're using. If you don't like it, force it to be upright: \textup{\&}. Alternatively, you can use \textsl{\&} to get a slanted look, which is like the upright version, but skewed to look similar to italic text:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ruled,linesnumbered]{algorithm2e}

\begin{document}
    \begin{algorithm}[H]
      \If{$b$ \textup\& $0x01$ or $b$ \textsl\& $0x80$} {
        $p \leftarrow p \oplus a$\;  
      }
    \end{algorithm}
\end{document}

output

wrtlprnft
  • 3,859
3

If you plan to use & as a math symbol, it is convenient to define it as such, when it appears in a math formula:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[ruled,linesnumbered]{algorithm2e}

\DeclareMathSymbol{\mathampersand}{\mathrel}{operators}{`&}
\let\textampersand\&
\DeclareRobustCommand{\&}{%
  \ifmmode
    \mathampersand
  \else
    \textampersand
  \fi
}

\begin{document}

\begin{algorithm}[H]
\caption{Foo \& bar}

\If{$b \& \mathtt{0x01}$} {
  $p \leftarrow p \oplus a$\;
}
\end{algorithm}

\end{document}

enter image description here

egreg
  • 1,121,712