# LaTeX Math Mode in algorithmic environment

The following code works fine:

\documentclass{article}
\usepackage{algpseudocode,amsmath}

\begin{document}
\begin{algorithmic}[1]
\State \Call{MergeSort}{$T(1,\dotsc,n)$}
\end{algorithmic}
\end{document}


However, if I replace $...$ with $$...$$, I get an error for some reason. So the following code does not work:

\documentclass{article}
\usepackage{algpseudocode,amsmath}

\begin{document}
\begin{algorithmic}[1]
\State \Call{MergeSort}{$$T(1,\dotsc,n)$$}
\end{algorithmic}
\end{document}


I thought $...$ and $$...$$ are equivalent. Why can this be happening? Thank you.

The \Call macro uses \ifthenelse, which has a very handy feature: it allows combining tests with propositional logic connectives (called \AND and \OR) with parentheses for the stating the precedence. Here's the catch! The symbols for these parentheses are $$ and $$. So the presence of $$ and $$ in the second argument of \Call (that is used in an \ifthenelse{\equal{#2}{}} test during processing if \Call) confuses TeX, because at the time of this evaluation $$ and $$ aren't the math formulas delimiters, but rather the “propositional logic parentheses”.

So, use \$.

Alternative solution:

\documentclass{article}
\usepackage{algpseudocode,amsmath,xifthen}

\algrenewcommand\Call[2]{\textproc{#1}\ifthenelse{\isempty{#2}}{}{(#2)}}%

\begin{document}

\begin{algorithmic}[1]
\State \Call{MergeSort}{$$T(1,\dotsc,n)$$}
\end{algorithmic}
\end{document}


This works because xifthen introduces a new \isempty test that doesn't interpret its argument. Of course also other algpseudocode macros would need similar treatment.

• seems that the $$...$$ conflict should be mentioned in the algpseudocode documentation (algorithmicx bundle). doesn't seem to be, so maybe someone could report it to the author. – barbara beeton Feb 19 '15 at 15:22