I try to draw on a listing with tikz overlay. but the image only show the listing, then the part out side of that region has been cut out.
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{listings}
\begin{document}
\definecolor{lbcolor}{rgb}{1,1,1}
\newcounter{m}
\lstset{%
frame = tb,
tabsize = 1,
numbers = left,
framesep = 3pt,
framerule = 0.4pt,
commentstyle = \color{Green},
keywordstyle = \color{blue},
stringstyle = \color{DarkRed},
backgroundcolor = \color{WhiteSmoke},
showstringspaces = false,
}
\newcommand\makenode[2]{%
\tikz[baseline=0pt, remember picture] {
\node[fill=gray!50,anchor=base,#1/.try,inner sep=0] (m-\the\value{m}) {#2}; }%
\stepcounter{m}%
}
\lstdefinelanguage{MYLANG}{
backgroundcolor=\color{lbcolor},
alsoletter=0123456789,
keywords={
5479,3802,1677,29,14,3787,1692,3071,469,2602
},
keywordstyle=\makenode{keyword},
sensitive=false, %not case-sensitive
} %
\begin{lstlisting}[language=MYLANG,numbers=none,escapechar=!,
basicstyle=\ttfamily\bfseries, linewidth=\linewidth]
$ adb shell free -m
total used free shared buffers
Mem: 5479 3802 1677 29 14
-/+ buffers/cache: 3787 1692
Swap: 3071 469 2602
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay,
font=\tiny,
every edge/.append style = {
->, thick, >=stealth,
DimGray, dashed, line width = 1pt },
every node/.append style={
align=center,anchor=north,minimum height=1pt,
font=\bfseries,fill=green!10,inner sep=0},
%text width=5pt
]
\def\dy{-50pt}
\draw (m-0.west) -- ++(-5pt,0) -- ++(0,\dy) node[pos=1,below] (N1) {total(MB)};
\draw (m-7.west) -- ++(-5pt,0);
\draw (m-1.west) -- ++(-5pt,0) -- ++(0,\dy) node[pos=1,below] (N2) {used(MB)};
\draw (m-5.west) -- ++(-5pt,0);
\draw (m-8.west) -- ++(-5pt,0);
\draw (m-2.west) -- ++(-5pt,0) -- ++(0,\dy) node[pos=1,below] (N3) {free(MB)};
\draw (m-6.west) -- ++(-5pt,0);
\draw (m-9.west) -- ++(-5pt,0);
\draw (m-3.west) -- ++(-5pt,0) -- ++(0,\dy) node[pos=1,below] (N4) {shared(MB)};
\draw (m-4.west) -- ++(-5pt,0) -- ++(0,\dy) node[pos=1,below] (N5) {buffer(MB)};
\end{tikzpicture}
\end{document}
Update
@Qrrbrbirlbel's answer is perfect, but when I try to replace the tikz
with tikzpicture
, but looks like not work as expected. the offset of each annotation line shift to left a little bit.
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{positioning}
\usepackage{listings}
\begin{document}
\definecolor{lbcolor}{rgb}{1,1,1}
\newcounter{m}
\lstset{%
frame = tb,
tabsize = 1,
numbers = left,
framesep = 3pt,
framerule = 0.4pt,
commentstyle = \color{Green},
keywordstyle = \color{blue},
stringstyle = \color{DarkRed},
backgroundcolor = \color{WhiteSmoke},
showstringspaces = false,
}
\newcommand\makenode[2]{%
\hspace{-.15em}%
\tikz[baseline=0pt, remember picture]
\node[fill=gray!50, anchor=base, inner sep=+.15em,
text depth=+0pt, outer sep=+0pt, #1/.try] (m-\the\value{m}) {#2};%
\hspace{-.15em}%
\stepcounter{m}%
}
\lstdefinelanguage{MYLANG}{
backgroundcolor=\color{lbcolor},
alsoletter=0123456789,
keywords={5479,3802,1677,29,14,3787,1692,3071,469,2602},
keywordstyle=\makenode{keyword},
sensitive=false, %not case-sensitive
} %
\begin{varwidth}{\linewidth}
\begin{lstlisting}[language=MYLANG,numbers=none,escapechar=!,
basicstyle=\ttfamily\bfseries, linewidth=\linewidth]
$ adb shell free -m
total used free shared buffers
Mem: 5479 3802 1677 29 14
-/+ buffers/cache: 3787 1692
Swap: 3071 469 2602
\end{lstlisting}
\begin{tikzpicture}[
remember picture, baseline=+0pt,
font=\tiny,
every node/.append style={font=\bfseries, fill=green!10, text depth=+0pt},
vh/.style={to path=|-(\tikztotarget)},
every edge/.append style={vh, gray!50, overlay},
n 4/.style={yshift=-\baselineskip-2*(\pgfkeysvalueof{/pgf/inner ysep})},
]
\foreach[count=\i] \t/\n/\ns in {
total/0/7, used/1/{5, 8}, free/2/{6, 9}, shared/3, buffer/4}{
\node[anchor=base, n \i/.try] (n) at ([xshift=-5pt]m-\n.west|-0,0) {\t(MB)}
edge (m-\n)
\unless\ifx\n\ns foreach \N in \ns {edge (m-\N)} \fi;
}
\end{tikzpicture}
\end{varwidth}
\end{document}