使用 Latex 的算法选项

使用 Latex 的算法选项

这是我的算法:

 \usepackage{algorithm}
 \usepackage{algorithmic}
 \begin{document}
 \begin{algorithm}[H]
 \caption{My algo}
 \begin{algorithmic}[1]
 \STATE instruction 1
 \STATE instruction 2
 \end{algorithmic}
 \end{algorithm}
 \end{document}

我如何指定哪条指令已应用此选项:[1]在此处输入图片描述

我想删除“Début”和“Fin”之前的数字,还想在“ch”和“po”之前添加空格

答案1

可选参数algorithmic[1]提供了编号为每一个行。使用 可获得模 2 的编号[2],依此类推。

以下是是否有可能连接环线(如algorithm2ealgorithmic- 使用algpseudocode(来自algorithmicx) - 插入首次亮相作为关键词。其他的还需要翻译成法语:

在此处输入图片描述

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx

\makeatletter
\renewcommand{\ALG@name}{Algorithme}% Algorithm in French
\algnewcommand{\Debut}{\Statex \textbf{D\'ebut}\pushindent}
\algnewcommand{\Fin}{\Statex \textbf{Fin}\popindent}

% This is the vertical rule that is inserted
\def\therule{\makebox[\algorithmicindent][l]{\hspace*{.5em}\vrule height .75\baselineskip depth .25\baselineskip}}%

\newtoks\therules% Contains rules
\therules={}% Start with empty token list
\def\appendto#1#2{\expandafter#1\expandafter{\the#1#2}}% Append to token list
\def\gobblefirst#1{% Remove (first) from token list
  #1\expandafter\expandafter\expandafter{\expandafter\@gobble\the#1}}%
\def\LState{\State\unskip\the\therules}% New line-state
\def\pushindent{\appendto\therules\therule}%
\def\popindent{\gobblefirst\therules}%
\def\printindent{\unskip\the\therules}%
\def\printandpush{\printindent\pushindent}%
\def\popandprint{\popindent\printindent}%

%      ***      DECLARED LOOPS      ***
% (from algpseudocode.sty)
\algdef{SE}[WHILE]{While}{EndWhile}[1]
  {\printandpush\textbf{Tant que} #1}
  {\popandprint\textbf{Fin Tant que}}%
\algdef{SE}[FOR]{For}{EndFor}[1]
  {\printandpush\algorithmicfor\ #1\ \algorithmicdo}
  {\popandprint\algorithmicend\ \algorithmicfor}%
\algdef{S}[FOR]{ForAll}[1]
  {\printindent\algorithmicforall\ #1\ \algorithmicdo}%
\algdef{SE}[LOOP]{Loop}{EndLoop}
  {\printandpush\algorithmicloop}
  {\popandprint\algorithmicend\ \algorithmicloop}%
\algdef{SE}[REPEAT]{Repeat}{Until}
  {\printandpush\algorithmicrepeat}[1]
  {\popandprint\algorithmicuntil\ #1}%
\algdef{SE}[IF]{If}{EndIf}[1]
  {\printandpush\algorithmicif\ #1\ \algorithmicthen}
  {\popandprint\algorithmicend\ \algorithmicif}%
\algdef{C}[IF]{IF}{ElsIf}[1]
  {\popandprint\pushindent\algorithmicelse\ \algorithmicif\ #1\ \algorithmicthen}%
\algdef{Ce}[ELSE]{IF}{Else}{EndIf}
  {\popandprint\pushindent\algorithmicelse}%
\algdef{SE}[PROCEDURE]{Procedure}{EndProcedure}[2]
   {\printandpush\algorithmicprocedure\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicprocedure}%
\algdef{SE}[FUNCTION]{Function}{EndFunction}[2]
   {\printandpush\algorithmicfunction\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}%
   {\popandprint\algorithmicend\ \algorithmicfunction}%
\makeatother

\begin{document}

\begin{algorithm}
  \caption{My algorithme}
  \begin{algorithmic}[1]
    \Debut
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState $b\gets r$
        \LState $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \LState \textbf{Retour} $b$\Comment{The gcd is b}
    \Fin
  \end{algorithmic}
\end{algorithm}
\end{document}

\State提供“常规线路”(带号码),同时\Statex提供未编号的行。因此,我定义了两者首次亮相属于\Statex


添加宏

\def\LStatex{\Statex\unskip\the\therules}% New line-state

允许您打印未编号的行。现在您可以按如下方式使用它:

在此处输入图片描述

\begin{algorithm}
  \caption{My algorithme}
  \begin{algorithmic}[1]
    \Debut
      \LState $r\gets a\bmod b$
      \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \LState $a\gets b$
        \LState Here is a very long expression
        \LStatex \expandafter\hskip\algorithmicindent\relax that has to go to a new line.
        \LState $r\gets a\bmod b$
      \EndWhile\label{euclidendwhile}
      \LState \textbf{Retour} $b$\Comment{The gcd is b}
    \Fin
  \end{algorithmic}
\end{algorithm}

相关内容