如何引用命令 AlgHeader 中的计数器?

如何引用命令 AlgHeader 中的计数器?

首先,一个计数器AlgCounter首先,定义

\AlgNo其次,定义一个新的命令,并AlgCounter在定义中使用计数器\AlgNo计数器。

第三,为了画出算法的三条线,\AlgHeader定义了一个新的命令,并\AlgNo使用了\AlgHeader

问题是如何在命令中引用计数器\AlgHeader

\documentclass{article}
\usepackage{algpseudocode}
\usepackage{amsfonts}

\newcounter{AlgCounter}
\newcommand{\AlgNo}{\refstepcounter{AlgCounter}\textbf{\theAlgCounter}}

\newcommand{\AlgHeader}[1]{ 
\noindent\rule{\textwidth}{0.7pt}\\
\noindent\underline{
    \makebox[0.98\textwidth][l]{
        \textbf{ Algorithm \AlgNo}: #1}
    }   
}

\newcommand{\AlgFooter}{\noindent\rule[\baselineskip]{\textwidth}{0.7pt}}

\begin{document}

    \AlgHeader{name1}\label{alg:nl1}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello world! 
    \end{algorithmic}
    \AlgFooter

    Algorithm \ref{alg:nl1} is ...

    \AlgHeader{name1}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello Java!  
    \end{algorithmic}
    \AlgFooter

    Algorithm \ref{alg:nl1} is ...

\end{document}

答案1

我会使用algorithm环境;通过定义新的浮动样式,可以处理对默认外观的细微更改。

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsfonts}

% define a new float style (based on the “ruled” style)
\makeatletter
\newcommand\floatc@dowtia[2]{{\@fs@cfont #1}: #2\par}
\newcommand\fs@dowtia{%
  \def\@fs@cfont{\bfseries}%
  \let\@fs@capt\floatc@dowtia
  \def\@fs@pre{\hrule height.7pt depth0pt \kern2pt}%
  \def\@fs@post{\kern2pt\hrule height 0.7pt\relax}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue
}
\makeatother

\floatstyle{dowtia}
\restylefloat{algorithm}

\begin{document}

\begin{algorithm}[h]
\caption{name1}\label{alg:nl1}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello world! 
    \end{algorithmic}
\end{algorithm}

Algorithm \ref{alg:nl1} is ...

\begin{algorithm}
\caption{name2}\label{alg:nl2}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello Java!  
    \end{algorithmic}
\end{algorithm}

Algorithm \ref{alg:nl2} is ...

\end{document}

在此处输入图片描述

对于长算法,我们可以重现环境的工作algorithm,因此我们将有一个统一的外观。

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{etoolbox}

% define a new float style
\makeatletter
\newcommand\floatc@dowtia[2]{{\@fs@cfont #1}: #2\par}
\newcommand\fs@dowtia{%
  \def\@fs@cfont{\bfseries}%
  \let\@fs@capt\floatc@dowtia
  \def\@fs@pre{\hrule height.7pt depth0pt \kern2pt}%
  \def\@fs@post{\kern2pt\hrule height 0.7pt\relax}%
  \def\@fs@mid{\kern2pt\hrule\kern2pt}%
  \let\@fs@iftopcapt\iftrue
}
% define a longalgorithm environment
\newenvironment{longalgorithm}[1][]
 {% the optional argument is ignored
  \par\addvspace{\bigskipamount}
  \hrule height .7pt depth 0pt \kern2pt
  \def\@captype{algorithm}%
  \def\@fs@cfont{\bfseries}%
  \def\@fs@capt##1##2{{\@fs@cfont ##1}: ##2\par}%
  \def\caption{\refstepcounter\@captype\@dblarg{\float@caption\@captype}}%
  \BeforeBeginEnvironment{algorithmic}{\unvbox\@floatcapt}%
  \def\ALG@endalgorithmic{\par\nobreak\kern2pt\hrule height 0.7pt\kern 2pt}%
 }
 {\addvspace{\bigskipamount}}
\makeatother

\floatstyle{dowtia}
\restylefloat{algorithm}

\begin{document}

\begin{algorithm}[h]
\caption{name1}\label{alg:nl1}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello world! 
    \end{algorithmic}
\end{algorithm}

Algorithm \ref{alg:nl1} is ...

\begin{longalgorithm}
\caption{name2}\label{alg:nl2}
    \begin{algorithmic} 
        \Require{some input}
        \Ensure{some output}
        \State Hello Java!  
    \end{algorithmic}
\end{longalgorithm}

Algorithm \ref{alg:nl2} is ...

\end{document}

在此处输入图片描述

您可以一直使用longalgorithm,但对于短的来说,如果它们是浮点数就更好了。

相关内容