伪代码:算法包将所有图形框起来

伪代码:算法包将所有图形框起来

为了编写伪代码,我决定使用这里不幸的是,这个包改变了我所有图形的样式,如下所示:

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

我希望我的标准图形没有这些水平线,并在图形下方有一个标题,但我的算法就像1

这可能吗?

提前致谢 :-)

更新: 以下是 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}

我发现我的问题出在这几行:\usepackage{float} \restylefloat{figure}

我使用是因为我想让我的图形固定在我定义它们的位置。

答案1

我发现了一个非常简单的解决方法。

% 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

\usepackage如果我改变调用的顺序,它就会起作用

答案2

你似乎正在关注错误的建议

algorithm包裹algorithm使用定义浮动环境float包裹。因此无需float再次加载。其次,为了使图形保持“定义的位置”,您需要调整规格\floatplacement,而不是浮动的样式(通过\floatstyle\restylefloat):

在此处输入图片描述

\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}

请注意,在我的示例中,我没有强制将ERE 与环境figure放在一起。确保[H]figure\floatplacement{figure}{H}全部人物。

答案3

不要重新设置figure环境样式,这也会影响其布局。让带标题的图形保持在原位的更简单方法是加载包capt-of(基本上是一行包)并使用命令\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}

在此处输入图片描述

\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}

相关内容