在算法中左对齐并重置数字

在算法中左对齐并重置数字

我正在使用算法包,我想让某些文本左对齐。此外,我想在某些文本之后重置算法中每行的编号。MWE 提供了以下代码来说明这个问题:

 \documentclass{article}
 \usepackage[utf8]{inputenc}
 \usepackage{amsmath}
 \usepackage{amsfonts}
 \usepackage{amssymb}
 \usepackage{graphicx}
 \usepackage{algorithmic}
 \usepackage{algorithm}
 \usepackage{setspace}

 \begin{document}
 \begin{algorithm}
 \caption{algorithm}
 \begin{spacing}{1}
 \begin{algorithmic}[1]

  % I want the below text to be left 
  % "Some text before the logic:"
   \FOR{ any condition z}
     \STATE apply logic.
   \ENDFOR 

  % I want the below text to be left 
  % "Some text before the logic:"

  % Here I want the numbering to start from 1 again
  \FOR{ any condition x }
    \STATE apply logic.
  \ENDFOR 
  \end{algorithmic}
  \end{spacing}
  \end{algorithm}
  \end{document}

答案1

像这样?你可以完全破坏algorithmic环境 ;)

 \documentclass{article}
 \usepackage[utf8]{inputenc}
 \usepackage{amsmath}
 \usepackage{amsfonts}
 \usepackage{amssymb}
 \usepackage{graphicx}
 \usepackage{algorithmic}
 \usepackage{algorithm}
 \usepackage{setspace}

\newcommand\algotext[1]{\end{algorithmic}#1\begin{algorithmic}[1]}

 \begin{document}
 \begin{algorithm}
 \caption{algorithm}
 \begin{spacing}{1}
 \begin{algorithmic}[1]

  % I want the below text to be left 
  % "Some text before the logic:"
   \FOR{ any condition z}
     \STATE apply logic.
   \ENDFOR 

  % I want the below text to be left 
  \algotext{Some text before the logic:}

  % Here I want the numbering to start from 1 again
  \FOR{ any condition x }
    \STATE apply logic.
  \ENDFOR 
  \end{algorithmic}
  \end{spacing}
  \end{algorithm}
  \end{document}

相关内容