如何让 tickzmark 创建的图形保持在边距内?

如何让 tickzmark 创建的图形保持在边距内?

我正在尝试在列表环境中调出源代码的某些部分。根据对我的原始问题的回复评论这里我可以根据需要调出这些部分(见下面的输出)。但是括号显示在边距之外。

如何在边缘内制作用 tikzmark 生成的图形?

以下是显示该问题的示例图像和源乳胶:

在此处输入图片描述

\documentclass[twocolumn]{article}

\usepackage{enumitem}
\usepackage{tikz}
\usepackage{listings}
\usepackage{xparse}
\usepackage{lipsum}

\usetikzlibrary{calc,decorations.pathreplacing}

\newcommand{\tikzmark}[1]{%
  \tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {};}

\newcommand*{\BracektStubSize}{0.5em}%
\NewDocumentCommand\LeftBracket{%
    O{}% #1 = draw options
    O{0pt}% #2 = shift to be applied (optional, for use with nested braces)
    m% #3 = top \tikzmark name
    m% #4 = bottom \tikzmark name
    m% #5 = node text
}{%
    \begin{tikzpicture}[overlay,remember picture,baseline]
        \coordinate (Top Start of Bracket) at ([shift={(#2-15pt,5pt)}]#3.north east);
        \coordinate (Bottom Start of Bracket) at ([shift={(#2-15pt,2pt)}]#4.south east);
        \draw[thick, #1] 
            (Top Start of Bracket) --++(-\BracektStubSize,0) 
                |-(Bottom Start of Bracket) ;

        \node[xshift=-5pt, align=left, anchor=east, #1]
            at ($(Bottom Start of Bracket)!0.5!(Top Start of Bracket)$) {#5};
    \end{tikzpicture}    
}

\begin{document}

\lipsum[1]

\begin{lstlisting}[escapechar=\&]
List<Point2D> data = // ...

&\tikzmark{Mark A}&val x = scaleBand<Int> {
    rangeRound(listOf(0.0, width))
    domain(data.indices.toList())
    padding(0.1)
&\tikzmark{End A}&}


&\tikzmark{Mark B}&val y = scaleLinear<Double> {
  domain(listOf(height, 0))
  range(listOf(0, data.maxBy { it.y })
&\tikzmark{End B}&}

&\tikzmark{Mark C}&val bar = rect<Point2D> {
    width(x.bandwidth)
    height { d, _ -> height - y(d.y) }
    fill(Color.STEELBLUE)
&\tikzmark{End C}&}

&\tikzmark{Mark D}&val root = Group()
root.selectAll<Point2D>(".bar")
    .data(data)
    .enter()
    .append { d, _, _ -> bar(d) }
    .translateX { _, i, _ -> x(i) }
&\tikzmark{End D}&    .translateY { d, _, _ -> y(d.y) }
\end{lstlisting}

\LeftBracket[black][0pt]{Mark A}{End A}{a}
\LeftBracket[black][0pt]{Mark B}{End B}{b}
\LeftBracket[black][0pt]{Mark C}{End C}{c}
\LeftBracket[black][0pt]{Mark D}{End D}{d}


\end{document}

答案1

尝试添加

\lstset{
xleftmargin=9mm,
breaklines=true,
} 

\usepackage{listings}

这样清单缩进。但是要小心,如果这些\tikzmark对最终出现在不同的列中……

在此处输入图片描述

相关内容