2

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

enter image description here

Thanks a lot!

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

1 Answer 1

3

Here is an example, based on this post.

\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\begin{document}
\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;}!
\end{lstlisting}
\tikz[overlay,remember picture]{%
\draw[fill=gray]([xshift=4pt]x6.south)--([xshift=8pt]x2.south)--(x5|-x2.south)--(x4.south)--(x4|-x6.south)
--cycle;} 
\end{document}

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
%\url{https://tex.stackexchange.com/q/419759/86}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newcounter{tmlistings}
\newcounter{newtmlistings}

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

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


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

\lstset{
  keywordstyle=\makenewnode{keyword},
  stringstyle=\makenewnode{string},
  identifierstyle=\makenewnode{identifier},
}


\begin{document}

\begin{lstlisting}[language=c,escapechar=!]
int gilligan (int j) {
  for (int i = 1; i < j; i++) {
    if (i % 2 == 0) {
      cout << i
    }
  }
}!\makenewnode{keyword}{\strut}!
\end{lstlisting}

\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;
\end{tikzpicture}

\end{document}

enter image description here

1
  • 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!!!! Mar 18, 2018 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .