![如何在必须换行时自动保持缩进级别](https://linux22.com/image/274254/%E5%A6%82%E4%BD%95%E5%9C%A8%E5%BF%85%E9%A1%BB%E6%8D%A2%E8%A1%8C%E6%97%B6%E8%87%AA%E5%8A%A8%E4%BF%9D%E6%8C%81%E7%BC%A9%E8%BF%9B%E7%BA%A7%E5%88%AB.png)
我正在使用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
):
然而,进一步调试后,我发现以下组合有效:
cam-thesis
和pdfLaTeX
:article
和XeLaTeX
:article
和pdfLaTeX
:
以上三个选项都可以正常工作,但我确实需要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}}