使用 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路徑(在本例中為 at )(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 end)鍵指定給節點,它將放置在該pos鍵通往的任何位置。

至於你的問題,它看起來怎麼樣,當然是意見問題。在我看來,用 Ti 來做這件事kZ 毀了桌子,排成一行並將總和放入其中,看起來會好得多。

關於所提供的程式碼,我不明白為什麼你會發出這麼多\DrawArrowX命令,它們是相同的命令,只是做了一些小改動。僅使用一種\DrawArrow並大量使用 Ti即可獲得相同的結果kZ 樣式,那麼您無需記住哪個\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}

相關內容