I am currently studying algorithmic complexity of some programs, I have to deal with lines and lines of code and it would be really nice and helpful if I could wrap some of those lines in a right (or left) curly bracket and note down the complexity of said lines (math formulas) next to it.

I am using the listings package because in my opinion it does a fantastic job in showing code but to be honest I am not such a latex expert and that is why so far I have got only nasty errors and nothing done. I hope you can help me sort this out.

Edit: Here it is what I would like to achieve on paper: enter image description here

  • I think this is quite difficult, but it might help if you posted an image of what you were looking for. For example, is the bracketed material part of a larger listing? or just a code snippet with a margin note? Do you require syntax highlighting, or not? – Steven B. Segletes Oct 28 '13 at 14:08
  • @StevenB.Segletes Done! Thank you for taking interest! – haunted85 Oct 28 '13 at 16:42
  • 1
    Please note the edit I made to my answer. The \Shortstack needs a [l] optional argument in order to left-align the code. – Steven B. Segletes Oct 28 '13 at 19:05

I'd be the first to admit that this structure may be too cumbersome for what you hope to achieve, but I'll present it anyway. What we do here is stick different blocks of verbatim code into named macros (these macros are boxes that contain the given code lines). If we just regurgitate the named macros in sequence (UNMARKED CODE in the MWE), we recover the source code, though note that being a box, a macro will not break across a page.

But then I created a \labelcode{}{} macro, that takes as its first argument a macro that regurgitates a code box, and as its second argument the label to apply to that block. If there is no nesting, it is quite straightforward: just provide the macro name associated with the particular myverbbox environment. If the labels are to be nested, however, you must use \savestack to stuff the inner code plus labels into a new box, and then apply \labelcode on the saved stack macro.

I tried to structure the layout of \begin{myverbbox} and \end{myverbbox} to not interfere with the flow of the code, but I will note that the blank spaces prior to \end{mybox} are part of the verb box and will affect the horizontal positioning of any label applied.

\parindent 0in
\parskip 1em
mt Partition (A, start, end)      \end{myverbbox}
   pivot = A[start]
   i = start - 1
   j = end + 1          \end{myverbbox}
            i = i + 1
         until A[i] >= pivot
            j = j - 1
         until A[j] <= pivot      \end{myverbbox}%
         if i < j then
           exchange(A[i], A[j])   \end{myverbbox}
      until (i >=j)               \end{myverbbox}
   RETURN j                       \end{myverbbox}



enter image description here

  • Steven, regarding your note (in the fourth version of this post), I believe no spaces are removed, but tabsize is 4. If, in your editor/IDE, tabs don't get replaced by spaces, and if you use a tabsize other than 4, copying your code from there and pasting it here will mangle the indentation. – jub0bs Feb 9 '14 at 12:04
  • @Jubobs Thanks for pointing this out. I'll see if I can reconstitute a working answer. – Steven B. Segletes Feb 9 '14 at 17:20
  • @Jubobs I believe it is fixed now. Somehow, I think the presence of 3 successive backslashes in the code got converted into a single backslash during a copy paste. After returning the extra backslashes, I included a % to break it up between lines. Hopefully, all is now well. – Steven B. Segletes Feb 9 '14 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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