1

for writing pseudocode I decided to use the solution from here. Unfortunately, this package changes the style of all my figures defined like this:

\begin{figure}[H]
    \centering
    \includegraphics[height=4cm]{AKP13_command_dependency_graph}
    \caption{Command Dependency Graph}
    \label{fig:AKP13_command_dependency_graph}
\end{figure}

I would like to have my standard figures without these horizontal lines and with a caption under the figure but my Algorithms like in 1.

Is this possible?

Thanks in advance :-)

UPDATE: Here is a MWE:

\documentclass[twoside, openright, 12pt]{book}
%pseudocode
\usepackage{mathtools} 
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
% include graphics
\usepackage{graphicx}
\graphicspath{{./arbeit/figures/}}
% fix graphics on defined position
\usepackage{float}
\restylefloat{figure}

\begin{document}

\begin{figure}[H]
    \centering
    \includegraphics[height=4cm]{AKP13_command_dependency_graph}
    \caption{Command Dependency Graph}
    \label{fig:AKP13_command_dependency_graph}
\end{figure}

\begin{algorithm}
    \caption{Dependency Graph Assembly}\label{algo:AKP13_command_dependency_graph_assembly}
    \begin{algorithmic}[1]
        \Procedure{MyProcedure}{}
        \State $\textit{stringlen} \gets \text{length of }\textit{string}$
        \State $i \gets \textit{patlen}$
        \BState \emph{top}:
        \If {$i > \textit{stringlen}$} \Return false
        \EndIf
        \State $j \gets \textit{patlen}$
        \BState \emph{loop}:
        \If {$\textit{string}(i) = \textit{path}(j)$}
        \State $j \gets j-1$.
        \State $i \gets i-1$.
        \State \textbf{goto} \emph{loop}.
        \State \textbf{close};
        \EndIf
        \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
        \State \textbf{goto} \emph{top}.
        \EndProcedure
    \end{algorithmic}
\end{algorithm}

\end{document}

I figured out that my problem comes with these lines: \usepackage{float} \restylefloat{figure}

I use the because I want my figures to be fixed at the place where I defined them.

3
  • Hmmm... this is not the typical behaviour of the algorithm package. While it loads float, which can be used to restyle the presentation of any float, it only does so for algorithm. Can you provide a minimal example that replicates your current behaviour?
    – Werner
    Commented Mar 23, 2017 at 15:29
  • I don't understand why you need to restyle your figure float in order to have them be fixed at the place where you define them? That has nothing to do with the restyling of figure. Can you explain?
    – Werner
    Commented Mar 23, 2017 at 16:44
  • I took this solution from here: tex.lickert.net/tipps/optionh/optionh.html
    – lukasl1991
    Commented Mar 23, 2017 at 16:55

3 Answers 3

2

I've found an incredibly easy workaround.

% fix graphics on defined position
\usepackage{float}
\restylefloat{figure}

%pseudocode
\usepackage{mathtools} 
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother

It works if I change the order of the \usepackage calls

1

You seem to be following incorrect advice.

The algorithm package defines the algorithm float environment using the float package. So there's no need to load float again. Secondly, in order to keep figures "where they're defined", you need to adjust the \floatplacement specification, not the style of the float (via \floatstyle or \restylefloat):

enter image description here

\documentclass{book}

\usepackage{algorithm,graphicx}
\usepackage[noend]{algpseudocode}

\floatplacement{figure}{H}% Figures should always stay where they're defined.

\begin{document}

\begin{figure}
  \centering
  \includegraphics[height=4cm]{example-image}
  \caption{A figure}
\end{figure}

\begin{algorithm}
  \caption{An algorithm}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}\Comment{The g.c.d.\ of $a$ and $b$}
      \State $r \gets a \bmod b$
      \While{$r \neq 0$}\Comment{We have the answer if $r$ is $0$}
        \State $a \gets b$
        \State $b \gets r$
        \State $r \gets a \bmod b$
      \EndWhile\label{euclidendwhile}
      \State \textbf{return} $b$\Comment{The g.c.d.\ is $b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\end{document}

Note that in my example I don't force figure to be placed [H]ERE with the figure environment. \floatplacement{figure}{H} ensures that for all figures.

2
  • I now applied Gernots solution. But now I have the problem that algorithms are not placed where they were defined...
    – lukasl1991
    Commented Mar 23, 2017 at 17:26
  • @lukasl1991: You can then use \floatplacement{algorithm}{H} as well.
    – Werner
    Commented Mar 23, 2017 at 17:29
0

Don't restyle the figure environment, which also affects its layout. An easier way to have a captioned figure staying in place is to load the capt-of package (basically a one-line package) and to use the command \captionof{figure}{...}:

\usepackage{capt-of}
...
\begin{document}
...
\begin{center}
  ... Code of figure ...
  \captionof{figure}{...The caption of the figure ...}
  \label{...label to refer to figure ...}
\end{center}

enter image description here

\documentclass[twoside, openright, 12pt]{book}
%pseudocode
\usepackage{mathtools} 
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
% include graphics
\usepackage{graphicx}
\graphicspath{{./arbeit/figures/}}
% fix graphics on defined position
\usepackage{float}
%\restylefloat{figure}
\usepackage{capt-of}

\begin{document}

\begin{center}
    \includegraphics[height=4cm]{example-image}
    \captionof{figure}{Command Dependency Graph}
    \label{fig:AKP13_command_dependency_graph}
\end{center}

\begin{algorithm}
    \caption{Dependency Graph Assembly}\label{algo:AKP13_command_dependency_graph_assembly}
    \begin{algorithmic}[1]
        \Procedure{MyProcedure}{}
        \State $\textit{stringlen} \gets \text{length of }\textit{string}$
        \State $i \gets \textit{patlen}$
        \BState \emph{top}:
        \If {$i > \textit{stringlen}$} \Return false
        \EndIf
        \State $j \gets \textit{patlen}$
        \BState \emph{loop}:
        \If {$\textit{string}(i) = \textit{path}(j)$}
        \State $j \gets j-1$.
        \State $i \gets i-1$.
        \State \textbf{goto} \emph{loop}.
        \State \textbf{close};
        \EndIf
        \State $i \gets i+\max(\textit{delta}_1(\textit{string}(i)),\textit{delta}_2(j))$.
        \State \textbf{goto} \emph{top}.
        \EndProcedure
    \end{algorithmic}
\end{algorithm}

\end{document}
1
  • Thanks for your effort! This approach also works for me. :-)
    – lukasl1991
    Commented Mar 23, 2017 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .