使用以下代码,我可以成功地在表格内绘制蓝色框,但无法控制框的高度,尤其是宽度。换句话说,我无法让框适合表格的宽度。请问有什么技巧可以轻松控制框的宽度和高度吗?
\documentclass{article}
\usepackage{tabularx, booktabs}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
\tikz[overlay,remember picture]{
\draw[black,#1]
($(#2)+(-0.25em,2.0ex)$) rectangle
($(#3)+(-0.25em,-0.375ex)$);}
}
\begin{table}[H]
\resizebox{\columnwidth}{!}{\begin{tabular}{l c c c c c c c c c c}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& \multicolumn{9}{c}{\textit{Dependent variable:}} \\
\cline{2-11}
\\[-1.8ex] & $\sigma^l$ & $\sigma^\pi$ & $\sigma^k$ & ROE & log(der) & log($\phi_l$) & log($\phi_k$)& TFP& HHI &$\mu$ \\
\\[-1.8ex] & (1) & (2) & (3) & (4) & (5) & (6) &(7) & (8) & (9) & (10)\\
\hline \\[-1.8ex]
\textbf{Top} & $-$0.087 & $-$0.279 & 0.087 & $-$0.018 & 0.111 & $-$0.090 & $-$1.372$^{*}$ & $-$7.257 & $-$0.006 & 3.279$^{*}$ \\
$\times$\textbf{Post} & (0.083) & (0.204) & (0.083) & (0.016) & (0.240) & (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
\tikzmark{top left 2}\color{red}{Top30}& $-$0.127$^{***}$ & 0.115$^{***}$ & 0.127$^{***}$ & 0.004 & 0.102$^{*}$ & 0.102$^{**}$ & 0.162 & 1.101 & $-$0.031$^{***}$ & 1.214$^{***}$ \\
\color{red}{PostRe} & (0.018) & (0.044) & (0.018) & (0.004) & (0.055) & (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
& & & & & & & & & &\\
Top3 & $-$0.100$^{*}$ & 0.184 & 0.100$^{*}$ & 0.034$^{***}$ & 0.077 & 0.282$^{*}$ & 1.456$^{***}$ & $-$1.161 & 0.143$^{***}$ & \tikzmark{bottom right 2}3.548$^{***}$ \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176) & (0.159) & (0.554) & (3.427) & (0.033) & (1.335) \\
& & & & & & & & & & \\
\hline
\hline \\[-1.8ex]
& \multicolumn{10}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\
\end{tabular}}
\DrawBox[ultra thick, blue]{top left 2}{bottom right 2}
\scriptsize \textit{Note:}
\end{table}
\end{document}
答案1
无论您最终做什么,请不要用大锤\resizebox
强行将类似表格的环境塞进文本块的宽度。
我建议您使用一个tabular*
环境,将宽度设置为\textwidth
。使用正常大小的边距,这种方法只需要您发出指令\small
,将标称字体大小减少 10%。我还建议您将十个数据列中的数字与小数点对齐。这可以借助包来完成dcolumn
。
我把\tikzmark{bottom right 2}
指令保留在代码中原来的位置,即倒数第二列的末尾。如果您希望蓝色矩形也包含最后一列,只需移至\tikzmark{bottom right 2}
相关行的最后一个单元格即可。
\documentclass{article}
\usepackage{tikz,pgf}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
\tikz[overlay,remember picture]{
\draw[black,#1]
($(#2)+(-0.25em,2.0ex)$) rectangle
($(#3)+(-0.25em,-0.375ex)$);}
}
%% new code:
\usepackage[a4paper,margin=2.5cm]{geometry} % set page parameters as needed
\usepackage{amsmath,booktabs,dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\newcommand\mc[1]{\multicolumn{1}{>{$}c<{$}}{#1}} % handy shortcut macro
\begin{document}
\begin{table}[h]
\setlength\tabcolsep{0pt} % let LaTeX figure out intercolumn whitespace
\small % 10% linear reduction in font size
\caption{The table's caption\strut}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
l
*{10}{d{2.5}} % align numbers on decimal markers
}
\toprule
& \multicolumn{9}{c}{Dependent variables} \\
\cmidrule{2-11}
& \mc{\sigma^l} & \mc{\sigma^\pi} & \mc{\sigma^k}
& \mc{\text{ROE}} & \mc{\log(\mathrm{der})} & \mc{\log(\phi_l)}
& \mc{\log(\phi_k)} & \mc{\text{TFP}} & \mc{\text{HHI}} & \mc{\mu} \\
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)}
& \mc{(6)} & \mc{(7)} & \mc{(8)} & \mc{(9)} & \mc{(10)} \\
\midrule
\textbf{Top}
& -0.087 & -0.279 & 0.087 & -0.018 & 0.111
& -0.090 & -1.372^{*} & -7.257 & -0.006 & 3.279^{*} \\
$\times$\textbf{Post}
& (0.083) & (0.204) & (0.083) & (0.016) & (0.240)
& (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
\addlinespace
\tikzmark{top left 2}
\color{red}{Top30}
& -0.127^{***} & 0.115^{***} & 0.127^{***} & 0.004 & 0.102^{*}
& 0.102^{**} & 0.162 & 1.101 & -0.031^{***} & 1.214^{***} \\
\color{red}{PostRe}
& (0.018) & (0.044) & (0.018) & (0.004) & (0.055)
& (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
\addlinespace
Top3
& -0.100^{*} & 0.184 & 0.100^{*}
& 0.034^{***} & 0.077 & 0.282^{*}
& 1.456^{***} & -1.161 & 0.143^{***}
\ \ \tikzmark{bottom right 2} & 3.548^{***} \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176)
& (0.159) & (0.554) & (3.427) & (0.033) & (1.335) \\
\bottomrule
\addlinespace
\multicolumn{11}{@{}l}{\footnotesize $^{*}\ p<0.1$; $^{**}\ p<0.05$; $^{***}\ p<0.01$} \\
\end{tabular*}
\DrawBox[ultra thick, blue]{top left 2}{bottom right 2}
\smallskip
\footnotesize\textit{Note}: \dots
\end{table}
\end{document}
答案2
无需使用 TikZ,在这里您可以找到使用tabularray
和的解决方案siunitx
:
\documentclass{article}
\usepackage{geometry}
\usepackage{float}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\UseTblrLibrary{siunitx}
\NewColumnType{A}[1][-1.3*]{Q[si={input-symbols = {()},
group-digits = false,
table-format=#1,
table-number-alignment=center,
explicit-sign,},c]}
\begin{document}
If you would like to include the last column:
\begin{table}[H]\centering
\begin{tblr}{
colspec={l*{4}{A}*{2}{A[1.3]}AA[-1.3]AA[-1.3**]},
row{1-3}={guard},
row{even[4]}={belowsep+=-1ex},
columns={font=\scriptsize},
column{1}={leftsep=1pt,rightsep=0pt},
cell{1}{2}={c=10}{c, font=\itshape\scriptsize},
cell{Y}{1}={c=11}{r, mode=math},
cell{2-Y}{2-Z}={mode=math},
cell{4-5}{1}={font=\bfseries\scriptsize},
cell{6-7}{1}={fg=red},
hline{1} = {1}{-}{solid},
hline{1} = {2}{-}{solid},
hline{2} = {2-Z}{solid,leftpos=-1,endpos},
hline{4} = {solid},
hline{6} = {blue, wd=2pt},
vline{1,Z} = {6-X}{blue,wd=2pt},
hline{X} = {1}{-}{blue,wd=2pt},
hline{X} = {2}{-}{solid},
hline{X} = {3}{-}{solid},
}
& Dependent variable: &&&&&&&&&\\
& \sigma^l & \sigma^\pi & \sigma^k & \textrm{ROE} & \log(\textrm{der}) & \log(\phi_l) & \log(\phi_k)& \textrm{TFP}& \textrm{HHI} &\mu \\
& (1) & (2) & (3) & (4) & (5) & (6) &(7) & (8) & (9) & (10)\\
Top & -0.087 & -0.279 & 0.087 & -0.018 & 0.111 & -0.090 & -1.372* & -7.257 & -0.006 & 3.279* \\
$\times$Post & (0.083) & (0.204) & (0.083) & (0.016) & (0.240) & (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
Top30 & -0.127*** & 0.115*** & 0.127*** & 0.004 & 0.102* & 0.102** & 0.162 & 1.101 & -0.031*** & 1.214*** \\
PostRe & (0.018) & (0.044) & (0.018) & (0.004) & (0.055) & (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
Top3 & -0.100* & 0.184 & 0.100* & 0.034*** & 0.077 & 0.282* & 1.456*** & -1.161 & 0.143*** & 3.548*** \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176) & (0.159) & (0.554) & (3.427) & (0.033) & (1.335) \\
^{*}\textrm{p}<0.1; ^{**}\textrm{p}<0.05; ^{***}\textrm{p}<0.01 &&&&&&&&&&\\
Note:&&&&&&&&&&\\
\end{tblr}
\end{table}
If you would \emph{not} like to include the last column:
\begin{table}[H]\centering
\begin{tblr}{
colspec={l*{4}{A}*{2}{A[1.3]}AA[-1.3]A[-1.3**]A[1.3**]},
row{1-3}={guard},
row{even[4]}={belowsep+=-1ex},
columns={font=\scriptsize},
column{1}={leftsep=1pt,rightsep=0pt},
cell{1}{2}={c=10}{c, font=\itshape\scriptsize},
cell{Y}{1}={c=11}{r, mode=math},
cell{2-Y}{2-Z}={mode=math},
cell{4-5}{1}={font=\bfseries\scriptsize},
cell{6-7}{1}={fg=red},
hline{1} = {1}{-}{solid},
hline{1} = {2}{-}{solid},
hline{2} = {2-Z}{solid,leftpos=-1,endpos},
hline{4} = {solid},
hline{6} = {1-Y}{blue, wd=2pt},
vline{1,Y} = {6-X}{blue,wd=2pt},
hline{X} = {1}{1-Y}{blue,wd=2pt},
hline{X} = {2}{-}{solid},
hline{X} = {3}{-}{solid},
}
& Dependent variable: &&&&&&&&&\\
& \sigma^l & \sigma^\pi & \sigma^k & \textrm{ROE} & \log(\textrm{der}) & \log(\phi_l) & \log(\phi_k)& \textrm{TFP}& \textrm{HHI} &\mu \\
& (1) & (2) & (3) & (4) & (5) & (6) &(7) & (8) & (9) & (10)\\
Top & -0.087 & -0.279 & 0.087 & -0.018 & 0.111 & -0.090 & -1.372* & -7.257 & -0.006 & 3.279* \\
$\times$Post & (0.083) & (0.204) & (0.083) & (0.016) & (0.240) & (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
Top30 & -0.127*** & 0.115*** & 0.127*** & 0.004 & 0.102* & 0.102** & 0.162 & 1.101 & -0.031*** & 1.214*** \\
PostRe & (0.018) & (0.044) & (0.018) & (0.004) & (0.055) & (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
Top3 & -0.100* & 0.184 & 0.100* & 0.034*** & 0.077 & 0.282* & 1.456*** & -1.161 & 0.143*** & 3.548*** \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176) & (0.159) & (0.554) & (3.427) & (0.033) & (1.335) \\
^{*}\textrm{p}<0.1; ^{**}\textrm{p}<0.05; ^{***}\textrm{p}<0.01 &&&&&&&&&&\\
Note:&&&&&&&&&&\\
\end{tblr}
\end{table}
\end{document}
答案3
在{NiceTabular}
中nicematrix
,您有一个命令\Block
可以在单元格矩形周围绘制一个框架。
\documentclass{article}
\usepackage[a4paper,margin=2.5cm]{geometry}
\usepackage{amsmath,booktabs}
\usepackage{siunitx}
\usepackage{nicematrix}
\usepackage{enumitem}
\usepackage{caption}
\newcommand\mc[1]{\multicolumn{1}{>{$}c<{$}}{#1}}
\begin{document}
\begin{table}[h]
\setlength{\tabcolsep}{0pt}
\small
\caption{The table's caption}
\begin{NiceTabular*}{\textwidth}{@{\extracolsep{\fill}}
l
*{10}{S[input-symbols = {()}]}
}[notes/para]
\toprule
& \Block{1-9}{Dependent variables} \\
\cmidrule{2-11}
& \mc{\sigma^l} & \mc{\sigma^\pi} & \mc{\sigma^k}
& \mc{\text{ROE}} & \mc{\log(\mathrm{der})} & \mc{\log(\phi_l)}
& \mc{\log(\phi_k)} & \mc{\text{TFP}} & \mc{\text{HHI}} & \mc{\mu} \\
& \mc{(1)} & \mc{(2)} & \mc{(3)} & \mc{(4)} & \mc{(5)}
& \mc{(6)} & \mc{(7)} & \mc{(8)} & \mc{(9)} & \mc{(10)} \\
\midrule
\textbf{Top}
& -0.087 & -0.279 & 0.087 & -0.018 & 0.111
& -0.090 & -1.372\tabularnote{$p<0.1$} & -7.257 & -0.006 & 3.279\tabularnote{$p<0.1$} \\
$\times$\textbf{Post}
& (0.083) & (0.204) & (0.083) & (0.016) & (0.240)
& (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
\addlinespace
\Block[draw=blue,line-width=1pt]{2-10}{}
\color{red}{Top30}
& -0.127\tabularnote{$p<0.01$} & 0.115\tabularnote{$p<0.01$} & 0.127\tabularnote{$p<0.01$} & 0.004 & 0.102\tabularnote{$p<0.1$}
& 0.102\tabularnote{$p<0.005$} & 0.162 & 1.101 & -0.031\tabularnote{$p<0.01$} & 1.214\tabularnote{$p<0.01$} \\
\color{red}{PostRe}
& (0.018) & (0.044) & (0.018) & (0.004) & (0.055)
& (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
\addlinespace
Top3
& -0.100\tabularnote{$p<0.1$} & 0.184 & 0.100\tabularnote{$p<0.1$}
& 0.034\tabularnote{$p<0.01$} & 0.077 & 0.282\tabularnote{$p<0.1$}
& 1.456\tabularnote{$p<0.01$} & -1.161 & 0.143\tabularnote{$p<0.01$}
& 3.548\tabularnote{$p<0.01$} \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176)
& (0.159) & (0.554) & (3.427) & (0.033) & (1.335) \\
\bottomrule
\end{NiceTabular*}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。
答案4
另一个解决方案是使用tabularray
包。与 CarLaTeX 答案非常相似(第一个 +1 是我的),但使用了talltblr
表和booktabs
库siunitx
:
\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\begin{document}
\begin{table}[ht]
\scriptsize
\begin{talltblr}[
label=none, % <---
entry=none, % <---
remark{Notes} = { $^{*}\ p<0.1$; $^{**}\ p<0.05$; $^{***}\ p<0.01$.}
]{colspec = {Q[l, font=\bfseries]
*{10}{X[c, si={table-format=-1.3{**},
input-symbols={()}}]} },
colsep = 2pt,
row{1-3} = {guard},
row{2} = {mode=math},
row{odd[5]}= {abovesep=0pt},
hline{6,8} = {1-Y}{blue, 1pt},
vline{1,Y} = {6-7}{blue, 1pt},
}
\toprule
& \SetCell[c=10]{c} Dependent variables
& & & & (5) & (6) & (7) & (8) & (9) & (10) \\
\cmidrule{2-11}
& \sigma^l & \sigma^\pi & \sigma^k
& \mathrm{ROE} & \log(\mathrm{der}) & \log(\phi_l)
& \log(\phi_k) & \mathrm{TFP} & \mathrm{HHI}
& \mu \\
& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) \\
\midrule
Top & -0.087 & -0.279 & 0.087 & -0.018 & 0.111 & -0.090 & -1.372* & -7.257 & -0.006 & 3.279* \\
$\times$Post
& (0.083) & (0.204) & (0.083) & (0.016) & (0.240) & (0.217) & (0.756) & (4.679) & (0.046) & (1.823) \\
\SetCell{fg=red} Top30
& -0.127*** & 0.115*** & 0.127*** & 0.004 & 0.102* & 0.102** & 0.162 & 1.101 & -0.031*** & 1.214*** \\
\SetCell{fg=red} PostRE
& (0.018) & (0.044) & (0.018) & (0.004) & (0.055) & (0.049) & (0.152) & (1.070) & (0.011) & (0.424) \\
Top3 & -0.100* & 0.184 & 0.100* &0.034*** & 0.077 & 0.282* & 1.456*** & -1.161 & 0.143*** & 3.548*** \\
& (0.061) & (0.149) & (0.061) & (0.012) & (0.176) & (0.159) & (0.554) &(3.427) & (0.033) & (1.335) \\
\bottomrule
\end{talltblr}
\end{table}
\end{document}