在算法中在行前添加空格

在算法中在行前添加空格

我需要在“气候”和“每个地区”前面添加缩进,以便溢出的行不会与新的伪代码行混淆。在此处输入图片描述我已经尝试了一切,包括(出于某种原因,algorithm2e 包对我来说不起作用)我尝试了通常的方法,例如\添加空格、$\thinmuskip$添加\indent缩进和其他方法,但都不起作用。我的 latex 代码如下

\documentclass{article}    
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{algorithm}
\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
initialisation: choose initial emission control rates $\boldsymbol{\mu^k}=[\mu^k(1) \ ... \ \mu^k(Tmax)]$ for each region $k \in 1,...,12$;
store initial $\boldsymbol{\mu} ^k$ in vector $CR$;

\For{\textup{each region $i \in 1,...,12$}}{{\nosemic find $\boldsymbol{\mu^i}$ that maximises $\sum_{t=1}^{Tmax} U[c^{i}(t), L^{i}(t)]R(t)$ subject to economic and \;
 climate equations, with all other $\boldsymbol{\mu} ^k$ fixed from $CR$;

replace the old value of $\boldsymbol{\mu^i}$ in $CR$ with the new optimal $\boldsymbol{\mu^i}$;
}
}
\While{\textup{|$\boldsymbol{\mu^i}-\boldsymbol{\mu^j}$|>threshold for some regions $i,j$}}{
{repeat \textbf{for} loop\;
}
}
\caption{Noncooperative Equilibrium for RICE}
\end{algorithm}

\end{document}

答案1

我仍然无法编译您的示例。但我想有一个快速解决您问题的方法:只需\phantom{for}在“climate”一词前面插入类似以下内容:

\documentclass{article}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{algorithm}
\begin{document}

\begin{algorithm}[H]
\SetAlgoLined
initialisation: choose initial emission control rates $\boldsymbol{\mu^k}=[\mu^k(1) \ ... \ \mu^k(Tmax)]$ for each region $k \in 1,...,12$;
store initial $\boldsymbol{\mu} ^k$ in vector $CR$;

\For{\textup{each region $i \in 1,...,12$}}{{\nosemic find $\boldsymbol{\mu^i}$ that maximises $\sum_{t=1}^{Tmax} U[c^{i}(t), L^{i}(t)]R(t)$ subject to economic and \;
 \phantom{for} climate equations, with all other $\boldsymbol{\mu} ^k$ fixed from $CR$;

replace the old value of $\boldsymbol{\mu^i}$ in $CR$ with the new optimal $\boldsymbol{\mu^i}$;
}
}
\While{\textup{|$\boldsymbol{\mu^i}-\boldsymbol{\mu^j}$|>threshold for some regions $i,j$}}{
{repeat \textbf{for} loop\;
}
}
\caption{Noncooperative Equilibrium for RICE}
\end{algorithm}

\end{document}

在此处输入图片描述

答案2

你可以这样做\SetAlgoHangIndent{1.5em}(或者你认为合适的任何长度)。我还建议进行其他更改,以避免笨拙的输入并保持一致性。

请注意,不要将algorithm2ealgorithm一起加载。

\documentclass{article}    
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{amsmath}

\begin{document}

\begin{algorithm}
\SetAlgoLined
\SetAlgoHangIndent{1.5em}
\SetArgSty{textnormal}
\textbf{initialisation:} choose initial emission control rates 
  $\boldsymbol{\mu}^k=[\mu^k(1) \dots \mu^k(T_{\mathrm{max}})]$
  for each region $k \in 1,\dots,12$; store initial
  $\boldsymbol{\mu}^k$ in vector $CR$;

\For{each region $i \in 1,\dots,12$}{%
  find $\boldsymbol{\mu}^i$ that maximises
  $\sum_{t=1}^{T_{\mathrm{max}}} U[c^{i}(t), L^{i}(t)]R(t)$ subject to economic and
  climate equations, with all other $\boldsymbol{\mu}^k$ fixed from $CR$;

  replace the old value of $\boldsymbol{\mu^i}$ in $CR$ with the new optimal
  $\boldsymbol{\mu}^i$;
}

\While{$|\boldsymbol{\mu}^i-\boldsymbol{\mu}^j|> \text{threshold}$ for some regions $i,j$}{%
{repeat \textbf{for} loop;}
}
\caption{Noncooperative Equilibrium for RICE}
\end{algorithm}

\end{document}

在此处输入图片描述

相关内容