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내부 )이 잘못된 위치에 있습니다. 당신이 말하는 경우:\DrawArrowCD

\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시작 은 어디 인가? 그러면 경로의 맨 끝 좌표에 노드가 배치됩니다(별칭은 key 입니다 at end). 따라서 노드에 명시적으로 키(또는 별칭 및 중 pos하나 )를 제공하면 해당 키가 연결되는 모든 위치에 배치됩니다 .midwayat startat endpos

당신이 어떻게 보이는지에 대한 질문은 확실히 의견의 문제입니다. 내 생각으로는 Ti와 함께 이 작업을 수행하는 것입니다.케이Z는 테이블을 망치고 있습니다. 행을 만들고 그 안에 합계를 넣으면 훨씬 더 좋아 보일 것입니다.

제시된 코드와 관련하여 왜 그렇게 많은 \DrawArrowX명령을 작성하는지 이해가 되지 않습니다. 명령은 약간만 변경된 동일한 명령입니다. \DrawArrowTi를 한 번만 사용하고 확장하여 동일한 결과를 얻을 수 있습니다.케이Z 스타일을 사용하면 어느 것이 무엇인지 기억할 필요가 없으며 \DrawArrowX문서 중간에 스타일을 변경할 수 있으므로 유연성이 더욱 높아집니다. 젠장, 이라는 스타일을 만들 수도 있고 A스타일 을 B수정하는 모든 것을 만들 수도 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}

관련 정보