如何在表格内绘制带有标签头的箭头?

如何在表格内绘制带有标签头的箭头?

我想画以下 在此处输入图片描述

这是我的 MWE

\documentclass[preview]{standalone}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\usepackage{tikz} 

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}}

\newcommand\tikzmark[2]{%
\tikz[remember picture,baseline] \node[inner sep=2pt,outer sep=0] (#1){#2};%
} 
\newcommand\link[2]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shift={(0,0)}]
  \draw[->] (#1) to (#2);
\end{tikzpicture}%
}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{MMMMMMMMMX}
               &         &\tikzmark{y}{} &               &     &   &&&&\\[2em] %\cline{1-6}
               &1        &\tikzmark{b}{} &\tikzmark{d}{} &     &     &&&&\\[2em]  
\tikzmark{x}{} &1        &1              &\tikzmark{f}{} &\tikzmark{h}{}&  &&&&\\[2em]  
\tikzmark{a}{} &1        &2              &1              &\tikzmark{j}{}&\tikzmark{l}{} &&&&\\[2em]  
\tikzmark{c}{} &1        &3              &3              &  1&  &&&&\\[2em]  
\tikzmark{e}{} &1        &4              &6              &4&1&&&  \\[2em]
\tikzmark{g}{} &1        &5              &10             &10&5&1&&  \\[2em]
\tikzmark{i}{} &1        &6              &15             &20&15&6&1&  \\[2em]
\tikzmark{k}{} &$\vdots$ &$\vdots$       &$\vdots$       & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\ddots$
\end{tabularx}
\link{x}{y}
\link{a}{b} 
\link{c}{d} 
\link{e}{f}
\link{g}{h}
\link{i}{j}
\link{k}{l}
\end{document} 

我遇到了箭头标签的问题,它是对角线的总和。

答案1

我会用 Ti 画这个Z 矩阵,它为您提供了更多可能性。例如,使用以下代码:

\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}[line cap=round]
  \def\sep{0.3cm}
  \matrix(D)[matrix of math nodes,minimum size=0.7cm]
  {
       1 &        &        &        &        &        &        & \\
       1 &      1 &        &        &        &        &        & \\
       1 &      2 &      1 &        &        &        &        & \\
       1 &      3 &      3 &      1 &        &        &        & \\
       1 &      4 &      6 &      3 &      1 &        &        & \\
       1 &      5 &     10 &     10 &      5 &      1 &        & \\
       1 &      6 &     15 &     20 &     15 &      6 &      1 & \\
  \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots \\
  };
  \begin{scope}[rotate=45]
  \foreach\x/\y in{1/1, 2/1, 3/2, 4/3, 5/5, 6/8, 7/13}
    \draw[thick,red,->] (D-\x-1.south) --++ (1+0.5*\x,0) node[right,rotate=45] {$\y$};
  \end{scope}
\end{tikzpicture}
\end{document}

你会得到: 在此处输入图片描述

答案2

您滥用表格,使用额外的列和行来使箭头更长。如果您希望箭头平行并以美观的方式结束,则这样tikzmark做没有意义,因为它会根据数字或您的情况下的额外空白单元格来绘制箭头

这看起来很糟糕:

\documentclass[preview, border=1cm]{standalone}
\usepackage{tabularx}
\usepackage{tikz} 
\usetikzlibrary{tikzmark}

\renewcommand\tabularxcolumn[1]{m{#1}}
\newcolumntype{M}{>{\centering\arraybackslash}m{1cm}}

\newcommand\link[3]{%
\begin{tikzpicture}[remember picture, overlay, >=stealth, shift={(0,0)}]
  \draw[->] (pic cs:#1) -- (pic cs:#2) node[above right]{#3};
\end{tikzpicture}%
}

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{MMMMMMMMMX}
               &         &\tikzmark{y}{} &               &     &   &&&&\\[2em] %\cline{1-6}
               &1        &\tikzmark{b}{} &\tikzmark{d}{} &     &     &&&&\\[2em]  
\tikzmark{x}{} &1        &1              &\tikzmark{f}{} &\tikzmark{h}{}&  &&&&\\[2em]  
\tikzmark{a}{} &1        &2              &1              &\tikzmark{j}{}&\tikzmark{l}{} &&&&\\[2em]  
\tikzmark{c}{} &1        &3              &3              &  1&  &&&&\\[2em]  
\tikzmark{e}{} &1        &4              &6              &4&1&&&  \\[2em]
\tikzmark{g}{} &1        &5              &10             &10&5&1&&  \\[2em]
\tikzmark{i}{} &1        &6              &15             &20&15&6&1&  \\[2em]
\tikzmark{k}{} &$\vdots$ &$\vdots$       &$\vdots$       & $\vdots$ & $\vdots$ & $\vdots$ & $\vdots$ & $\ddots$
\end{tabularx}
\link{x}{y}{1}
\link{a}{b}{1}
\link{c}{d}{2}
\link{e}{f}{3}
\link{g}{h}{5}
\link{i}{j}{8}
\link{k}{l}{13}
\end{document} 

带箭头总计的表格

要像绘图中那样旋转总和,可以使用以下命令:

\draw[->] (pic cs:#1) -- (pic cs:#2) node[above right, rotate=45]{#3};  

答案3

这有点不同,但是为什么不使用matrix? 例如:

\documentclass[preview, border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark, matrix, calc, arrows.meta, backgrounds}

\begin{document}
\begin{tikzpicture}[myline/.style={
        orange!20, line width=1.2em, cap=round, -Triangle Cap},]
        \matrix (m) [matrix of nodes, nodes in empty cells,
        nodes = {circle, minimum width=3em, anchor=center},
        ]
        {
            1 &   &    &    &    &   &\\
            1 & 1 &    &    &    &   &\\
            1 & 2 & 1  &    &    &   &\\
            1 & 3 & 3  & 1  &    &   &\\
            1 & 4 & 6  & 4  & 1  &   &\\
            1 & 5 & 10 & 10 & 5  & 1 &\\
            1 & 6 & 15 & 20 & 15 & 6 & 1\\
        };
        \begin{scope}[on background layer]
            \draw [myline] (m-1-1.center) -- (m-1-1.center) -- ++(45:1) coordinate(t1);
            \draw [myline] (m-2-1.center) -- ($(m-1-1.center)!0.5!(m-2-2.center)$) -- ++(45:1) coordinate(t2);
            \draw [myline] (m-3-1.center) -- (m-2-2.center) -- ++(45:1) coordinate(t3);
            \draw [myline] (m-4-1.center) -- ($(m-2-2.center)!0.5!(m-3-3.center)$) -- ++(45:1) coordinate(t4);
            \draw [myline] (m-5-1.center) -- (m-3-3.center) -- ++(45:1) coordinate(t5);
            \draw [myline] (m-6-1.center) -- ($(m-3-3.center)!0.5!(m-4-4.center)$) -- ++(45:1) coordinate(t6);
            \draw [myline] (m-7-1.center) -- (m-4-4.center) -- ++(45:1) coordinate(t7);
        \end{scope}
        \foreach [count=\i] \tot in {1, 1, 2, 3, 5, 8, 13} \node[font=\bfseries] at (t\i) {\tot}; 
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

与。{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix,tikz}

\begin{document}

\renewcommand{\arraystretch}{1.5}
\setlength{\arraycolsep}{10pt}

$\begin{NiceTabular}[columns-width=auto]{ccccccc}
  1 \\
  1 & 1 \\
  1 & 2 & 1 \\
  1 & 3 & 3  & 1 \\
  1 & 4 & 6  & 4  & 1 \\
  1 & 5 & 10 & 10 & 5  & 1 \\
  1 & 6 & 15 & 20 & 15 & 6 & 1 \\
  \\
\CodeAfter
  \begin{tikzpicture} [->, right,every node/.append style = {rotate = 45}]
    \draw (2.5-|1) -- (1.5-|2) node {$1$} ; 
    \draw (3.5-|1) -- (2-|2.5) node {$1$} ; 
    \draw (4.5-|1) -- (2.5-|3) node {$2$} ; 
    \draw (5.5-|1) -- (3-|3.5) node {$3$} ; 
    \draw (6.5-|1) -- (3.5-|4) node {$5$} ; 
    \draw (7.5-|1) -- (4-|4.5) node {$8$} ; 
    \draw (8.5-|1) -- (4.5-|5) node {$13$} ; 
  \end{tikzpicture}
\end{NiceTabular}$

\end{document}

上述代码的输出

相关内容