
Estoy luchando para que aparezca un texto en el lugar correcto al final de una flecha, dibujada con tikzmark. El código es el siguiente
\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}
Esto produce:
Quiero que las palabras "Suma=6,542" aparezcan centradas debajo de su flecha, pero parece que no puedo mover el texto hacia la derecha. ¿Alguien puede ayudar por favor? Además, si alguien tiene alguna forma más elegante de lograr este tipo de etiquetado sobre la mesa, me encantaría ver alguna sugerencia. Por ejemplo, me preguntaba si se vería mejor si el contenido "Sum=..." estuviera en cuadros.
Muchas gracias.
Respuesta1
Su node
(dentro del comando \DrawArrowC
y D
) está en la posición incorrecta. Si usted dice:
\draw (0,0) -- node{A} (2,0);
De forma predeterminada, el texto A
se colocará en midway
la ruta (en este caso, en (1,0)
) si coloca el nododespuésla última coordenada, se colocará, por defecto, al final de la ruta, es decir, en la última coordenada de entrada:
\draw (0,0) -- (2,0) node{A};
En este caso A
se colocará en (2,0)
. Otra opción es dar explícitamente la posición del nodo a través de la pos
clave, diciendo
\draw node[pos=1]{A} (0,0) -- (2,0);
¿Dónde 1
está el final del camino y 0
el comienzo? Eso colocará el nodo en la coordenada final de la ruta (un alias para eso es la clave at end
). Por lo tanto, si proporciona explícitamente la clave pos
(o uno de sus alias midway
y ) al nodo at start
, at end
se colocará dondequiera que pos
conduzca esa clave.
En cuanto a tu pregunta sobre cómo se ve, es ciertamente una cuestión de opinión. En mi opinión, hacer esto con TikZ está arruinando la tabla, haz una fila y pon las sumas en ella, así se vería mucho mejor.
Con respecto al código presentado, no entiendo por qué haces tantos \DrawArrowX
comandos, son los mismos comandos con algunas pequeñas cosas cambiadas. Puedes lograr el mismo resultado con un solo \DrawArrow
y extenso uso de Ti.kZ estilos, entonces no necesita recordar cuál \DrawArrowX
es cuál y tiene aún más flexibilidad, ya que puede cambiar los estilos a mitad del documento. Maldita sea, incluso puedes crear estilos llamados A
y B
lo que sea que modifique el arr
estilo para que no tengas varios \DrawArrowX
comandos. A continuación se muestra un ejemplo de cómo se puede hacer:
\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}