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.

I am writting a book and I use the listings package to type some matlab code. I would like to highlight every code block with an arrow or whatever hanging in the margin at the first line. Moreover The code should be indented.

More specifically, I would like to define a listings environment, say 'matlabcode', so that the latex code

  \documentclass{article}
\usepackage{listings}
\usepackage{lipsum}

\lstnewenvironment{matlabcode}{}{}

\begin{document}
\lipsum[1]
\begin{matlabcode}
>> plot(sin(1:10))
more matlab code
more matlab code
\end{matlabcode}
\end{document}

gave something like

enter image description here

where the arrow might be a latex symbol, a pgf/tikZ picture, etc.

share|improve this question
2  
good question :) I made your snippet into a complete MWE- please consider doing this for future questions :) –  cmhughes Nov 22 '12 at 18:25
    
I notice that you have been a member for 2 years, but yet have hardly up voted and also have not accepted any answers. Please review your questions and if any answered you question should should How do you accept an answer?. That way we can consider them answered. –  Peter Grill Nov 23 '12 at 8:39
add comment

1 Answer

up vote 5 down vote accepted

You can alayws use tikz, and if you allow for an optional parameter to the matlabcode environment you can change the style as desired on a per use basis:

enter image description here

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usepackage{lipsum}

\newcommand{\Arrow}[1][]{\tikz [overlay, line width=3pt, red, ->,#1] \draw (0,0) -- (1,0);}%

\lstnewenvironment{matlabcode}[1][]{%
    \smash{\raisebox{-3.5ex}{\Arrow[xshift = -1.2cm,#1]}}%
}{}

\begin{document}
\lipsum[1]
\begin{matlabcode}
>> plot(sin(1:10))
more matlab code
more matlab code
\end{matlabcode}

\begin{matlabcode}[blue, line width=4pt, -latex]
>> plot(sin(1:10))
more matlab code
more matlab code
\end{matlabcode}
\end{document}
share|improve this answer
    
Thank you for your answer! Your code works even for intented code via the xleftmargin keyword, so it fits my needs. Anyway, it would be nice if one did not have to adjust by hand the vertical shift of the box as it depends on the font size. –  rgallego Nov 23 '12 at 8:39
    
You are welcome. I am not sure I follow. Are you saying you would prefer the xshift to be part of the \Arrow (as that is easy) macro, or are you referring to some other problem. If it is some other problem it is best if you provide a test case that illustrates the problem. –  Peter Grill Nov 23 '12 at 8:43
    
I mean that the amount of negative shift in \raisebox{-3.5ex}, in order to vertically adjust the arrow, depends on the fontsize if one wants to align the arrow tip with the center of the text line. That is not actually as problem since I guess the vertical shift can be set in terms of the font height. –  rgallego Nov 23 '12 at 10:47
    
The ex units are not fixed and should scale with the font. –  Peter Grill Nov 23 '12 at 10:48
    
That's right, but if you for example put a \Huge after \begin{document} the arrows do not get properly aligned. But as I said this is not an issue for me. –  rgallego Nov 23 '12 at 21:04
add comment

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.