如何为句子的各个部分创建悬垂标签?

如何为句子的各个部分创建悬垂标签?

附有参考图片。在此处输入图片描述 我还希望能够改变箭头和标签文本的颜色等。

答案1

编辑:具有更多功能的较新版本是这里作为对后续问题的回答。


我编写了一个命令\mylabel,将内联文本作为第一个强制参数,将标签文本作为第二个强制参数。您可以使用可选\setmylabel参数全局自定义外观(针对当前组)或局部自定义外观(针对此用途)。

\documentclass{article}
\usepackage{blindtext}

\usepackage{tikz}
\usetikzlibrary{positioning}


% --------------- def options ---------------

\pgfqkeys{/mylabel}{%
    % geometry
    shift x/.initial = 1.5em,
    shift y/.initial = .2em,
    slope width/.initial = .3em,
    text xsep/.initial  = .1em,
    text ysep/.initial  = .1em,
    label xsep/.initial = .3333em,
    label ysep/.initial = .3333em,
    %
    % colors
    line color/.initial  = black,
    text color/.initial  = black,
    label color/.initial = black,
    color/.style = {line color=#1, label color=#1},
    %
    % label positions
    /mylabel/pos/.is choice,
    /mylabel/pos/above/.style = {pos=above right},
    /mylabel/pos/below/.style = {pos=below right},
    /mylabel/pos/below right/.style = {%
        _anchor label  = north west,
        _direction     = below,
        _line pos      = south,
    },
    /mylabel/pos/above right/.style = {%
        _anchor label  = south west,
        _direction     = above,
        _line pos      = north,
    },
    %
    % internal
    _anchor label/.initial,
    _direction/.initial,
    _line pos/.initial,
    %
    % struts
    inline strut/.initial=\vphantom{Ap},
    label strut/.initial=\strut,
}

\newcommand{\setmylabel}[1]{%
    \pgfqkeys{/mylabel}{#1}%
}


% --------------- init options ---------------

\setmylabel{%
    pos = below,
}


% --------------- \mylabel command ---------------

\newcommand{\mylabel}[3][]{% [#1: options], #2: inline text, #3: label text
    \begin{tikzpicture}[baseline=(inline.base)]
        % process options
        \setmylabel{#1}

        % draw text
        \node[%
            inner ysep = \pgfkeysvalueof{/mylabel/text xsep},
            inner xsep = \pgfkeysvalueof{/mylabel/text ysep},
            text = \pgfkeysvalueof{/mylabel/text color},
        ] (inline) {\pgfkeysvalueof{/mylabel/inline strut}#2};
        \node (label) [
            \pgfkeysvalueof{/mylabel/_direction} = \pgfkeysvalueof{/mylabel/shift y} of inline,
            xshift = \pgfkeysvalueof{/mylabel/shift x},
            anchor = \pgfkeysvalueof{/mylabel/_anchor label},
            inner xsep = \pgfkeysvalueof{/mylabel/label xsep},
            inner ysep = \pgfkeysvalueof{/mylabel/label ysep},
            text = \pgfkeysvalueof{/mylabel/label color},
        ] {\pgfkeysvalueof{/mylabel/label strut}#3};

        % draw lines
        \draw[draw=\pgfkeysvalueof{/mylabel/line color}]
            (inline.\pgfkeysvalueof{/mylabel/_line pos} west) -- (inline.\pgfkeysvalueof{/mylabel/_line pos} east)
            (inline.\pgfkeysvalueof{/mylabel/_line pos}) -- ([xshift=\pgfkeysvalueof{/mylabel/slope width}] inline.\pgfkeysvalueof{/mylabel/_line pos} |- label) -- (label);
        ;

        % set bounding box
        \pgfresetboundingbox
        \useasboundingbox 
            (inline.south west) rectangle (inline.north east)
            (inline |- label.south) rectangle (inline |- label.north)
        ;
    \end{tikzpicture}%
}


% --------------- test document ---------------

\setmylabel{%
    line color  = orange,
    text color  = blue,
    label color = green,
}

\begin{document}
    \blindtext
    text \mylabel[pos=above]{part}{label~1} of a \mylabel{sentence}{label~2}
    \blindtext
\end{document}

我建议将 def 选项、init 选项和 \mylabel 命令放在单独的文件中我的标签.tex在子目录中前言并将其包含在内\input{preamble/mylabel}

在此处输入图片描述


编辑:为了进一步定制,您可以添加另一个 pgfkey

line/.initial =,

并将代码的第一行“绘制线条”替换为

\pgfkeys{/mylabel/line/.get=\tmpLineStyle}%
\expandafter \draw \expandafter [\tmpLineStyle, draw=\pgfkeysvalueof{/mylabel/line color}]

然后您有另一个选项,可以line向其传递诸如dotteddasheddoublethick、等值(请参阅thinline width=2ptZ 文档第15.3节绘制路径)。您可以通过用逗号连接多个值来组合它们。如果值包含逗号或等号,则必须用括号括起来。

例如:

line = {thick, dashed},

在此处输入图片描述

相关内容