我正在努力处理 LaTeX 中的表格,我想在表格环境中绘制线条。我的目标是像这样,线条将第一列的值连接到第二列的值:
我可能不得不使用 TikZ,但我找不到类似的东西。我能找到的最好的其实是这个没有任何进展。有可能做到这样的事吗?
更新:MWE
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{threeparttable}
\usepackage{multirow}
\begin{document}
\begin{table}
\begin{threeparttable}
\begin{tabular}{ccccc}
2018 & \multirow{2}{*}1 & 5 & 10 & 15 \\
2019 & \multirow{2}{*}2 & 6 & 11 & 16 \\
2020 & \multirow{2}{*}3 & 7 & 12 & 17 \\
2021 & \multirow{2}{*}4 & 8 & 13 & 18 \\
2022 & & 9 & 14 & 19 \\
\end{tabular}
\end{threeparttable}
\end{table}
\end{document}
谢谢
答案1
使用 TikZmatrix:
\documentclass[10pt,a4paper]{article}
%\usepackage[utf8]{inputenc}<--- no more needed in up-to-date distribution
\usepackage[T1]{fontenc}
\usepackage{threeparttable}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{table}\centering
\begin{threeparttable}
\begin{tikzpicture}
\matrix[matrix of nodes,
row sep=-4pt,
column sep=4pt,
row 1/.style={font=\bfseries, text height=8pt, text depth=4pt}] (mymatr)
{
Year & Header 1 (something) & Header 2 & Header 3 & Header 4 \\[10pt]
2018 & & 5 & 10 & 15 \\
& 1 \\
2019 & & 6 & 11 & 16 \\
& 2 \\
2020 & & 7 & 12 & 17 \\
& 3 \\
2021 & & 8 & 13 & 18 \\
& 4 \\
2022 & & 9 & 14 & 19 \\
};
\foreach \ind
[evaluate=\ind as \indpre using int(\ind-1),
evaluate=\ind as \indpost using int(\ind+1)
] in {3, 5, 7, 9}
{
\draw (mymatr-\indpre-1.east) --
(mymatr-\ind-2.west);
\draw (mymatr-\indpost-1.east) --
(mymatr-\ind-2.west);
}
% hlines
\draw[very thick] (mymatr.north west) -- (mymatr.north east);
\draw[shorten >=-4pt, shorten <=-4pt] (mymatr-1-1.south west) -- (mymatr-1-5.south east);
\draw[very thick] (mymatr.south west) -- (mymatr.south east);
\end{tikzpicture}
\begin{tablenotes}[para]\small
Note: I added the headers only to show how to manage them with a Ti\emph{k}Zmatrix.
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}
答案2
\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usepackage{threeparttable}
\usepackage{multirow}
\begin{document}
\begin{threeparttable}
\begin{tabular}{ccccc}
2018\tikzmark{18} & \multirow{2}{*}{\tikzmark{1819}1} & 5 & 10 & 15 \\
2019\tikzmark{19} & \multirow{2}{*}{\tikzmark{1920}2} & 6 & 11 & 16 \\
2020\tikzmark{20} & \multirow{2}{*}{\tikzmark{2021}3} & 7 & 12 & 17 \\
2021\tikzmark{21} & \multirow{2}{*}{\tikzmark{2122}4} & 8 & 13 & 18 \\
2022\tikzmark{22} & & 9 & 14 & 19 \\
\end{tabular}
\end{threeparttable}
\begin{tikzpicture}[remember picture, overlay]
\draw[transform canvas={yshift=0.6ex}]
([xshift=1pt]pic cs:18) -- ([xshift=-1pt]pic cs:1819) --
([xshift=1pt]pic cs:19) -- ([xshift=-1pt]pic cs:1920) --
([xshift=1pt]pic cs:20) -- ([xshift=-1pt]pic cs:2021) --
([xshift=1pt]pic cs:21) -- ([xshift=-1pt]pic cs:2122) --
([xshift=1pt]pic cs:22);
\end{tikzpicture}
\end{document}