2

I am showing right and top braces, whereas while attempting to add overbraces using a new function called 'AddNoteOver', I am getting this error: "Package tikz Error: + or - expected ...sting-1-start}{listing-1-end}{overbrace!.}" , my file is interpreted, though!.

\documentclass[english,a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{decorations.pathreplacing,calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

\newcommand*{\AddNote}[4]{%
    \begin{tikzpicture}[overlay, remember picture]
        \draw [decoration={brace,amplitude=0.7em},decorate, thick,black]
            ($(#3)!([yshift=1.5ex]#1)!($(#3)-(0,1)$)$) --  
            ($(#3)!(#2)!($(#3)-(0,1)$)$)
                node [align=center, text width=3cm, pos=0.5, anchor=west] {#4};
    \end{tikzpicture}
}%

\newcommand*{\AddNoteOver}[3]{%
\begin{tikzpicture}[overlay, remember picture]
    \draw [decoration={brace,amplitude=0.5em},decorate, thick,black]
            {($($(#1).north west$)-(0,-0.3)$) -- ($($(#2).north east$)-(0,-0.3)$) node [align=center, text width=3cm, pos=0.5, anchor=west,above=3mm] {#3}};
\end{tikzpicture}
}%

\begin{document}

\lipsum[1]

\begin{lstlisting}[
basicstyle={\fontsize{7}{8}\ttfamily},mathescape,
identifierstyle={\color{black}},
tabsize=2,
language=Java,
numbersep=1pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run}
]
public  $\tikzmark{listing-1-start}$ PersistentJournal(PrevaylerDirectory directory)  $\tikzmark{listing-1-end}$
 throws IOException {
        PrevaylerDirectory.checkValidJournalSuffix(journalSuffix); $\tikzmark{listing-4-end}$
        _monitor = monitor;
        _directory = directory;
        _directory.produceDirectory();s;
        _journalSuffix = journalSuffix;                            $\tikzmark{listing-7-end}$
    }
\end{lstlisting}

\AddNote{listing-4-end}{listing-7-end}{listing-4-end}{Right Brace!.}
\AddNoteOver{listing-1-start}{listing-1-end}{overbrace!.}

\end{document}

enter image description here

2
1

There were a few mistakes in your \AddNoteOver. The main one being (#1).north west should not have the (), so should be #1.north west:

enter image description here

Notes:

Code:

\documentclass[english,a4paper,12pt]{report}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{listings}
\usetikzlibrary{decorations.pathreplacing,calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}

% https://tex.stackexchange.com/questions/51019/how-can-i-put-a-curly-brace-inside-an-algorithm-to-group-code-lines/51037#51037
\newcommand*{\AddNote}[4]{%
    \begin{tikzpicture}[overlay, remember picture]
        \draw [decoration={brace,amplitude=0.7em},decorate, thick,black]
            ($(#3)!([yshift=1.5ex]#1)!($(#3)-(0,1)$)$) --  
            ($(#3)!(#2)!($(#3)-(0,1)$)$)
                node [align=center, text width=3cm, pos=0.5, anchor=west] {#4};
    \end{tikzpicture}
}%

\newcommand*{\AddNoteOver}[3]{%
\begin{tikzpicture}[overlay, remember picture]
    \draw [decoration={brace,amplitude=0.5em},decorate, thick,black]
            {($(#1.north west)-(0,-0.3)$) -- ($(#2.north east)-(0,-0.3)$) node [align=center, text width=3cm, pos=0.5, anchor=west,above=3mm] {#3}};
\end{tikzpicture}
}%

\begin{document}

\lipsum[1]

\vspace{1.0cm}
\begin{lstlisting}[
basicstyle={\fontsize{7}{8}\ttfamily},mathescape,
identifierstyle={\color{black}},
tabsize=2,
language=Java,
numbersep=1pt,
numbers=left,
xleftmargin=0.5cm,frame=tlbr,framesep=2pt,framerule=0pt,
morekeywords ={class,run},
]
public  $\tikzmark{listing-1-start}$ PersistentJournal(PrevaylerDirectory directory)  $\tikzmark{listing-1-end}$
 throws IOException {
        PrevaylerDirectory.checkValidJournalSuffix(journalSuffix); $\tikzmark{listing-4-end}$
        _monitor = monitor;
        _directory = directory;
        _directory.produceDirectory();s;
        _journalSuffix = journalSuffix;                            $\tikzmark{listing-7-end}$
    }
\end{lstlisting}

\AddNote{listing-4-end}{listing-7-end}{listing-4-end}{Right Brace!.}
\AddNoteOver{listing-1-start}{listing-1-end}{overbrace!.}

\end{document}
0

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.