使用独立子文件时算法包出现错误

使用独立子文件时算法包出现错误

我正在尝试创建一个独立的子文件,并在其中添加一些算法。

当我在序言中使用时,文档编译正确 \documentclass[9pt]{article}。但是当我将其更改为

\documentclass[float=false, crop=false]{standalone}
\usepackage[subpreambles=true]{standalone}`

错误输出:

ERROR: Extra }, or forgotten \endgroup.
--- TeX said ---
\@endfloatbox ...pagefalse \outer@nobreak \egroup                                                          \color@endbox 
l.28 \end{algorithm}

--- HELP ---
From the .log file...

I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

完整代码:

%\documentclass[float=false, crop=false]{standalone}
%\usepackage[subpreambles=true]{standalone}

\documentclass[9pt]{article} 
\usepackage{import}
\usepackage{booktabs}
\usepackage{siunitx}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}

\begin{center}
\begin{algorithm}{}
    \caption{Euclid’s algorithm}
    \label{euclid}
    \begin{algorithmic}[1] % The number tells where the line numbering should start
        \Procedure{Euclid}{$a,b$} \Comment{The g.c.d. of a and b}
            \State $r\gets a \bmod b$
            \While{$r\not=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 gcd is b}
        \EndProcedure
    \end{algorithmic}
\end{algorithm}
\end{center}
\end{document}

编辑

通过替换 documentclass 中的浮动布尔值解决了问题,\documentclass[float=true, crop=false]{standalone}但我不确定为什么。

答案1

由于独立版没有预定义宽度,因此正常的文本断行不适用。您需要将所有内容放入 minipage 中或使用 varwidth 包。其他内容未定义,或者在独立版中不是个好主意。

\documentclass[varwidth]{standalone}
\usepackage[noend]{algpseudocode}
\begin{document}
    \begin{algorithmic}[1] % The number tells where the line numbering should start
        \Procedure{Euclid}{$a,b$} \Comment{The g.c.d. of a and b}
            \State $r\gets a \bmod b$
            \While{$r\not=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}% won't work
            \State \textbf{return} $b$\Comment{The gcd is b}
        \EndProcedure
    \end{algorithmic}
\end{document}

您应该这样在文档中使用它。顺便说一句,不要将浮动元素放在中心环境中。

\documentclass[9pt]{article} 
\usepackage{import}
\usepackage{booktabs}
\usepackage{siunitx}

\usepackage{algorithm}
%\usepackage[noend]{algpseudocode}% not needed
\usepackage{graphicx}
\begin{document}

\begin{algorithm}
    \caption{Euclid’s algorithm}
    \label{euclid}
    \includegraphics{test5}% insert your filensmae here
\end{algorithm}
\end{document}

演示

答案2

通过替换 documentclass 中的浮动布尔值解决问题\documentclass[float=true, crop=false]{standalone}

相关内容