Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In the "algorithm" environment, when a function call spans multiple lines, I would like to have its arguments aligned, like so:

MyFunction(long_arg_name0,
           long_arg_name1,
           long_arg_name2);

How can I achieve this effect?

(second) EDIT: Here's a minimal example that fails to achieve the desired effect:

\documentclass{article}

\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}

\begin{algorithm}[t]
\begin{algorithmic}
  \While{true}
  \State MyLongWindedFunction(LongArgument0,\\ LongArgument1,\\ LongArgument2,\\ LongArgument3)
  \EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}

When I stick the standard alignment marker & between the arguments, I get the error message:

! Misplaced alignment tab character &.
l.10   \State MyLongWindedFunction(LongArgument0,&
                                               \\ LongArgument1, &\\ Lon...
share|improve this question
    
Please post a complete, minimal and compilable (including the document class, the relevant packages used) version of your code. –  Gonzalo Medina Mar 24 '11 at 19:33
    
Ok, compilable example added. –  SuperElectric Mar 24 '11 at 20:29

1 Answer 1

up vote 3 down vote accepted

One possible solution would be to calculate the length of the string that preceeds the alignment point (in this case, the string MyLongWindedFunction( ) and then use this length to add the necessary horizontal space:

\documentclass{article}
\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{algorithm}

\newlength\mylen
\settowidth\mylen{MyLongWindedFunction(}

\begin{document}

\begin{algorithm}[t]
\begin{algorithmic}
  \State MyLongWindedFunction(LongArgument0,\\\hspace{\mylen}LongArgument1,\\\hspace{\mylen}LongArgument2,\\\hspace{\mylen}LongArgument3)
\end{algorithmic}
\end{algorithm}

\end{document}

To answer the EDIT in the original question: simply add \parindent (the indentation used in the algorithm) to the length \mylen:

\documentclass{article}

\usepackage{algorithmicx}
\usepackage{algpseudocode}
\usepackage{algorithm}

\newlength\mylen
\settowidth\mylen{MyLongWindedFunction(}
\addtolength\mylen{\parindent}

\begin{document}

\begin{algorithm}[t]
\begin{algorithmic}
  \While{true}
  \State MyLongWindedFunction(LongArgument0,\\\hspace*{\mylen}LongArgument1,\\\hspace*{\mylen}LongArgument2,\\\hspace*{\mylen}LongArgument3)
  \EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}
share|improve this answer
    
I just discovered that this solution doesn't quite work when MyLongWindedFunction is within a loop block, since the function itself is indented a few spaces away from the left, a spacing not captured by \settowidth\mylen{MyLongWindedFunction(}. I've changed the example code above to include such a loop. Any workaround ideas would be greatly appreciated! –  SuperElectric Mar 28 '11 at 18:50
    
@SuperElectric: I added some code showing you how to proceed; all you have to do is to add the length of \parindent to \mylen. –  Gonzalo Medina Mar 28 '11 at 19:57
    
Yep, that worked. Thanks! –  SuperElectric Mar 30 '11 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.