2

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}
  • yoour MWE doesn't work even in article document class since it seems that some packages are missing. – Zarko Jun 6 '17 at 18:06
  • You were right, the mathtools package was missing. Now it compiles in article. – aaragon Jun 6 '17 at 18:32
5

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

  • 2
    can you indicate changes in your solution in comparison to your question? – Zarko Jun 6 '17 at 20:33
  • I prefer to use a minipage and play with the width. – John Kormylo Jun 7 '17 at 13:10
  • Note: this does not work when loading the algorithm package (\usepackage{algorithm}) and wrapping the algorithmic environment \begin{algorithmic}...\end{algorithmic} in a algorithm environment \begin{algorithm}...\end{algorithm}. Basically makes the solution unworkable for me :( – lowndrul Jun 4 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.