How to automatically keep the indentation level when a line has to be broken
I'm using the algpseudocode package with two custom commands, \Let and
\LongState to handle automatic indentation of (broken) long lines in the
spirit of Werner's answer, which uses \parbox to wrap the long line's
content.
However, the following approach does not work well when the indentation
level is more than one. Here is an example:
\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}
The following renders as:
Line 3 is an example of a well broken long line, whereas lines 7 and 9
stretch out too far.
No comments:
Post a Comment