如何在必须换行时自动保持缩进级别

如何在必须换行时自动保持缩进级别

我正在使用algpseudocode带有两个自定义命令的包,\Let并且\LongState本着以下精神处理(断开的)长行的自动缩进沃纳回答,用于\parbox包装长行的内容。

但是,当缩进级别超过 1 时,以下方法效果不佳。以下是示例:

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{calc}

% A command for defining assignments within the algorithmic environment which
% supports automatic indentation when the second argument is too long to fit
% on one line
\newcommand*{\Let}[2]{\State #1 $\gets$
\parbox[t]{\linewidth-\algorithmicindent-\widthof{ #1 $\gets$}}{#2\strut}}
% A \State command that supports automatic indentation when the argument's
% content is too long to fit on one line
\newcommand*{\LongState}[1]{\State
\parbox[t]{\linewidth-\algorithmicindent}{#1\strut}}

\begin{document}

\begin{algorithm}
\caption{This is some testing pseudo-code showing what happens with nested long
lines}

\begin{algorithmic}[1]
  \Function{test}{$(x, y)$}
    \Let{$a$}{some math expression}
    \Let{$b$}{some very very long expression that doesn't fit on one line and is
    even longer and longer}
    \For{each $e$ in a list}
        \Let{$l(e)$}{the length of element $e$}
        \If{some condition on $l(e)$}
            \LongState{run some complex sub-routine and get the result and this
            description is very very long, long indeed...}
            \Let{$a$}{some math expression}
            \Let{$b$}{some very very long expression that doesn't fit on one
            line and is even longer and longer}
            \If{some other condition}
                \Let{$c$}{another math expression}
            \EndIf
        \EndIf
    \EndFor
  \EndFunction
\end{algorithmic}

\end{algorithm}

\end{document}

以下内容呈现为: 在此处输入图片描述

第 3 行是一条断开的长线的示例,而第 7 行和第 9 行则延伸得太远了。

编辑:我最初在撰写博士论文时遇到了这个问题凸轮理论类和 XeLaTeX。我的设置是 Fedora 17,带有 TeXLive 2013,由因德里奇·诺维

如果我使用Gonzalo Medina 的回答并将文档的类别更改为\documentclass{cam-thesis}(以下是 MWE):

\documentclass{cam-thesis}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{calc}
\usepackage{linegoal}

% A command for defining assignments within the algorithmic environment which
% supports automatic indentation when the second argument is too long to fit
% on one line
\newcommand*{\Let}[2]{\State #1 $\gets$
\parbox[t]{\linegoal}{#2\strut}}
% A \State command that supports automatic indentation when the argument's
% content is too long to fit on one line
\newcommand*{\LongState}[1]{\State
\parbox[t]{\linegoal}{#1\strut}}

\begin{document}

\begin{algorithm}
\caption{This is some testing pseudo-code showing what happens with nested long
lines}

\begin{algorithmic}[1]
  \Function{test}{$(x, y)$}
    \Let{$a$}{some math expression}
    \Let{$b$}{some very very long expression that doesn't fit on one line and is
    even longer and longer}
    \For{each $e$ in a list}
        \Let{$l(e)$}{the length of element $e$}
        \If{some condition on $l(e)$}
            \LongState{run some complex sub-routine and get the result and this
            description is very very long, long indeed...}
            \Let{$a$}{some math expression}
            \Let{$b$}{some very very long expression that doesn't fit on one
            line and is even longer and longer}
            \If{some other condition}
                \Let{$c$}{another math expression}
            \EndIf
        \EndIf
    \EndFor
  \EndFunction
\end{algorithmic}

\end{algorithm}

\end{document}

我得到以下(cam-thesis带有XeLaTeX): 在此处输入图片描述

然而,进一步调试后,我发现以下组合有效:

  1. cam-thesispdfLaTeX在此处输入图片描述

  2. articleXeLaTeX在此处输入图片描述

  3. articlepdfLaTeX在此处输入图片描述

以上三个选项都可以正常工作,但我确实需要cam-thesis使用XeLaTeX

答案1

一个选项是使用\linegoal(来自linegoal包)作为 es 的宽度\parbox\linegoal扩展到该行剩余部分的尺寸:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{calc}
\usepackage{linegoal}

% A command for defining assignments within the algorithmic environment which
% supports automatic indentation when the second argument is too long to fit
% on one line
\newcommand*{\Let}[2]{\State #1 $\gets$
\parbox[t]{\linegoal}{#2\strut}}
% A \State command that supports automatic indentation when the argument's
% content is too long to fit on one line
\newcommand*{\LongState}[1]{\State
\parbox[t]{\linegoal}{#1\strut}}

\begin{document}

\begin{algorithm}
\caption{This is some testing pseudo-code showing what happens with nested long
lines}

\begin{algorithmic}[1]
  \Function{test}{$(x, y)$}
    \Let{$a$}{some math expression}
    \Let{$b$}{some very very long expression that doesn't fit on one line and is
    even longer and longer}
    \For{each $e$ in a list}
        \Let{$l(e)$}{the length of element $e$}
        \If{some condition on $l(e)$}
            \LongState{run some complex sub-routine and get the result and this
            description is very very long, long indeed...}
            \Let{$a$}{some math expression}
            \Let{$b$}{some very very long expression that doesn't fit on one
            line and is even longer and longer}
            \If{some other condition}
                \Let{$c$}{another math expression}
            \EndIf
        \EndIf
    \EndFor
  \EndFunction
\end{algorithmic}

\end{algorithm}

\end{document}

在此处输入图片描述

附录

使用XeLaTeX 和cam-thesis文档类(问题中出现了该类的链接)塔尼兹,存在问题\linegoal;标准类中不存在此问题(请参阅对问题的编辑)。快速的解决方法是使用较短的值:

\documentclass{cam-thesis}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{calc}
\usepackage{linegoal}

% A command for defining assignments within the algorithmic environment which
% supports automatic indentation when the second argument is too long to fit
% on one line
\newcommand*{\Let}[2]{\State #1 $\gets$
\parbox[t]{-52,29pt+\linegoal}{#2\strut}}
% A \State command that supports automatic indentation when the argument's
% content is too long to fit on one line
\newcommand*{\LongState}[1]{\State
\parbox[t]{-52,29pt+\linegoal}{#1\strut}}

\begin{document}

\begin{algorithm}
\caption{This is some testing pseudo-code showing what happens with nested long
lines}

\begin{algorithmic}[1]
  \Function{test}{$(x, y)$}
    \Let{$a$}{some math expression}
    \Let{$b$}{some very very long expression that doesn't fit on one line and is
    even longer and longer}
    \For{each $e$ in a list}
        \Let{$l(e)$}{the length of element $e$}
        \If{some condition on $l(e)$}
            \LongState{run some complex sub-routine and get the result and this
            description is very very long, long indeed...}
            \Let{$a$}{some math expression}
            \If{some condition on $l(e)$}
            \Let{$b$}{some very very long expression that doesn't fit on one
            line and is even longer and longer}
            \If{some other condition}
                \Let{$c$}{another math expression}
              \EndIf
            \EndIf
        \EndIf
    \EndFor
  \EndFunction
\end{algorithmic}
\end{algorithm}

\end{document}

我建议您与作者/维护者联系以cam-thesis评论此问题。

答案2

用这个命令替换以前的 LongState 命令对我的 journal 和 pdflatex 都有效。

一切顺利。

\newcommand*{\LongState}[1]{\State
\parbox[t]{\linewidth-\algorithmicindent-\algorithmicindent}{#1\strut}}

相关内容