[Tex/LaTex] How to resolve the error: “Missing { inserted.” inside an algorithm2e

algorithm2e

When I use ShareLatex or MikTex to compile the following LaTeX code:

\begin{algorithm}
    \uIf{$|F_d(D'(C_{\hat{i}})) - F_d(D(C_{\hat{i}}))| > \sigma$ OR numSeqNoise > $\mu$}{
      SplitCluster($C_j$)\;
    } 
    \uElseIf{$|F_d(D'(C_{\hat{i}})) - F_d(D(C_{\hat{i}}))| > \tau$}{
      numSeqNoise++\;
    } 
    \uElseIf{$j \neq \hat{i}$}{
        Remove $S_{R}$ from cluster $C_j$\;
        Place $S_{R}$ in cluster $C_\hat{i}$\;
     }\uElse{
        Replace $S_{R}$ in cluster $C_\hat{i}$\;
     }
\end{algorithm}

it compiles normally, but when I try to use WriteLatex, then the following error always happens:

Missing { inserted.
<to be read again>
            \gdef
l.485          }
\uElse{

The line where the error is pointed out is }\uElse{.

Somebody can help me? 😉

Best Answer

You need to use C_{\hat{i}} instead of C_\hat{i}, because \hat doesn't open a group.

Typographical advice: Because \hat{i} is too high and looks weird, LaTeX provides a dotless i \imath.

enter image description here

It is favourable to use \hat{\imath} instead of \hat{i}. I replaced every occurrence in the example below.

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
  \uIf{$|F_d(D'(C_{\hat{\imath}})) - F_d(D(C_{\hat{\imath}}))| > \sigma$ OR $\mathrm{numSeqNoise} > \mu$}{
    SplitCluster($C_j$)\;
  } 
  \uElseIf{$|F_d(D'(C_{\hat{\imath}})) - F_d(D(C_{\hat{\imath}}))| > \tau$}{
    numSeqNoise++\;
  } 
  \uElseIf{$j \neq \hat{\imath}$}{
    Remove $S_{R}$ from cluster $C_j$\;
    Place $S_{R}$ in cluster $C_{\hat{\imath}}$\;
  }\uElse{
    Replace $S_{R}$ in cluster $C_{\hat{\imath}}$\;
  }
\end{algorithm}
\end{document}

enter image description here

Related Question