在列表中使用顶部和右括号时出现错误

在列表中使用顶部和右括号时出现错误

我正在显示右括号和上括号,而当尝试使用名为“AddNoteOver”的新函数添加上括号时,出现了以下错误:“包 tikz 错误:预期为 + 或 - ...sting-1-start}{listing-1-end}{overbrace!.}” ,但是我的文件被解释过了!

\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}

在此处输入图片描述

答案1

您的 中有几个错误\AddNoteOver。最主要的是(#1).north west不应该有(),因此应该是#1.north west

在此处输入图片描述

笔记:

代码:

\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}

相关内容