Posicionar texto perto de setas usando tikz

Posicionar texto perto de setas usando tikz

Estou lutando para que algum texto apareça no lugar certo no final de uma seta, desenhada com tikzmark. O código é o seguinte

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

Isso produz:

insira a descrição da imagem aqui Quero que as palavras "Soma = 6.542" apareçam centralizadas sob a seta, mas não consigo mover o texto para a direita. Alguém pode ajudar por favor? Além disso, se alguém tiver alguma maneira mais elegante de conseguir esse tipo de rotulagem na mesa, ficaria feliz em ver alguma sugestão. Por exemplo, eu me perguntei se ficaria melhor se o conteúdo "Sum=..." estivesse em caixas, talvez?

Muito obrigado.

Responder1

Seu node(dentro do comando \DrawArrowCand D) está na posição errada. Se você diz:

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

Por padrão, o texto Aserá colocado midwayno caminho (neste caso, em (1,0)) se você colocar o nódepoisa última coordenada, ela será colocada, por padrão, bem no final do caminho, ou seja, na última coordenada de entrada:

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

Neste caso Aserá colocado em (2,0). Outra opção é fornecer explicitamente a posição do nó através da poschave, dizendo

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

Onde 1está o fim do caminho e 0o começo. Isso colocará o nó na coordenada final do caminho (um alias para isso é key at end). Portanto, se você fornecer explicitamente a chave pos(ou um de seus aliases midwaye ) ao nó at start, at endele será colocado onde quer que essa poschave leve.

Quanto à sua questão de como fica, é certamente uma questão de opinião. Na minha opinião, fazer isso com TikZ está estragando a tabela, faça uma linha e coloque as somas nela, ficaria muito melhor.

Em relação ao código apresentado, não entendo porque você faz tantos \DrawArrowXcomandos, são os mesmos comandos com algumas coisinhas alteradas. Você pode obter o mesmo resultado com apenas um \DrawArrowuso extensivo de TikEstilos Z, então você não precisa lembrar qual \DrawArrowXé qual e tem ainda mais flexibilidade, já que pode alterar os estilos no meio do documento. Droga, você pode até fazer estilos chamados Ae Bo que quer que modifique o arrestilo para que você não tenha vários \DrawArrowXcomandos. Aqui está um exemplo de como isso pode ser feito:

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

informação relacionada