Algorithm environment with Figure Caption

4,989 views
Skip to first unread message

Orkun S.

unread,
Apr 23, 2014, 1:48:29 PM4/23/14
to latexus...@googlegroups.com
Hi all,

I am using algorithm environment but I want figure caption under it instead of Algorithm Caption. Below is a sample code of my concern.

%\begin{algorithm}
%\caption{Simulated Annealing}
%\label{Fig:Figure1}
%\begin{algorithmic}
%\STATE Solve problem 1
%\FOR{$i=1,..,m$}
%\STATE Solve problem 2
%\IF{solution of problem 1 is greater than problem 2}
%\STATE Stop!
%\ENDIF
%\ENDFOR
%\end{algorithmic}
%\end{algorithm}

Thus, I want it to look just like an algorithm but I only want figure caption. How can I achieve this?

Thanks!

Werner Grundlingh

unread,
Apr 23, 2014, 2:11:18 PM4/23/14
to latexus...@googlegroups.com
Either use the capt-of package to set a caption of a figure inside the algorithm floating environment, or use a figure float directly:

Inline image 1

\documentclass{article}
\usepackage{algorithm,algorithmic}
\usepackage{capt-of}
\begin{document}
\begin{algorithm}[ht]
  \captionof{figure}{Simulated Annealing}\label{alg:Figure1}
  \begin{algorithmic}
    \STATE Solve problem 1
    \FOR{$i = 1, \ldots, m$}
      \STATE Solve problem 2
      \IF{solution of problem 1 is greater than problem 2}
        \STATE Stop!
      \ENDIF
    \ENDFOR
  \end{algorithmic}
\end{algorithm}

\begin{figure}[ht]
  \caption{Simulated Annealing}\label{fig:Figure1}
  \begin{algorithmic}
    \STATE Solve problem 1
    \FOR{$i = 1, \ldots, m$}
      \STATE Solve problem 2
      \IF{solution of problem 1 is greater than problem 2}
        \STATE Stop!
      \ENDIF
    \ENDFOR
  \end{algorithmic}
\end{figure}

\end{document}


--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to latexusersgro...@googlegroups.com.
To post to this group, send email to latexus...@googlegroups.com.
Visit this group at http://groups.google.com/group/latexusersgroup.
For more options, visit https://groups.google.com/d/optout.

Orkun S.

unread,
Apr 23, 2014, 2:38:05 PM4/23/14
to latexus...@googlegroups.com
Thank you so much for your help! I think the first one is a better fit for me, but I need caption to be under it (i.e., under the line). How can I do that?

Werner Grundlingh

unread,
Apr 23, 2014, 2:55:58 PM4/23/14
to latexus...@googlegroups.com
In that case I suggest using a figure environment straight-up and modify algorithmic so that it provides the rules you're after (naturally, algorithm formats the float using the float package's ruled style). Placement of the caption below the algorithm is achieved by physically placing it below the algorithmic environment.

Inline image 1

\documentclass{article}
\usepackage{algorithmic}
\usepackage{letltxmacro}
\LetLtxMacro{\oldalgorithmic}{\algorithmic}
\LetLtxMacro{\endoldalgorithmic}{\endalgorithmic}
\renewenvironment{algorithmic}[1][0]{%
  \hrulefill\par
  \oldalgorithmic[#1]}
  {\endoldalgorithmic\par
   \vspace*{-.5\baselineskip}
   \hrulefill\par
  }

\begin{document}

\begin{figure}[ht]
  \begin{algorithmic}
    \STATE Solve problem 1
    \FOR{$i = 1, \ldots, m$}
      \STATE Solve problem 2
      \IF{solution of problem 1 is greater than problem 2}
        \STATE Stop!
      \ENDIF
    \ENDFOR
  \end{algorithmic}
  \caption{Simulated Annealing}\label{fig:Figure1}
\end{figure}

\end{document}


--

Orkun S.

unread,
Apr 23, 2014, 3:02:56 PM4/23/14
to latexus...@googlegroups.com
Dear Werner Grundlingh,

Thank yo so much for your time and help! It works perfectly!

On Wednesday, April 23, 2014 12:48:29 PM UTC-5, Orkun S. wrote:
Reply all
Reply to author
Forward
0 new messages