![如何结合 mdframed 和 tabu](https://linux22.com/image/297920/%E5%A6%82%E4%BD%95%E7%BB%93%E5%90%88%20mdframed%20%E5%92%8C%20tabu.png)
\documentclass[a4paper,10pt]{article}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{multicol}
\usepackage{tabu}
\mdfdefinestyle{graybox}{
innertopmargin=2mm,%
innerbottommargin=2mm,%
roundcorner=2mm,%
backgroundcolor=gray!20}
\begin{document}
\begin{mdframed}[style=graybox]
\begin{tabu} to \linewidth {@{}Xcrrr@{}}
\textbf{Name} & \textbf{Date} & \textbf{Price} & \textbf{Qty} & \textbf{Total}
\end{tabu}
\end{mdframed}
\vskip - \baselineskip
\begin{tabu} to \linewidth {@{}Xcrrr@{}}
Some name here & 2014-01-01 & 420 & 2 & 820
\end{tabu}
\end{document}
问题很明显:
我认为这肯定是一张单表,并且 mdframed 应该只用于第一行,而不是整个表。但是该怎么做呢?也许 mdframed 不是解决这个问题的正确方法?
期望输出:
更新:有关长表,请参阅标题中的 longtabu + 圆角框
答案1
您使用它的方式存在问题,列宽也存在问题。此外,我更tcolorbox
喜欢mdframed
:
\documentclass[a4paper,10pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{multicol,array}
\usepackage{tabu}
\newtcolorbox{mybox}[1][]{%
enhanced jigsaw,
boxrule=0.5pt,
left=0pt,right=0pt,top=0mm,bottom=0mm,
colback = gray!20,
arc=2mm,
}
\newcolumntype{R}{>{\raggedleft\arraybackslash}p{1cm}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{mybox}
\begin{tabu} to \linewidth {@{\,}XC{2cm}RRR@{}}
\textbf{Name} & \textbf{Date} & \textbf{Price} & \textbf{Qty} & \textbf{Total}
\end{tabu}%
\end{mybox}%
\vskip-0.55\baselineskip
\noindent
\begin{tabu} to \linewidth {XC{2cm}C{1cm}C{1cm}C{1cm}@{}}
Some name here & 2014-01-01 & 420 & 2 & 820
\end{tabu}
\end{document}
tcolorbox
这里有一个例子来展示我们只使用一个表的优势。
\documentclass[a4paper,10pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{array,tabularx}
\usepackage{colortbl}
\usetikzlibrary{calc}
\tcbset{enhanced jigsaw,
boxrule=0pt,
fonttitle=\bfseries\large,
fontupper=\normalsize\sffamily,
colback=white,colframe=white,
overlay={
\draw[preaction={fill=gray!40},rounded corners=2mm,thick]
(frame.north west) rectangle
($(frame.north east) + (0,-1.25\baselineskip)$);
}
}
\newcolumntype{R}{>{\raggedleft\arraybackslash}p{1cm}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{tcolorbox}[tabularx*={\arrayrulewidth0mm}{XC{2cm}RRR}]
\textbf{Name} & \textbf{Date} & \textbf{Price} & \textbf{Qty} & \textbf{Total}\\
Some name here & 2014-01-01 & 420 & 2 & 820
\end{tcolorbox}
\end{document}
对于自动调整大小的列,重新定义C
和R
列
\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
甚至更好
\newcolumntype{R}{>{\raggedleft\arraybackslash\hsize=0.5\hsize}X}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=0.5\hsize}X}