latex算法换行缩进问题?

latex算法换行缩进问题?

在使用 Latex 语法编写算法时,我遇到了缩进问题,下面的新行2号线没有对齐2号线

在此处输入图片描述

我们可以看到 'container ct' 超出了为了

这是乳胶代码:

\documentclass[conference]{IEEEtran}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath}
\begin{document}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

\begin{algorithm}[h]
    \caption{CC-WFDLBP}
    \label{alg:wfd}
    \begin{algorithmic}[1]
        \Require
        $CC$: Container Cluster to be placed,
        $H$: Set of Hosts
        \Ensure
        Placement Strategy $P$
        \For{Container $ct$ $\in$ $CC$}
            \State compute the dominant resource $dr$ by the current container $ct$
        \State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
        \State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
        \State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
        \EndFor
    \end{algorithmic}
\end{algorithm}
\end{document}

答案1

algpseudocodex包可以解决你的问题。

另外,为了更符合IEEE的风格要求,我还修改了一些风格,给出了两种风格。

\documentclass[conference]{IEEEtran}
\usepackage{algorithm}
\floatplacement{algorithm}{tbp}
\makeatletter
\newcommand{\algorithmname}{\ALG@name}
\renewcommand{\floatc@ruled}[2]{{\@fs@cfont #1:} #2\par}
\makeatother
\usepackage[noEnd=false]{algpseudocodex}
\tikzset{algpxIndentLine/.style={draw=black}}
\algrenewcommand{\alglinenumber}[1]{\bfseries\footnotesize #1}
\algrenewcommand{\textproc}{}
\begin{document}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\algrenewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

\begin{algorithm}[h]
    \caption{CC-WFDLBP}
    \label{alg:wfd}
    \begin{algorithmic}[1]
        \Require
        $CC$: Container Cluster to be placed,
        $H$: Set of Hosts
        \Ensure
        Placement Strategy $P$
        \For{Container $ct$ $\in$ $CC$}
            \State compute the dominant resource $dr$ by the current container $ct$
        \State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
        \State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
        \State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
        \EndFor
    \end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

\documentclass[conference]{IEEEtran}
\usepackage{algorithm}
\floatplacement{algorithm}{tbp}
\makeatletter
\newcommand{\algorithmname}{\ALG@name}
\renewcommand{\floatc@ruled}[2]{{\@fs@cfont #1:} #2\par}
\makeatother
\usepackage{algpseudocodex}
\tikzset{algpxIndentLine/.style={draw=black}}
\algrenewcommand{\alglinenumber}[1]{\bfseries\footnotesize #1}
\algrenewcommand{\textproc}{}
\begin{document}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}  % Use Input in the format of Algorithm
\algrenewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm

\begin{algorithm}[h]
    \caption{CC-WFDLBP}
    \label{alg:wfd}
    \begin{algorithmic}[1]
        \Require
        $CC$: Container Cluster to be placed,
        $H$: Set of Hosts
        \Ensure
        Placement Strategy $P$
        \For{Container $ct$ $\in$ $CC$}
            \State compute the dominant resource $dr$ by the current container $ct$
        \State compute Polak-Ribiere parameter $\beta_k=\frac{g_k^{T}(g_k-g_{k-1})}{\parallel g_{k-1} \parallel^{2}}$;
        \State compute the conjugate directions $d_k=-g_k+\beta_k d_{k-1}$;
        \State compute the step size $\alpha_k=s/\parallel d_k \parallel_{2}$;
        \EndFor
    \end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容