1

It's a never-ending story I have to tell. I want to make some code annotations. Finally, I got my macro, but now...

The tikz drawings in listings break in certain circumstances (near page break):

    \documentclass{article}

    \usepackage{blindtext}
    \usepackage{relsize}
    \usepackage{setspace}
    \usepackage{tikz}
    \usepackage{listings}
    \usetikzlibrary{decorations.pathreplacing,calc,arrows}
    \newcommand*\circled[1]{\tikz[baseline=(char.base)]{%
                \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
    \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
    \newcommand*{\AddNote}[5]{%
    \begin{tikzpicture}[overlay, remember picture]
    \coordinate (x) at (#2,0);
    \coordinate (a) at ($(x)!(#1.north)!($(x)+(0,1)$)$);
    \coordinate (b) at ($(a)+(0.8,0)$);
    \coordinate (c) at ($(b)+(0,#3)$);
    \draw [open triangle 45-] (a) -- (b) -- (c);
    \node[#5] at (c) {\bf\sffamily\smaller#4};
    \end{tikzpicture}%
    }

    \begin{document}

    \blindtext
    \blindtext

    \onehalfspacing
    \begin{lstlisting}[escapechar=+, caption=Muh, label=lst:oop, language=c]
    +\linespread{2}+
    .. Core members+\tikzmark{core0}+
    .. Log stream+\tikzmark{log0}+
    .. +\color{black}{Lock object}\tikzmark{lock0}+

    public void Method (object[] args)
    {
      .. Check arguments+\tikzmark{valid}+
      .. Ensure authorization+\tikzmark{auth}+
      .. Lock +\color{black}{object for}+ thread safety+\tikzmark{lock1}+
      .. Start transaction+\tikzmark{trans1}+
      .. Log start of operation+\tikzmark{log1}+
      .. Perform core operation+\tikzmark{core1}+
      .. Log completion of operation+\tikzmark{log2}+
      .. Commit or rollback transaction+\tikzmark{trans2}+
      .. Unlock +\color{black}{object}\tikzmark{lock2}+
    }
    \end{lstlisting}
    \AddNote{valid}{8}{0}{Validation}{right}%

    \AddNote{auth}{8}{0}{Authorization}{right}%

    \AddNote{log0}{13.8}{-1}{Logging}{left}%
    \AddNote{log1}{13.8}{3}{}{}%
    \AddNote{log2}{13.8}{4}{}{}%

    \AddNote{lock0}{12}{-1}{Threading}{left}%
    \AddNote{lock1}{12}{2}{}{}%
    \AddNote{lock2}{12}{3}{}{}%

    \AddNote{trans1}{10.2}{-1}{Transaction}{above left}%
    \AddNote{trans2}{10.2}{1}{}{}%
    \singlespacing

    \blindtext

    \end{document}
1

Well, there was also a problem with using \baselinestretch and \onehalfspacing outside the listing (it blow up the caption background).

Here is what I came up with: Put the listing in a figure and use \onehalfspacing in the listing definition, like:

\begin{lstlisting}[basicstyle=\ttfamily\footnotesize\onehalfspacing....]

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.