我正在写一篇论文,我想使用这个algorithmic
环境。问题是,如果我使用以下代码:
\begin{algorithmic}[H]
\label{alg1}
\FOR{$-M_x<m_x<M_x$}
\FOR{$-M_y<m_y<M_y$}
\FOR{$-M_z<m_z<M_z$}
\FOR{$0\leq q\leq 1$}
\FOR{$0\leq j\leq 1$}
\FOR{$0\leq k\leq 1$}
\STATE $1$
\STATE $2$
\STATE $3$
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\end{algorithmic}
该算法可以从一列开始,中断,然后在另一列继续:
我发现解决这个问题的唯一方法是添加一个图形环境:
\begin{figure}
\begin{algorithmic}[H]
\caption{Algorithm}\label{alg1}
\FOR{$-M_x<m_x<M_x$}
\FOR{$-M_y<m_y<M_y$}
\FOR{$-M_z<m_z<M_z$}
\FOR{$0\leq q\leq 1$}
\FOR{$0\leq j\leq 1$}
\FOR{$0\leq k\leq 1$}
\STATE $1$
\STATE $2$
\STATE $3$
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{figure}
问题是这和我的数字编号冲突了。我也试过使用,\begin{figure*}
但这样我就无法引用算法。
有没有办法让算法保持流畅并避免弄乱图形编号?
答案1
我可以为您提供一个algorithmic*
环境;标题必须指定为参数。
\documentclass[twocolumn]{article}
\usepackage{algorithm,algorithmic,etoolbox}
\usepackage{lipsum}
\makeatletter
\newenvironment{algorithmic*}[1]
{%
% remove the rule below
\par\vspace{-\bigskipamount}
\appto{\fs@ruled}{\def\@fs@post{}}
\begin{algorithm}[H]
\caption{#1}
\end{algorithm}\nobreak\vspace{-\medskipamount}
\begin{algorithmic}
}
{\end{algorithmic}\par\nobreak\kern2pt\hrule\bigskip}
\makeatother
\begin{document}
\lipsum[1-2]\lipsum[3][1-3]
\bigskip
\begin{algorithmic*}{Algorithm\label{alg1}}
\FOR{$-M_x<m_x<M_x$}
\FOR{$-M_y<m_y<M_y$}
\FOR{$-M_z<m_z<M_z$}
\FOR{$0\leq q\leq 1$}
\FOR{$0\leq j\leq 1$}
\FOR{$0\leq k\leq 1$}
\STATE $1$
\STATE $2$
\STATE $3$
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\end{algorithmic*}
\lipsum[1-3]
\end{document}
答案2
添加algorithm
包裹对于你的序言-它提供了algorithm
浮动环境和它自己的相关计数器。
\documentclass[twocolumn]{article}
\usepackage{algorithm,algcompatible}
\usepackage{lipsum}
\begin{document}
\sloppy\lipsum[1-2]
\begin{algorithm}
\caption{Algorithm}
\begin{algorithmic}
\FOR{$-M_x < m_x < M_x$}
\FOR{$-M_y < m_y < M_y$}
\FOR{$-M_z < m_z < M_z$}
\FOR{$0 \leq q \leq 1$}
\FOR{$0 \leq j \leq 1$}
\FOR{$0 \leq k \leq 1$}
\STATE $1$
\STATE $2$
\STATE $3$
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\ENDFOR
\end{algorithmic}
\end{algorithm}
\lipsum[3-5]
\end{document}