I have been searching for an answer for some time but have not come across a solution. I am taking some notes from a programming course and need to take a line of code and identify each individual part with alternating arrows. For example, the line:

this.Something = new AnotherThing(anArgument);

Now, I need an arrow below this line pointing up to 'this' coming from some text identifying the part. Now, another arrow would need to start at some explanation text above this line and point down to the 'Something'. The =, new, and parenthesis do not need anything, just the other items. I have been trying to use \underset and \overset, but the spacing on the line goes wonky. Any guidance will be greatly appreciated.

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. You can also upload a hand-drawing picture to illustrate your idea clearer. – Symbol 1 Jan 17 '15 at 3:20
  • @StevenB.Segletes Thank you, those are very similar to my issue, while some of the formatting is different, there is probably some useful stuff there I can pick out. I just wish they were named better so I would have found them myself lol. The only problem is, I don't want the circles from the first one, and on the second link I was hoping to actually have less formatting and just have alternating, straight arrows. – SPentland Jan 17 '15 at 3:22
  • You can change {\bigcirc} to {} in the answer at tex.stackexchange.com/questions/140529/… to get rid of the red circle. – Steven B. Segletes Jan 17 '15 at 3:25
  • You can define \newcommand\notate[3]{\unskip\def\useanchorwidth{T}\setbox0=\hbox{#1}\def\stackalignment{c}\stackunder[-6pt]{\def\stackalignment{c}\stackunder[-1.5pt]{\stackunder[-2pt]{\strut #1}{}}{\rule{\rlwd}{#2\baselineskip}}}{\strut$\downarrow$\brlap{\kern-2ex\footnotesize#3}}\ignorespaces} in the answer at tex.stackexchange.com/questions/167659/… to perhaps get preferable formatting. – Steven B. Segletes Jan 17 '15 at 3:32

Here's one option:

enter image description here

The code:




Some regular text before
@\tikzmark{a}@this.@\tikzmark{b}@Something = new AnotherThing(anArgument);
Some regular text after

\begin{tikzpicture}[remember picture,overlay]
  ([shift={(10pt,-3pt)}]pic cs:a) -- ++(10pt,-20pt) coordinate(aux1);
  at (aux1) {some explanatory text};
  ([shift={(15pt,9pt)}]pic cs:b) -- ++(10pt,20pt) coordinate(aux2);
\node[above,green!80!black,text width=4cm] 
  at (aux2) {some other explanatory text; it's longer};



  • The listings package was used to typeset the code. The package offers additional customization possibilities for typesetting code; please refer to the documentation

  • TikZ was used here. The tikzmark library was used to place some marks (the escapeinside feature from listings was used to place the marks inside lstlisting). Then those marks were used to draw the arrows and place the explanatory texts using \nodes.

  • Since the overlay option has to be used in the tikzpicture for the exaplanatory texts, some spacing will need to be added to get the proper spacing with the material surrounding the code.

  • Some internal calculations are involved, so the code needs two runs to stabilize.

| improve this answer | |
  • Wow, this is exactly what I was looking for! Thank you so much Gonzalo Medina. And a very special thanks for the extra information in the remarks section too. I was really hoping to get an answer that would lead me to further information!I'll have to remember to come back and vote it up when I have the required 15 reputation. – SPentland Jan 17 '15 at 3:31
  • @SPentland You're welcome! Glad I could help. – Gonzalo Medina Jan 17 '15 at 3:32

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.