[Tex/LaTex] standalone does not work with algorithms

algorithmicxalgorithmsalgpseudocodestandalone

I have a series of algorithms for which I would like to create independent pdf files I can add later. I tried to use the standalone package but somehow these are not compatible. This is the MWE:

\documentclass[border=1pt]{standalone}
%\documentclass{article}

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}
\usepackage{algorithmicx}
\usepackage{mathtools}


\newcommand*\Let[2]{\State #1 $\gets$ #2}
\newcommand*\Leth[2]{\State #1 $\gets$ #2}
\newcommand*\Stateh{\State}

\newcommand{\inlinecomment}[1]{{\color{gray} \it-- #1}}

\begin{document}

\begin{algorithm}[H]
  \caption{Mesh-discontinuity interaction
    \label{alg:interact}}
  \begin{algorithmic}[0]
    \Require{A mesh $\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ (node and element sets) and a discontinuity set $\mathcal{D}$}
    \Statex
    \Function{interact}{$\mathcal{M}, \mathcal{D}$}
      \For{$e_i \in \mathcal{E}$} 
        \For{$d_i \in \mathcal{D}$} 
          \Let{$ \mathcal{I} $}{$e_i  \cap d_i$} 
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\phantom{w_ k }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \text{s(} \mathcal{I}, e_i\text{)}$}  
            \If{$\text{isC}(d_i)$}
            \Let{$\mathcal{E}$}{$\mathcal{E} \cup \text{c}(\mathcal{I}, d_i)$}  
            \EndIf
            \For {$ n_k \in \mathcal{I}$} 
              \Let{$ \mathcal{N}$}{$\mathcal{N} \bigcup \left\{n_{k}, n_{k} \right\}$} 
              \Let{$ w_k \equiv \left\{ w_{\psi}, w_{\chi} \right\} $}{$\text{w} \left( k \right) $} 
            \EndFor
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$w, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{algorithm}


\end{document}

Best Answer

For those who may be interested in the solution, this is the code I ended up using:

\documentclass[preview,border=5pt]{standalone}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{algpseudocode}
\usepackage{mathtools}

\newcommand*\Let[2]{\State #1 $\gets$ #2}

\begin{document}

  \begin{algorithmic}[0]
    \Require{A mesh $\mathcal{M} := \left\{ \mathcal{N}, \mathcal{E} \right\}$ (node and element sets) and a discontinuity set $\mathcal{D}$}
    \Statex
    \Function{interact}{$\mathcal{M}, \mathcal{D}$}
      \For{$e_i \in \mathcal{E}$} 
        \For{$d_i \in \mathcal{D}$} 
          \Let{$ \mathcal{I} $}{$e_i  \cap d_i$} 
          \If{$\mathcal{I} \neq \emptyset $} 
            \Let{$\phantom{w_ k }\mathllap{\mathcal{E}}$}{$\mathcal{E} \cup \text{s(} \mathcal{I}, e_i\text{)}$}  
            \If{$\text{isC}(d_i)$}
            \Let{$\mathcal{E}$}{$\mathcal{E} \cup \text{c}(\mathcal{I}, d_i)$}  
            \EndIf
            \For {$ n_k \in \mathcal{I}$} 
              \Let{$ \mathcal{N}$}{$\mathcal{N} \bigcup \left\{n_{k}, n_{k} \right\}$} 
              \Let{$ w_k \equiv \left\{ w_{\psi}, w_{\chi} \right\} $}{$\text{w} \left( k \right) $} 
            \EndFor
          \EndIf
        \EndFor
      \EndFor
      \State \Return{$w, \mathcal{N}, \mathcal{E}$}
    \EndFunction
  \end{algorithmic}
\end{document}

which produced

enter image description here

Related Question