latex 小白 algorithmic already defined的原因

本文探讨了在使用LaTeX撰写算法时遇到的algorithmic环境冲突问题,详细解释了algorithmicx, algorithmic和algorithm包之间的关系及如何避免冲突,提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给小白的。这个latex错误没有关系,让她在那就行了。

因为algorithmic和algorithm package出来之前,大家是用algorithmicx这个package的,algorithmicx里面就定义了algorithmic环境,而新的algorithmic和algorithm里面也有algorithmic环境,所以两个定义的冲突了。

为什么不直接卸载algorithmicx这个package?因为其他比较老的package依赖它。我卸载过,结果很多必须的package不能用,没办法还得装回来。

详见:https://tex.stackexchange.com/questions/26921/command-algorithmic-already-defined

所以如果你的编辑器报了algorithmic already defined的错,你可以:

1.只用\usepackage{algorithm}  
\usepackage{algorithmicx}  
\usepackage{algpseudocode}三兄弟。

语法示例如下:

\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm  
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm  

% Server & Local action
\begin{algorithm}[H]
  	\caption{ Server Action}
  	\label{alg:Server Action}
  	\begin{algorithmic}[1]
  		\Require $D_i$,$f_i$,$key_i$\\
  		\Ensure	global model or $f_i$ itself
  		\State wait until each $D_i$ is sent
  		\State $D_i=Decrypt(D_{i}^{'})$
  		\State $D$=Aggregate($D_1$,$D_2$,$D_3$,...,$D_n$)
  		\State F=Train($D_{train}$)
  		\State meanwhile
  		\State wait until every $f_i$ is sent and F training process is finished.
  		\For{$i=0$;$i<n$;$i++$}
  		\State calculate $error_i$
  		\EndFor
  		\State calculate $error_F$
		\If {$error_i$<$error_F$}
  		\Return F to $client_i$;
  		\Else \
  		\Return $f_i$ to $client_i$;
  		\EndIf
  	\end{algorithmic}
\end{algorithm}

尽管我用的tex studio还是会在\State  \EndIf这些明亮上红色标出,但是编译已经没有错误的输出了,可以exit normally。

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值