In the paper "Beautiful Code Compelling Evidence", by J.R. Heard — Code is formated in a particular way which includes margin comments. I am looking for a way to do this as I think it is a very clear way to comment comment code in a literate programming style.
[Tex/LaTex] Source Code Margin Comments
commentslistingsmarginpar
Related Question
- [Tex/LaTex] texcl, escapeinside, and single character comments, with listings package
- [Tex/LaTex] Best practice for writing comments in LaTeX documents
- [Tex/LaTex] Including LaTeX documentation as comments in source code
- [Tex/LaTex] comment package: Show/hide comments Inside a section title
- [Tex/LaTex] way to toggle comments
Best Answer
As mentioned already in the comments you can use
escapeinside
option oflstlisting
(listings
package) to execute own code inside a code listing. There are alsoexecutebegin
andexecuteend
to insert code automatically before and after such markers.My idea: * Use
minipages
to restrain the listing to less than the text width and move it to the right using some horizontal space. * A separation line like in the linked paper can be added using theframe=l
option. * Use the above commands to add comments at the beginning of code lines. * The automatic added code places the comments to the left using\llap{<content>\hspace{<distance>}}
without taking up any official space. * To allow multi-line comments an extraminipage
with correct alignment is added around the comments.The code would look like this: (first listing only supports single line comments while the second supports multi-line comments!)
This looks as follows:
If this is used more often a new environment could be defined (
\newlstenvironment
) or the settings can be set globally.Note: In theory this could be hacked deeper into the
listings
code to allow the comment to be added at the end instead of the start. This would simplify the adding of comments but would be much more difficult to code.