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( \DrawArrowCandコマンド内)のD位置が間違っています。次のようにすると:

\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を明示的にノードに指定すると、そのキーが示す場所にノードが配置されます。posmidwayat startat endpos

見た目はどうかという質問に関しては、確かに意見の問題です。私の意見では、Tiでこれを行うと、Z は表を台無しにしています。行を作成してそこに合計を入力すると、見栄えがずっと良くなります。

提示されたコードに関して、なぜそんなに多くのコマンドを作成するのか理解できません。それらは、いくつかの小さな変更を加えた同じコマンドです。Tiを\DrawArrowX1回だけ使用して、同じ結果を得ることができます。\DrawArrowZ スタイルを使用すると、どれがどれであるかを覚える必要がなくなり\DrawArrowX、ドキュメントの途中でスタイルを変更できるため、さらに柔軟性が高まります。 と呼ばれるスタイルや、スタイルを変更するその他のスタイルも作成できるAためBarr複数のコマンドを使用する必要はありません\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}

関連情報