Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

Correctly formatting LaTeX-based pseudocode with the ∀ (for all) symbol

Here’s a methodology for using the ‘for all’ symbol (∀) in LaTeX so that subsequent lines of pseudocode are correctly indented.

I recently needed to write some pseudocode in LaTeX that incorporated the ∀ (turned A) symbol, but it proved to be rather difficult. What I required was the treatment of my “for all” in the same manner as a For loop (ie indented lines). To develop my code, I used the algorithmicx (including algpseudocode) and amsmath packages in Visual Studio Code with the LaTeX workshop extension. My preamble was as follows:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{amsmath}

To illustrate the problem I faced, I will use Algorithm 2 from Breen & Jones (1996).

LaTex contains a \ForAll function, but it prints the words “for all” and offers no opportunity to use the ∀ symbol. I was also opposed to using functions such as \quad to manually indent individual lines.

Here is the code using \ForAll:

\begin{document}
\begin{algorithm}
    \caption{Nonregional Maxima Suppression}\label{eq1}
    \text Explanation of variables: \(\textbf{f}\), input gray-scale image; \(\textbf{g}\), output image; \(\textbf{f(x)}\), gray-scale image at position \(\textbf{x}\); \(\textbf{F}\), domain of support for \(\textit{f}$, \(\textbf{mval}\), the minimum allowed value of \(\textit{f}\).
    \begin{algorithmic}[1]
    \State \(g \leftarrow F\)
    \ForAll {\(x \in F\)}
    \If {\(g(x) \neq mval\)}
        \If {\(a\)}
            \State \(g(z) \leftarrow mval, \forall{z} \in \Gamma_x \{w: g(w) = f(x)\} \)
        \EndIf
    \EndIf
    \EndFor
    \end{algorithmic}
\end{algorithm}

And here is the result with the words “for all” printed:

The solution was to develop a new command, via the following code:

\algnewcommand\algorithmicforeach{$\forall$}
\algdef{S}[FOR]{ForEach}[1]{\algorithmicforeach #1 \algorithmicdo}

The first line specifies a new function called “foreach”, which will be indicated with the ∀ symbol (\forall in LaTeX). The second line specifies how it is to be formatted.

The code is then changed to have a \ForEach argument, which concludes with \EndFor:

\begin{algorithm}
    \caption{Nonregional Maxima Suppression}\label{eq2}
    \text Explanation of variables: \(\textbf{f}$, input gray-scale image; \(\textbf{g}\), output image; \(\textbf{f(x)}\), gray-scale image at position \(\textbf{x}\); \(\textbf{F}\), domain of support for \(\textit{f}\), \(\textbf{mval}\), the minimum allowed value of \(\textit{f}\).
    \begin{algorithmic}[1]
    \State \(g \leftarrow F\)
    \ForEach {\(x \in F\)}
    \If {\(g(x) \neq mval\)}
        \If {\(a\)}
            \State \(g(z) \leftarrow mval, \forall{z} \in \Gamma_x \{w: g(w) = f(x)\} \)
    \EndIf
\EndIf
\EndFor
\end{algorithmic}

The result is as desired:

   

Comments

No comments have yet been submitted. Be the first!

Have Your Say

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.