Is there a way to add the geometrical shapes (triangle,...) as in the Listing below?

enter image description here

Thanks a lot!

  • Yes, if you post an MWE for the listing. – user121799 Mar 17 '18 at 23:41
  • It reminds me about an interesting problem in Digital Image Processing. – Artificial Stupidity Mar 18 '18 at 2:02
  • 2
    Hopefully, this is intended for an art project, not for tormenting readers of the listings. – Heiko Oberdiek Mar 18 '18 at 3:19
  • @Heiko Oberdiek , don't worry! ;) – user4712458 Mar 18 '18 at 12:41

Here is an example, based on this post.

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{lstlisting}[escapechar= !]
!\tikzmark{x1}!int gilligan(int j){
    int marmot(int i){ do something really cool !\tikzmark{x4}!
         int duck(int k){
        !\tikzmark{x2}!cout<< i; !\tikzmark{x5}!
!\tikzmark{x3}!   }
!\tikzmark{x6}!} !\tikz[overlay,remember picture]{\draw[fill=gray](x1.south)--([yshift=1pt]x2.west)--(x3.south)--cycle;}!
\tikz[overlay,remember picture]{%

enter image description here

I was not sure how close the shaded region should be to the code, but this can be adjusted. I also put one TikZ picture inside the listing because you'll need this if the code goes over more than one page.

UPDATE: It would have been nice (or even appropriate) if you would have mentioned that you got a really good answer on a related question. And yes, one can also use the methods used there.

\documentclass{article} % from https://tex.stackexchange.com/a/419865/121799


  \tikz[baseline=0pt, remember picture] { \node[fill=gray!50,thick,rounded corners,anchor=base,#1/.try] (listings-\the\value{tmlistings}) {#2}; }%

  \tikz[baseline=0pt, remember picture] { \node[inner
  sep=0pt,anchor=base,#1/.try] (newlistings-\the\value{newtmlistings}) {#2}; }%
%  \typeout{\thenewtmlistings}

% \tikzset{
%   keyword/.style={
%     fill=gray!75,
%     draw=black
%   }
% }



int gilligan (int j) {
  for (int i = 1; i < j; i++) {
    if (i % 2 == 0) {
      cout << i

\begin{tikzpicture}[remember picture,overlay]
% \foreach \i [evaluate={\j=int(\i+1)}] in {0,...,12}
% {\draw  (newlistings-\i)node[left]{\i} --(newlistings-\j); }
\draw[fill=gray] (newlistings-0.south west) -- ([xshift=-4pt]newlistings-12.west)
-- (newlistings-14.north-| newlistings-0.south west) -- cycle;
\draw[fill=gray] (newlistings-14.south) -- ([yshift=-2pt]newlistings-12.south)
-- ([xshift=1.1cm,yshift=-2pt]newlistings-12.south east)
-- ([xshift=1.3cm,yshift=-2pt]newlistings-9.south east) 
-- ([xshift=1.3cm,yshift=-2pt]newlistings-9.south east |- newlistings-14.south)
-- cycle;


enter image description here

  • thank you so much for these answers!!! I took the 1st one, whereas the 2nd one I'm still trying to understand how to adjust these shapes as they appear a bit bigger and completely outside the listing! Thanks!!!! – user4712458 Mar 18 '18 at 13:10

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.