使用 tikz 将文本定位在箭头附近

使用 tikz 将文本定位在箭头附近

我正在努力让一些文本出现在用 tikzmark 绘制的箭头末端的正确位置。代码如下

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{booktabs}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{kbordermatrix}
\usepackage{bigstrut}
\usetikzlibrary{tikzmark,arrows.meta,patterns,calc}

%\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[4][]{%
    \tikz[overlay,remember picture]{%
        \coordinate (TopLeft)     at ($(pic cs:#2)+(-0.2em,0.9em)$);
        \coordinate (BottomRight) at ($(pic cs:#3)+(0.2em,-0.3em)$);
        %
        \path (TopLeft); \pgfgetlastxy{\XCoord}{\IgnoreCoord};
        \path (BottomRight); \pgfgetlastxy{\IgnoreCoord}{\YCoord};
        \coordinate (LabelPoint) at ($(\XCoord,\YCoord)!0.5!(BottomRight)$);
        %
        \draw [red,#1] (TopLeft) rectangle (BottomRight);
        \node [below, #1, fill=none, fill opacity=1] at (LabelPoint) {#4};
    }
}


\newcommand\DrawArrow[4][]{
\begin{tikzpicture}[
remember picture,
overlay,
arr/.style={draw=gray,line width=2pt, {-latex}, #1},
xshift=1em,
yshift=-3ex,
]
\draw[arr]
([xshift=1ex]pic cs:#2)
 to[out=-50,in=-150,looseness=1.0] node[below=0pt] {#4}
([xshift=-1ex]pic cs:#3);
 \end{tikzpicture}
}

\newcommand\DrawArrowB[4][]{
\begin{tikzpicture}[
remember picture,
overlay,
arr/.style={draw=gray,line width=2pt, {-latex}, #1},
xshift=-1ex,
yshift=1ex,
]
\draw[arr]
([xshift=-1ex]pic cs:#2)
 to[out=-50,in=-150,looseness=1.0] node[below=0pt] {#4}
([xshift=-2ex]pic cs:#3);
 \end{tikzpicture}
}

\newcommand\DrawArrowC[4][]{
\begin{tikzpicture}[
remember picture,
overlay,
arr/.style={draw=black,line width=0.5pt, {-latex}, #1},
xshift=1.5ex,
yshift=-0.8ex,
]
\draw[arr]
([xshift=-1ex]pic cs:#2)
 to[out=100,in=10,looseness=0] node[below=10pt] {#4}
([xshift=-2ex]pic cs:#3);
 \end{tikzpicture}
}

\newcommand\DrawArrowD[4][]{
\begin{tikzpicture}[
remember picture,
overlay,
arr/.style={draw=black,line width=0.5pt, {-latex}, #1},
xshift=1.5ex,
yshift=-0.8ex,
]
\draw[arr]
([xshift=-1ex]pic cs:#2)
 to[out=100,in=10,looseness=0] node[below=10pt] {#4}
([xshift=8ex]pic cs:#3);
 \end{tikzpicture}
}


\begin{document}

\begin{table}[htbp]
 \centering
 \caption{Issue 2}
 \begin{tabular}{@{}rrrrrrr@{}}
 \toprule
 & \multicolumn{6}{c}{Development year} \\
 \cmidrule(l){2-7}
 {Year} & {1} & {2} & {3} & {4} & {5} & {6} \\
 \midrule
 2009  & \tikzmark{lefta}443  &        \tikzmark{leftb}1,136  &        2,128  &        2,898  &        3,403  &        3,873  \\
 2009  & 443  &        1,136  &        2,128  &        2,898  &        3,403  &        3,873  \\
 2010  &            396  &        1,333  &        2,181  &        2,986  &        3,692  &  \\
 2011  &            441  &        1,288  &        2,420  &        3,483  &       &  \\
 2012  &            359  &        1,421  &        2,864  &       &       &  \\
 2013  &            377\tikzmark{righta} &        1,363\tikzmark{rightb}  &       &       &       &  \\
 2014  &  344& - &    -  &     -  &    -  &  - \\
&\phantom{377}\tikzmark{line1end}&\phantom{1,363}\tikzmark{line2end}&&&&\\

 \end{tabular}%
 \DrawBox[thick, black]{lefta}{righta}{}
 \DrawBox[thick, black]{leftb}{rightb}{}
 \DrawArrowC{righta}{line1end}{Sum=2,016}
 \DrawArrowD{rightb}{line2end}{Sum=6,542}
 \label{tab:first two col sums}
 \end{table}%   
\end{document}

得出的结果为:

在此处输入图片描述 我希望“Sum=6,542”字样出现在箭头下方的中央,但似乎无法将文本移到右侧。有人能帮忙吗?此外,如果有人有更优雅的方法来实现这种类型的表格标签,那么我很乐意看到任何建议。例如,我想知道如果“Sum=...”内容本身放在方框中,看起来会不会更好?

非常感谢。

答案1

你的node(在\DrawArrowCandD命令里面) 位置不对。如果你说:

\draw (0,0) -- node{A} (2,0);

默认情况下,如果您将节点A放在midway(1,0)最后一个坐标,它将默认放置在路径的最末端,即最后一个输入坐标:

\draw (0,0) -- (2,0) node{A};

在这种情况下A,将被放置在(2,0)。另一种选择是通过键明确给出节点位置pos,即

\draw node[pos=1]{A} (0,0) -- (2,0);

1路径的终点和起点分别位于哪里0。这将把节点放置在路径的最末端坐标处(该节点的别名为键at end)。因此,如果您明确地将pos(或其别名midwayat startat end)键赋予节点,它将被放置在该pos键指向的任何位置。

至于你问的看起来怎么样,当然是一个意见问题。在我看来,用 TiZ 正在毁掉桌子,把它排成一排,并把总数放进去,这样看起来会好得多。

关于所呈现的代码,我不明白你为什么要制作这么多\DrawArrowX命令,它们是相同的命令,只是做了一些小改动。你只需使用一个命令就可以实现相同的结果,\DrawArrow并扩展使用 TiZ 样式,那么您就不必记住哪个\DrawArrowX是哪个,而且您拥有更大的灵活性,因为您可以在文档中更改样式。天哪,您甚至可以创建名为 的样式AB以及修改arr样式的任何样式,这样您就不必使用多个\DrawArrowX命令。以下是如何完成此操作的示例:

\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\usetikzlibrary{tikzmark,arrows.meta,patterns,calc}

%\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[4][]{%
    \tikz[overlay,remember picture]{%
        \coordinate (TopLeft)     at ($(pic cs:#2)+(-0.2em,0.9em)$);
        \coordinate (BottomRight) at ($(pic cs:#3)+(0.2em,-0.3em)$);
        %
        \path (TopLeft); \pgfgetlastxy{\XCoord}{\IgnoreCoord};
        \path (BottomRight); \pgfgetlastxy{\IgnoreCoord}{\YCoord};
        \coordinate (LabelPoint) at ($(\XCoord,\YCoord)!0.5!(BottomRight)$);
        %
        \draw [red,#1] (TopLeft) rectangle (BottomRight);
        \node [below, #1, fill=none, fill opacity=1] at (LabelPoint) {#4};
    }
}

\tikzset{arr draw/.style={draw=gray,line width=2pt,-latex},
         arr to/.style={out=-50,in=-150},
         arr node/.style={below=1mm, inner sep=1pt}}

\newcommand\DrawArrow[4][]{
\begin{tikzpicture}[remember picture,overlay, #1]
\draw[arr draw] (pic cs:#2) to[arr to] (pic cs:#3) node[arr node] {#4};
 \end{tikzpicture}
}

\begin{document}

\begin{table}[htbp]
 \centering
 \caption{Issue 2}
 \begin{tabular}{@{}rrrrrrr@{}}
 \toprule
 & \multicolumn{6}{c}{Development year} \\
 \cmidrule(l){2-7}
 {Year} & {1} & {2} & {3} & {4} & {5} & {6} \\
 \midrule
 2009  & \tikzmark{lefta}443  &        \tikzmark{leftb}1,136  &        2,128  &        2,898  &        3,403  &        3,873  \\
 2009  & 443  &        1,136  &        2,128  &        2,898  &        3,403  &        3,873  \\
 2010  &            396  &        1,333  &        2,181  &        2,986  &        3,692  &  \\
 2011  &            441  &        1,288  &        2,420  &        3,483  &       &  \\
 2012  &            359  &        1,421  &        2,864  &       &       &  \\
 2013  &            377\tikzmark{righta} &        1,363\tikzmark{rightb}  &       &       &       &  \\
 2014  &  344& - &    -  &     -  &    -  &  - \\
&\phantom{377}\tikzmark{line1end}&\phantom{1,363}\tikzmark{line2end}&&&&\\

 \end{tabular}%
 \DrawBox[thick, black]{lefta}{righta}{}
 \DrawBox[thick, black]{leftb}{rightb}{}
 % You can change the all the "arr's" styles globally by using \tikzset
 % in the middle of the document, then all \DrawArrow commands will follow such style
 % or locally through the optional argument of \DrawArrow, then just that command will follow that sytle.
 \DrawArrow[arr to/.style={bend left}, arr draw/.style={->}, arr node/.style={left, font=\tiny}]{righta}{line1end}{Sum=2,016}
 \DrawArrow{rightb}{line2end}{Sum=6,542}
 \label{tab:first two col sums}
 \end{table}%   
\end{document}

相关内容