在 Word 文档中,我有三个用箭头连接的表格,如下图所示,我想用 Latex 重现这些表格。我的问题是 1) 如何创建这三个表格,以便我可以将它们大致定位在图中的位置;2) 如何绘制连接不同表格单元格的箭头。
答案1
此答案使用tikzmark
您问题评论中提到的内容。基本上,这个想法是按正常方式排版表格,然后用tikzpicture
绘制连接箭头的 覆盖它们。
我为前两个表格创建了一个tabular
,为第三个表格创建了另一个。这里唯一的用处tikz
是创建第二列中的箭头。
\newcommand*{\myarrow}[1][50]{\tikz{\draw [black!#1, {Stealth[length=7.5pt]}-, thick] (0,0) -- (.35,0);}}
定义一个宏,\myarrow[]
它接受一个可选参数,该参数确定箭头的深浅程度(百分比)。默认值为,我也在示例中50
使用了它。85
环境tabular
使用包array
和dcolumn
两个自定义列:
\newcolumntype{d}{D{.}{.}{2}}
\newcolumntype{o}{>{$}c<{$}}
第一个将小数点居中,用于最多有 2 位小数的数字。这也使用数学模式。第二个只是创建居中的数学模式。
\tikzmark{}
用于在tabular
环境中标记稍后绘制箭头时使用的坐标。但是,此代码不会影响表格本身的排版。
然后,这些标记将用于tikzpicture
覆盖表格的图中。此图包含绘制连接表格的箭头的命令。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark, arrows.meta, calc, bending}
\usepackage{array, dcolumn}
\newcolumntype{d}{D{.}{.}{2}}
\newcolumntype{o}{>{$}c<{$}}
\newcommand*{\myarrow}[1][50]{\tikz{\draw [black!#1, {Stealth[length=7.5pt]}-, thick] (0,0) -- (.35,0);}}
\begin{document}
\begin{table}
\begin{tabular}{oco|d!{$\dots$}d}
\textbf{l} & & t & \multicolumn{1}{o!{$\dots$}}{P_{L1}} & \multicolumn{1}{o}{P_{Ln}}\\\cline{1-1}\cline{3-5}
\tikzmark{p1}1 & \myarrow & 1 & 0.40 & 0.58\\
\tikzmark{p2}1 & \myarrow & 2 & 0.41 & 0.60\\
\tikzmark{p3}2 & \myarrow[85] & 3 & 0.20 & 0.41\\
\tikzmark{p4}1 & \myarrow & 4 & 0.39 & 0.60\\
\vdots & & \vdots & \multicolumn{1}{o}{\vdots} & \vdots\\
\tikzmark{p5}2 & \myarrow[85] & M & 0.21 & 0.43\\
\end{tabular}\\[\bigskipamount]
\begin{tabular}{o|d!{$\dots$}dd}
c_i & \multicolumn{1}{o!{$\dots$}}{P_{L1}} & \multicolumn{1}{o}{P_{Ln}} & \multicolumn{1}{o}{Q_{L1}}\\\hline
\tikzmark{c1}c_1 & 0.40 & 0.59 & 0.10\\
\tikzmark{c2}c_2 & 0.21 & 0.42 & 0.05\\
\vdots & \multicolumn{1}{o}{\vdots} & \vdots & \vdots\\
c_k & 0.32 & 0.33 & 0.02\\
\end{tabular}
\end{table}
\begin{tikzpicture}[remember picture, overlay, -{Stealth[length=7.5pt]}, draw]
\coordinate (c1coord) at ($({pic cs:c1}) + (-.25em,.25em)$);
\coordinate (c2coord) at ($({pic cs:c2}) + (-.25em,.25em)$);
\foreach \i in {1,...,5}
\coordinate (p\i1) at ($({pic cs:p\i}) + (-.25em,.25em)$);
\coordinate (p12) at ($(p11) + (-1em,-1em)$);
\coordinate (p13) at ($(c1coord) + (-1em,1em)$ -| p12);
\coordinate (p22) at ($(p21) + (-1.5em,-1em)$);
\coordinate (p23) at ($(c1coord) + (-1.5em,1em)$ -| p22);
\coordinate (p32) at ($(p31) + (-2.5em,-1em)$);
\coordinate (p33) at ($(c2coord) + (-2.5em,1em)$ -| p32);
\coordinate (p42) at ($(p41) + (-2em,-1em)$);
\coordinate (p43) at ($(c1coord) + (-2em,1em)$ -| p42);
\coordinate (p52) at ($(p51) + (-3em,-1em)$);
\coordinate (p53) at ($(c2coord) + (-3em,1em)$ -| p52);
\foreach \i in {1,2,4}
\draw [black!50] (p\i1) to[out=-180, in=90] (p\i2) -- (p\i3) to[out=-90, in=180] (c1coord);
\foreach \i in {3,5}
\draw [black!85, dashed] (p\i1) to[out=-180, in=90] (p\i2) -- (p\i3) to[out=-90, in=180] (c2coord);
\end{tikzpicture}
\end{document}
答案2
怎么样TikZ
和一个matrix of math nodes
?
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
% The matrix entries
\matrix[
matrix of math nodes,
nodes in empty cells,
text depth=0.5ex,
text height=2ex,
] (mat)
{
l & & t & P_{L_{1}} & \cdots & P_{L_{n}} \\
1 & & 1 & 0.40 & \cdots & 0.58 \\
1 & & 2 & 0.40 & \cdots & 0.58 \\
2 & & 3 & 0.40 & \cdots & 0.58 \\
1 & & 2 & 0.40 & \cdots & 0.58 \\
\vdots & & \vdots & \vdots & & \vdots \\
2 & & 5 & 0.40 & \cdots & 0.58 \\
\\[10pt]
\mathbf{c}_{j} & P_{L_{1}} & \cdots & P_{L_{n}} & Q_{L_{1}} \\
\mathbf{c}_{1} & 0.32 & \cdots & 0.34 & 0.10 \\
\mathbf{c}_{2} & 0.32 & \cdots & 0.34 & 0.10 \\
\vdots & \vdots & & \vdots & \vdots \\
\mathbf{c}_{k} & 0.32 & \cdots & 0.34 & 0.10 \\
};
% The arrows in the second column
\begin{scope}[line width=1.5pt,<-,shorten >= -2pt,shorten <= -2pt]
\foreach \Fila in {2,3,5}
\draw[gray]
(mat-\Fila-2.west) -- (mat-\Fila-2.east);
\foreach \Fila in {4,7}
\draw
(mat-\Fila-2.west) -- (mat-\Fila-2.east);
\end{scope}
% The rules for the matrices
\draw (mat-1-1.south west) -- (mat-1-1.south east);
\draw (mat-1-3.south west) -- (mat-1-6.south east);
\draw (mat-1-4.north west) -- (mat-1-4.north west|-mat-7-4.south west);
\draw (mat-9-2.north west) -- (mat-9-2.north west|-mat-13-2.south west);
\draw (mat-9-1.south west) -- (mat-9-5.south east);
% The curved arrows connecting entries
\begin{scope}[
>=latex,
rounded corners
]
\foreach \Fila/\Corr in {2/8pt,3/15pt,5/22pt}
\draw[->,gray]
(mat-\Fila-1.west) -- ++(-\Corr,0) |- (mat-9-1.west);
\foreach \Fila/\Corr in {4/30pt,7/37pt}
\draw[->,dashed]
(mat-\Fila-1.west) -- ++(-\Corr,0) |- (mat-11-1.west);
\end{scope}
\end{tikzpicture}
\end{document}