3
\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}

The problem is obvious:

enter image description here

I think this must be a single table, and mdframed should be used just for the first row, not the whole table. But how? Maybe mdframed is not the right solution for this?

Desired output:

enter image description here

UPDATE: for long tables see longtabu + rounded box in the header

  • 1
    Does moving \end{mdframed} to be after \end{tabu} do what you want? I am not exactly sure what the desired outcome is: \begin{mdframed}[style=graybox] \begin{tabu} to \linewidth {@{}Xcrrr@{}} \textbf{Name} & \textbf{Date} & \textbf{Price} & \textbf{Qty} & \textbf{Total} \\ Some name here & 2014-01-01 & 420 & 2 & 820 \end{tabu} \end{mdframed} – Peter Grill Sep 16 '14 at 8:56
  • Desired outcome is that columns are aligned under each other. E.g. date value is under the "Date" column. But the frame should only show up in the first row (heading). Not the others. – nagylzs Sep 16 '14 at 9:08
4

The way in which you are using it, the problem is with the column widths. Further, I would prefer tcolorbox over 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}

enter image description here

\end{document}

Here is an example to show the advantage of tcolorbox in which we use only one table.

\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}

enter image description here

For auto sized columns redefine C and R columns

\newcolumntype{C}{>{\centering\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

Or even better

\newcolumntype{R}{>{\raggedleft\arraybackslash\hsize=0.5\hsize}X}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=0.5\hsize}X}
  • Yes, the question was about column widths. This was a MWE. I wanted a solution that works with multiple auto sized columns. So I think using two tables will never work in general. But in this particular case it does. BTW what is the advantage of using tcolorbox instead of mdframed? – nagylzs Sep 16 '14 at 9:29
  • @nagylzs See the update for the advantages ;-) – user11232 Sep 16 '14 at 9:32
  • This is now almost perfect. To be honest, I don't understand the tabularx*= part. Will this work with longtabu as well? Unfortunately, there will (probably) be lots of rows that don't fit onto a single page. – nagylzs Sep 16 '14 at 9:41
  • @nagylzs This may be a worth candidate for a new question. Please ask new one. – user11232 Sep 16 '14 at 11:18
  • New question asked. tex.stackexchange.com/questions/201419/… – nagylzs Sep 16 '14 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.