Graphics, Figures & TablesMissing $ inserted error while using algorithm2e in Texshop

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
jay_singh
Posts: 9
Joined: Tue Jul 14, 2020 6:27 pm

Missing $ inserted error while using algorithm2e in Texshop

Post by jay_singh »

Hello friends,
I'm trying to type an algorithm in Latex while using Algorithm2e. I have defined a function, but on compiling I am running into missing dollar inserted error frequently. Here is my script;

\begin{algorithm}[H]
\SetAlgoLined
\DontPrintSemicolon
\KwIn{$ arr$,$result$}
\KwOut{$ result$ }

\SetKwFunction{FMain}{nonoverlap}
\SetKwProg{Fn}{Function}{:}{}
\Fn{\FMain{$x$,$result$}}{
temp=x[0] \;

\ForEach{$idx$, $element$ \in enumerate($x[1:,],1$)}{
\uIf{\text{element}[0] $>$ temp[1]}{
$temp.append(element)$ \;
}
\uElseIf{
nonoverlap($x[idx:]$,$[]$)
}

}
result.append(temp) \;
\textbf{return} result \;
}
\textbf{End Function}
\caption{Algorithm for Identifying Non-Overlapping subarrays}
\label{NagetionAlgo}
\end{algorithm}


The error is being pointed at this line;

\textbf{End Function}

Kindly advise if I'm missing something here. Help is appreciated.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Bartman
Posts: 366
Joined: Fri Jan 03, 2020 2:39 pm

Missing $ inserted error while using algorithm2e in Texshop

Post by Bartman »

The command \in requires the math mode.

I also made a few changes to the example that, in my opinion, make sense.

Code: Select all

\documentclass[a4paper,11pt]{article}
\usepackage{algorithm2e}

\SetAlCapSkip{1em}

\begin{document}
\begin{algorithm}
\SetAlgoLined
\DontPrintSemicolon
\KwIn{$arr,result$}
\KwOut{$result$}

\SetKwFunction{FMain}{nonoverlap}
\SetKwProg{Fn}{Function}{:}{End~Function}
\Fn{\FMain{$x,result$}}{
    temp=x[0] \;

    \ForEach{$idx, element \in enumerate(x[1:,],1$)}{
        \uIf{element[0] $>$ temp[1]}{
            $temp.append(element)$ \;
        }
        \uElse{% \uElseIf replaced
            nonoverlap($x[idx:],[]$)
        }
    }
    result.append(temp) \;
    \Return result \;
}
\caption{Algorithm for Identifying Non-Overlapping subarrays}
\label{NagetionAlgo}
\end{algorithm}
\end{document}
jay_singh
Posts: 9
Joined: Tue Jul 14, 2020 6:27 pm

Missing $ inserted error while using algorithm2e in Texshop

Post by jay_singh »

Thanks for your help. It works
Post Reply