对角箭头(使用 TikZ)应平行对齐

对角箭头(使用 TikZ)应平行对齐

我的代码如下:

\documentclass{book}

\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}%

\begin{document}

\begin{center}
\renewcommand*\arraystretch{1.3}
\begin{tabular}{c | ccccc}
$b_5$ & 11\tikzmark{11} & & & & \\
$b_4$ & 7\tikzmark{7} & \tikzmark{12l}12\tikzmark{12r} & & & \\
$b_3$ & 4\tikzmark{4} & \tikzmark{8l}8\tikzmark{8r} & \tikzmark{13l}13\tikzmark{13r} & & \\
$b_2$ & 2\tikzmark{2} & \tikzmark{5l}5\tikzmark{5r} & \tikzmark{9l}9\tikzmark{9r} & \tikzmark{14l}14\tikzmark{14r} & \\
$b_1$ & 1 & \tikzmark{3}3 & \tikzmark{6}6\tikzmark{6r} & \tikzmark{10}10 & \tikzmark{15}15 \\
\hline
& $a_1$ & $a_2$ & $a_3$ & $a_4$ & $a_5$ \\
\end{tabular}
\end{center}

\begin{tikzpicture}[overlay, remember picture, shorten >=3pt, shorten <=3pt]
\draw [->] ({pic cs:2}) -- ({pic cs:3});
\draw [->] ({pic cs:4}) -- ({pic cs:5l});
\draw [->] ({pic cs:5r}) -- ({pic cs:6});
\draw [->] ({pic cs:7}) -- ({pic cs:8l});
\draw [->] ({pic cs:8r}) -- ({pic cs:9l});
\draw [->] ({pic cs:9r}) -- ({pic cs:10});
\draw [->] ({pic cs:11}) -- ({pic cs:12l});
\draw [->] ({pic cs:12r}) -- ({pic cs:13l});
\draw [->] ({pic cs:13r}) -- ({pic cs:14l});
\draw [->] ({pic cs:14r}) -- ({pic cs:15});
\end{tikzpicture}


\end{document}

输出为:

在此处输入图片描述

我需要所有箭头平行排列,但有些箭头不是。我标记了位置供您参考。我该如何实现?

答案1

AndréC 已经指出了可以使用的事实\tikzmarknode。这样做的重点是,无需手动添加所有这些箭头,可以使用循环。calc这里也不需要。

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\begin{document}

\begin{center}
\renewcommand*\arraystretch{1.3}
\begin{tabular}{c | ccccc}
$b_5$ & \tikzmarknode{11}{11} & & & & \\
$b_4$ & \tikzmarknode{7}{7} & \tikzmarknode{12}{12} & & & \\
$b_3$ & \tikzmarknode{4}{4} & \tikzmarknode{8}{8} & \tikzmarknode{13}{13} & & \\
$b_2$ & \tikzmarknode{2}{2} & \tikzmarknode{5}{5}& \tikzmarknode{9}{9} & \tikzmarknode{14}{14} & \\
$b_1$ & 1 & \tikzmarknode{3}{3} & \tikzmarknode{6}{6} & \tikzmarknode{10}{10} & \tikzmarknode{15}{15} \\
\hline
& $a_1$ & $a_2$ & $a_3$ & $a_4$ & $a_5$ \\
\end{tabular}
\end{center}

\begin{tikzpicture}[overlay, remember picture, shorten >=3pt, shorten <=3pt]
\foreach \X [count=\Y] in {3,6,10,15}
{\foreach \Z in {\the\numexpr\X-\Y,...,\the\numexpr\X-1}
{\draw [->] (\Z) -- (\the\numexpr\Z+1);} }
\end{tikzpicture}
\end{document}

在此处输入图片描述

但是,恕我直言,不应该强迫人们输入数字。它们也可以自动填写。重点是 TiZ 矩阵元素“知道”它的位置,它存储在计数\pgfmatrixcurrentrow和中\pgfmatrixcurrentcolumn。然后我们只需要一个古老的高斯公式就可以知道条目。如果你想要另一个矩阵,只需更改公式即可。

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{table}
\centering
\begin{tikzpicture}
\matrix[matrix of nodes,nodes in empty cells,column sep=1ex,row sep=1ex,
nodes={align=center,execute at begin node={
\ifnum\the\pgfmatrixcurrentcolumn>\the\pgfmatrixcurrentrow
\else
\pgfmathtruncatemacro{\effrow}{5-\pgfmatrixcurrentrow}%
\pgfmathtruncatemacro{\myres}{-\effrow+(\pgfmatrixcurrentcolumn+\effrow)*(\pgfmatrixcurrentcolumn+\effrow+1)/2}%
\myres%
\fi}}] (mat)
 {& & & & & \\
 & & & & & \\
 & & & & & \\
 & & & & & \\
 & & & & & \\
 };
\foreach \X  in {1,...,5}
{\path ([xshift=-2pt]mat.west|-mat-\X-1) node[left] (b\the\numexpr6-\X) {$b_{\the\numexpr6-\X}$}
 ([yshift=-2pt]mat.south-|mat-5-\X) node[below] (a\X) {$a_{\X}$};
\ifnum\X>1
\foreach \Y in {1,...,\the\numexpr\X-1}
{\draw[-stealth,shorten >=-2pt,shorten <=-2pt] (mat-\the\numexpr\X-1\relax-\Y) -- (mat-\X-\the\numexpr\Y+1);}
\fi} 
\draw (mat.north west) -- (a1.south-|mat.west) (mat.south east) -- (b5.west|-mat.south);
\end{tikzpicture}
\caption{A table.}
\label{tab:ABC}
\end{table}
\end{document}

在此处输入图片描述

答案2

此代码中最简单的事情是使用在每个数字周围\tikzmarknode创建的命令node并使用这些节点作为参考。

在此处输入图片描述

\documentclass{book}

\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}%

\begin{document}

\begin{center}
\renewcommand*\arraystretch{1.3}
\begin{tabular}{c | ccccc}
$b_5$ & \tikzmarknode{11}{11} & & & & \\
$b_4$ & \tikzmarknode{7}{7} & \tikzmarknode{12}{12} & & & \\
$b_3$ & \tikzmarknode{4}{4} & \tikzmarknode{8}{8} & \tikzmarknode{13}{13} & & \\
$b_2$ & \tikzmarknode{2}{2} & \tikzmarknode{5}{5}& \tikzmarknode{9}{9} & \tikzmarknode{14}{14} & \\
$b_1$ & 1 & \tikzmarknode{3}{3} & \tikzmarknode{6}{6} & \tikzmarknode{10}{10} & \tikzmarknode{15}{15} \\
\hline
& $a_1$ & $a_2$ & $a_3$ & $a_4$ & $a_5$ \\
\end{tabular}
\end{center}

\begin{tikzpicture}[overlay, remember picture, shorten >=3pt, shorten <=3pt]
\draw [->] (2) -- (3);
\draw [->] (4) -- (5);
\draw [->] (5) -- (6);
\draw [->] (7) -- (8);
\draw [->] (8) -- (9);
\draw [->] (9) -- (10);
\draw [->] (11) -- (12);
\draw [->] (12) -- (13);
\draw [->] (13) -- (14);
\draw [->] (14) -- (15);
\end{tikzpicture}


\end{document}

相关内容