I'm looking for a way to add region comments to an algorithm. A comment should appear left of the listing and mark the corresponding code region by, for example, a large curly bracket. Like summaries for specific code regions.

Edit 1: See this image. Like that but with the curly bracket and the comment left of the code.

Edit 2: Here's the link to the corresponding question for the image from edit1.

I would like to see it as some kind of command to be easily reusable.

Do you have any idea how to achieve something like this?

  • Please give the poster time to respond before voting to close. – Andrew Swann Nov 4 '14 at 12:08
  • Have to give it a try before I can tell whether this is what I am looking for. I'll get back here later. – Hendrik Wiese Nov 4 '14 at 12:26
  • Alright, it's almost what I want... Aside from one thing: I need the comments relate to a region, not a single line. And the relation should be visualized by a curly bracket. Like in this image (sorry for stealing): i.stack.imgur.com/UHUJY.png but with the bracket and the comment left of the code. – Hendrik Wiese Nov 4 '14 at 21:32
  • @HendrikWiese: Any reason why you did not just link to the question instead of linking to an image? That image sure looks familiar :-). Linking to the question might be useful as a starting point to a solution for you, especially since you did not provide a MWE](meta.tex.stackexchange.com/questions/228) – Peter Grill Nov 5 '14 at 0:03
  • 1
    I think the real answer here is going to depend on how you want to specify the comments. If it is inline with the code then the solution in Source Code Margin Comments will work. If you prefer to specify them outside of the code then that is a different thing all together. The point of a MWE is to set up the problem for someone trying to help. Just include a sample listing and how you want to actually specify where the comments go. Furthermore, are you referring to a listing, or algorithm type of environments? – Peter Grill Nov 5 '14 at 0:43

One way to do this is to use \alglinenumber to automagicaly insert a \tkizmark at each line, and then after the algorithm you can insert comments via \AddNote. So, if you want to add a comment for lines 1 thru 5, and lines 7-9 you would use:

\AddNote[blue]{1}{5}{Comments for lines 1--5.}
\AddNote[red]{7}{9}{Comments for lines 7--9.}

enter image description here





\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

    %% #1 = draw options
    %% #2 = top line number to start comment from
    %% #3 = bottom line number where comment ends
    %% #4 = text of comment
    \begin{tikzpicture}[overlay, remember picture]
        \draw [decoration={brace,amplitude=0.5em},decorate,ultra thick,red, #1]
            ($(#3)+(\HorizontalOffset,-\VerticalOffset)$) --  ($(#2)+(\HorizontalOffset,\VerticalOffset)$)
            node [align=left, text width=\SpaceReservedForComments-1.0em, pos=0.5, anchor=east] {#4};

\makeatletter% Add a \tkizmark for each line so we can reference it later

\algnewcommand{\algorithmicgoto}{\textbf{go to}}%
  \caption{Euclid’s algorithm}\label{euclid}
    \Procedure{Euclid}{$a,b$} \Comment{The g.c.d. of a and b}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}  \Comment{We have the answer if r is 0} \label{marker}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \State \textbf{return} $b$\Comment{The gcd is b}
    \State \Goto{marker}
% ---------------
% Now insert all to comments that we desire on specific line numbers:
\AddNote[blue]{1}{5}{Comments for lines 1--5.}
\AddNote[red]{7}{9}{Comments for lines 7--9.}
% ---------------

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