表格中的对角虚线

表格中的对角虚线

我目前有下表:在此处输入图片描述

对应下面的代码:

  \documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[font=small,labelfont=bf]{caption}

\usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.5cm]{geometry}
\linespread{1.5}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{tabularx}
\usepackage{adjustbox}
\usepackage[flushleft]{threeparttable}

\begin{document}
\begin{table}[h!]
\ra{0.8}
\centering
\caption{bla }
\begin{tablenotes}
\renewcommand\baselinestretch{1}
\small
    \item Note: bla.   
\end{tablenotes}
\setlength{\tabcolsep}{8pt}
\begin{tabular}{l | c | c c c c }
\toprule
  \multicolumn{1}{c}{\bfseries Panel A: Countries}\\
   \hline
 & Average & Netherlands &  Germany & France & Belgium  \\ \hline
Netherlands & 3.83 &  &  &  &    \\
Germany  & 3.87 &  &  &  &    \\
France  & 3.95 &  &  &  &    \\
Belgium  & 3.91 &  &  &  &    \\
\midrule
 \multicolumn{1}{c}{\bfseries Panel B: Clients}\\
   \hline
 & Average & Advisory &  Execution Only &  &   \\\hline
Advisory & 3.86 &  &  &  &    \\
Execution Only & 3.78 &  &    &  &  \\
\bottomrule
\end{tabular}\label{tab:VarImportc}

\end{table} 
\end{document}

我想在两个面板的右侧添加两条对角虚线(即从荷兰-荷兰到比利时-比利时以及从咨询-咨询到执行-执行)。

另外,我想知道如何使第一列(平均值)与表格的右侧区分开来?我现在通过垂直线做到了,但看起来不太好看。

答案1

首先,你的代码有一些小错误。其次,你的问题对我来说不清楚!顺便说一句,据我从你的文字中注意到的,我将借助tikz包来完成​​。请参阅以下代码:

  \documentclass{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[font=small,labelfont=bf]{caption}

\usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.5cm]{geometry}
\linespread{1.5}
\usepackage{booktabs}
%\usepackage{threeparttable}
\usepackage{tabularx}
\usepackage{adjustbox}
\usepackage{tikz}
\usepackage[flushleft]{threeparttable}


\newcommand{\tikzmark}[1]{\noindent\tikz[overlay,remember picture] \node (#1) {};}

\begin{document}
\begin{table}[h!]
%\ra{0.8}
\centering
\caption{bla }
\begin{tablenotes}
\renewcommand\baselinestretch{1}
\small
    \item Note: bla.   
\end{tablenotes}
\setlength{\tabcolsep}{8pt}
\begin{tabular}{l || c || c c c c }
\toprule
  \multicolumn{1}{c}{\bfseries Panel A: Countries}\\
   \hline
 & Average & Netherlands &  Germany & France & Belgium  \\ \hline
Netherlands & 3.83 & \tikzmark{start}\phantom{Netherlands} &  &  &    \\
Germany  & 3.87 &  &  &  &    \\
France  & 3.95 &  &  &  &    \\
Belgium  & 3.91 &  &  &  &  \phantom{Belgium}\tikzmark{end}   \\
\midrule
 \multicolumn{1}{c}{\bfseries Panel B: Clients}\\
   \hline
 & Average & Advisory &  Execution Only &  &   \\\hline
Advisory & 3.86 &  &  &  &    \\
Execution Only & 3.78 &  &    &  &  \\
\bottomrule
\end{tabular}\label{tab:VarImportc}
\begin{tikzpicture}[overlay,remember picture]
    \draw [dashed] (start.north) -- (end.south);
\end{tikzpicture}
\end{table} 
\end{document}

在此处输入图片描述

答案2

您可以通过在相关单元格中放置 tikz 坐标来实现这一点,然后使用remember picture和,让您在创建表格后返回并绘制线条。例如(我已将您的示例缩减为 MWE - 请注意,您目前正在调用该包两次,您需要修复此问题):overlaythreeparttable

\documentclass{standalone}

\linespread{1.5}
\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{tabularx}
\usepackage{adjustbox}

\usepackage{tikz}
\begin{document}

\setlength{\tabcolsep}{8pt}
\begin{tabular}{l | c | c c c c }
\toprule
  \multicolumn{1}{c}{\bfseries Panel A: Countries}\\
   \hline
 & Average & Netherlands &  Germany & France & Belgium  \\ \hline
Netherlands & 3.83 & \tikz[remember picture,overlay]{\coordinate(Netherlands);} &  &  &    \\
Germany  & 3.87 &  &  &  &    \\
France  & 3.95 &  &  &  &    \\
Belgium  & 3.91 &  &  &  &  \tikz[remember picture,overlay]{\coordinate(Belgium);}  \\
\midrule
 \multicolumn{1}{c}{\bfseries Panel B: Clients}\\
   \hline
 & Average & Advisory &  Execution Only &  &   \\\hline
Advisory & 3.86 & \tikz[remember picture,overlay]{\coordinate(Advisory);} &  &  &    \\
Execution Only & 3.78 &  &  \tikz[remember picture,overlay]{\coordinate(Exec);}  &  &  \\
\bottomrule
\end{tabular}

\begin{tikzpicture}[remember picture,overlay]
\draw[dashed](Netherlands) -- (Belgium);
\draw[dashed](Advisory) -- (Exec);
\end{tikzpicture}

\end{document}

这将从相应单元格的中心绘制一条虚线。您可以通过在命令中指定xshiftand/or参数来移动起点/终点,例如:yshift\coordinate

\tikz[remember picture,overlay]{\coordinate[xshift=20pt](Netherlands);}

这个答案基于这里完成的类似工作:在表格单元格的角落添加 TikZ 节点

答案3

环境与经典环境(包)类似,但在单元格、列和行下创建 PGF/TikZ 节点。然后就可以使用所谓的TikZ 绘制您想要的任何规则{NiceTabular}nicematrix{tabular}array\CodeAfter

\documentclass{article}
\usepackage[english]{babel}
\usepackage[font=small,labelfont=bf]{caption}
\usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.5cm]{geometry}
\linespread{1.5}
\usepackage{booktabs}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{table}[h!]

\centering
\caption{my caption }
\setlength{\tabcolsep}{8pt}
\begin{NiceTabular}{l | c | c c c c }
    \toprule
    \Block[l]{1-*}{\bfseries Panel A: Countries} \\ \hline
     & Average & Netherlands &  Germany & France & Belgium  \\ \hline
    Netherlands & 3.83 &  &  &  & \\
    Germany  & 3.87 &  &  &  & \\
    France  & 3.95 &  &  &  & \\
    Belgium  & 3.91 &  &  &  & \\
    \midrule
    \Block[l]{1-*}{\bfseries Panel B: Clients} \\ \hline
     & Average & Advisory &  Execution Only &  & \\ \hline
    Advisory & 3.86 &  &  &  & \\
    Execution Only & 3.78 &  & &  &  \\
    \bottomrule
\CodeAfter
    \tikz \draw [dotted] (3-|3) -- (7-|7) (9-|3) -- (11-|7) ; 
\end{NiceTabular}\label{tab:VarImportc}

\end{table} 

\end{document}

您需要多次编译(因为使用了 PGF/TikZ 节点nicematrix)。

上述代码的输出

相关内容