[Tex/LaTex] getting this “Missing $ inserted” error in an algorithm figure just like all the other algorithm figures

algorithmsenvironmentserrors

I'm writing a paper in LaTeX, and I'm getting a strange "Missing $ inserted" error when I try to compile the paper using pdflatex, but only if it contains the following code.

\begin{algorithm*}[tbh]
  \begin{haskell}
  translate :: \textsc{Featherweight Java} \Rightarrow \textsc{Feather-Sub terms}\\
  translate (class C_1 extends C_2 \{T_i f_i ^ \{i \in 1...n\}\; K M_j ^ \{j \in 1...m\}\}) = \hsbody{\langle C_1 = \{(translate C_2), f_i \: (translate T_i) ^ \{i \in 1...n\}\} \rangle where (translate K) and (translate M_j ^ \{j \in 1...m\}) are emitted as separate translations into the same unit}\
  translate (C(C_i f_i) \{ super(f_i); this.f_i = f_i; \} ^ \{i \in 1...n\}) = \hsbody{\hslet{C = \lambda f_i.\langle C = \{l_i = f_i\} ^ \{i \in 1...n\}}{rest of translation unit}}\
  translate (C' m(C_i x_i ^ \{i \in 1...n\}) \{ return e; \} in class C) = \hslet{ m = \lambda t.case t of \langle C=this \rangle \Rightarrow \lambda x_i.(translate ((C')e)) for each subclass C' of C with overriding method m' \langle C' = this \rangle \Rightarrow m' t x_i}{rest of translation unit}\
  translate x = x

  translate t.f \hswhere{t is known to have type C} = case t of \langle C = x \rangle x.f for each subclass C' of C \langle C' = x \rangle x.f\
  translate t_1.m_i ^ \{i \in 1...n\} = m (translate t_1) (translate t_i) ^ \{i \in 1...n \} \
  translate (new C(t_i ^ \{i \in 1...n\})) = C (translate t_i) ^ \{i \in 1...n \} \
  translate ((C)t) = \hslet{t' = (translate t)}{\langle C = \{for each f_i : \tau_i \in (translate C) f_i = t'.f_i\} \rangle}
  \end{haskell}
\caption{Encoding of Featherweight Java into Feather Sub\label{fig:Encoding-of-Featherweight}}
\end{algorithm*}

I already have \usepackage commands in my prelude for the algorithm* and haskell environments, and I use them earlier in the paper. This algorithm is listed in an appendix section, could that change things somehow? What can I do to fix it?

I would really appreciate any help I can get with this, as the submission deadline is this Friday! Everything else about the paper is ready for submission except this, so, once again, would really appreciate any help.

Best Answer

This is a common problem and as the comments suggest, it's because of all the _ characters. It's a pain to escape all of them. The way I handle this is to include the underscore package. That takes care of the _ in the normal mode. This way, you can keep your algo text as it is.

Related Question