我正在尝试绘制一个带箭头的矩阵。但它不能占用太多空间,否则箭头无法正常显示。
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[column sep=0pt, row sep=0pt]
&&0&0&0&0&0&0&0&0&0&0\\
&0\ar[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\ar[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\ar[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\ar[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\ar[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\ar[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\ar[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\ar[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\ar[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\ar[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\ar[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\ar[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4\\
\end{tikzcd}
\end{document}
如果简单的线条看起来更好,我并不特别需要完整的箭头。但我确实需要所有对角线都呈直线(目前有一条奇怪的曲线),箭头不仅仅是箭头头,垂直线位于行之间的中心。
答案1
使箭头更长的一个相当简单的方法是将设置inner sep
为 0,并增加row
和column sep
s,以使尺寸保持不变(大致)。
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[column sep=3pt, row sep=5pt,nodes={inner sep=0pt,align=center,
text width={width("20")}}]
&&0&0&0&0&0&0&0&0&0&0\\
&0\ar[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\ar[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\ar[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\ar[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\ar[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\ar[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\ar[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\ar[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\ar[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\ar[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\ar[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\ar[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4\\
\end{tikzcd}
\end{document}
答案2
另一种选择是在 tikz 环境中使用节点矩阵,但它有很多选项,并且更加困难但可定制性更高。
结果:
梅威瑟:
% arara: pdflatex: {synctex: yes, interaction: nonstopmode}
\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,arrows.meta}
\begin{document}
\begin{tikzpicture}[
%Global environment config.
%baseline=0cm, %Nice but I think is usefull only in tikzpicture env.
%Environment styles declarations
>=Stealth,
Matrix/.style={
matrix of math nodes,
nodes in empty cells,
column sep=0.25em,
row sep=1em,
nodes={inner xsep=0.5pt,inner ysep=2pt,minimum width=1.5em}
},
Brackets/.style={
left delimiter={[},
right delimiter={]}
}
]
%Start drawing the thing.
\matrix [Matrix] at (0,0)(M1){
%Matrix contents
&&0&0&0&0&0&0&0&0&0&0\\
&0&1&2&3&4&5&6&7&8&9&10\\
0&1&0&1&2&3&4&5&6&7&8&9\\
0&2&1&0&1&2&3&4&5&6&7&8\\
0&3&2&1&0&1&2&3&4&5&6&7\\
0&4&3&2&1&0&1&2&3&4&5&6\\
0&5&4&3&2&1&0&1&2&3&4&5\\
0&6&5&4&3&2&1&0&1&2&3&4\\
0&7&6&5&4&3&2&1&0&1&2&3\\
0&8&7&6&5&4&3&2&1&0&1&2\\
1&9&8&7&6&5&4&3&2&1&1&2\\
1&10&9&8&7&6&5&4&3&2&2&2\\
1&11&10&9&8&7&6&5&4&3&3&3\\
1&12&11&10&9&8&7&6&5&4&4&4\\
};
\foreach \x [evaluate=\x as \m using int(\x+1)]in {2,3,...,10}{
\draw[->] (M1-\x-\x) -- (M1-\m-\m);
}
\draw[->] (M1-11-11) -- (M1-12-11);
\draw[->] (M1-12-11) -- (M1-13-11);
\draw[->] (M1-13-11) -- (M1-14-12);
\end{tikzpicture}
\end{document}
答案3
对于通用来说,这实际上不是一个可行的解决方案,但我只是想向自己证明它可以作为 TABstack 来实现。
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabstackengine,graphicx}
\newcommand\minidarrow{\scalebox{.8}{\ensuremath{\downarrow}}}
\newcommand\minidrarrow{\scalebox{.8}{\rotatebox[origin=c]{-45}{$\rightarrow$}}}
\def\AR[#1#2]{%
\setbox0=\hbox{0}%
\ifx#1d
\ifx\relax#2\relax
\kern-.5\wd0
\bclap[.35\dimexpr\Lstackgap]{\minidarrow}%
\kern.5\wd0%
\else
\ifx r#2
\kern.5\wd0
\bclap[.3\dimexpr\Lstackgap]{\minidrarrow}%
\kern-.5\wd0
\else
% ...
\fi
\fi
\else
% ...
\fi
}
\fixTABwidth{T}
\setstackgap{L}{1.15\baselineskip}
\setstacktabbedgap{3pt}% May need to be tailored
\begin{document}
\Matrixstack{
&&0&0&0&0&0&0&0&0&0&0\\
&0\AR[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\AR[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\AR[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\AR[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\AR[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\AR[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\AR[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\AR[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\AR[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\AR[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\AR[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\AR[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4}
\end{document}
可以通过更改几行来尝试压缩:
\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabstackengine,graphicx}
\newcommand\minidarrow{\scalebox{.8}[.6]{\ensuremath{\downarrow}}}
\newcommand\minidrarrow{\scalebox{.8}{\rotatebox[origin=c]{-45}{$\rightarrow$}}}
\def\AR[#1#2]{%
\setbox0=\hbox{0}%
\ifx#1d
\ifx\relax#2\relax
\kern-.5\wd0
\bclap[.15\dimexpr\Lstackgap]{\minidarrow}%
\kern.5\wd0%
\else
\ifx r#2
\kern.5\wd0
\bclap[.3\dimexpr\Lstackgap]{\minidrarrow}%
\kern-.5\wd0
\else
% ...
\fi
\fi
\else
% ...
\fi
}
\fixTABwidth{T}
\setstackgap{L}{.8\baselineskip}
\setstacktabbedgap{0.7pt}% May need to be tailored
\begin{document}
\Matrixstack{
&&0&0&0&0&0&0&0&0&0&0\\
&0\AR[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\AR[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\AR[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\AR[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\AR[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\AR[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\AR[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\AR[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\AR[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\AR[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\AR[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\AR[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4}
\end{document}