2

This question is a continuation of How to combine mdframed and tabu

I have lots of rows and it won't work with tabluarx. I would like to present this rounded box around the heading rows of a longtable or longtabu.

Example:

\documentclass[a5paper,10pt]{article}
\usepackage[many]{tcolorbox}
\usepackage{array,tabularx}
\usepackage{colortbl}
\usetikzlibrary{calc}

\newcommand{\Repeat}[2]{% \repeat already defined
    \foreach \n in {1,...,#1}{#2}
}

\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 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
LAST &  9999-99-99 & 999 & 9 & 99999 \\

\end{tcolorbox}

\end{document}

enter image description here

I need to use a longtable or a longtabu here. And tcolorbox does not work together with longtable or longtabu. I'm now thinking about drawing the background of the header cells with tikz, but not sure how.

UPDATE:

\begin{longtabu} {cc}
\boxedheader{ \textbf{D1} & \textbf{D2} }
2014-01-01 & 2014-01-01 \\
2014-01-01 & 2014-01-01 \\
\end{longtabu}

Result:

enter image description here

Possible workaround is to create fake columns with invisible content:

\setlength{\tabcolsep}{1pt}
\begin{longtabu} {lcp{12pt}cr}
\boxedheader{ & \textbf{D1} & & \textbf{D2} & }
& 2014-01-01 & & 2014-01-01 & \\
& 2014-01-01 & & 2014-01-01 & \\
\end{longtabu}

enter image description here

1 Answer 1

5

You could adapt tcolorbox to be used together with longtabu. But if the the table header should be boxed only, I would suggest to use a pure tikz solution (plus tikzmark):

\documentclass[a5paper,10pt]{article}
\usepackage{array,tabularx,longtable,tabu}
\usepackage{colortbl,tikz}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}

\newcounter{headercounter}

\newcommand{\boxedheader}[1]{%
  \stepcounter{headercounter}%
  \begin{tikzpicture}[remember picture]
  \path[thick,rounded corners=2mm,draw=black,fill=gray!40!white,overlay]
    (-0.2,-0.25\baselineskip) rectangle ([xshift=2mm,yshift=0.75\baselineskip]pic cs:tableright\roman{headercounter});
  \end{tikzpicture}%
  #1
  \tikzmark{tableright\roman{headercounter}}\\%
}

\begin{document}

\begin{longtabu} to \linewidth {@{}Xcrrr@{}}
\boxedheader{\textbf{Name} & \textbf{Date} & \textbf{Price} & \textbf{Qty} & \textbf{Total}}
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
Some name here &  2014-01-01 & 420 & 2 & 820 \\
LAST &  9999-99-99 & 999 & 9 & 99999 \\
\end{longtabu}

\end{document}

enter image description here

If the first parameter is not X, an absoult positioning without \baselineskip seems to be needed:

\newcommand{\boxedheader}[1]{%
  \stepcounter{headercounter}%
  \begin{tikzpicture}[remember picture]
  \path[thick,rounded corners=2mm,draw=black,fill=gray!40!white,overlay]
    (-0.2,-0.1) rectangle ([xshift=2mm,yshift=3mm]pic cs:tableright\roman{headercounter});
  \end{tikzpicture}%
  #1
  \tikzmark{tableright\roman{headercounter}}\\%
}
10
  • I'm excited about this solution. Please see the update in the question.
    – nagylzs
    Commented Sep 16, 2014 at 13:17
  • Updated the question. I'll be accepting this solution but will wait 24 hours because using these hand typed labels for \boxedheader invocations is very clumsy. There must be a workaround.
    – nagylzs
    Commented Sep 16, 2014 at 14:14
  • 1
    @nagylzs See my updated answer. It should work now for many tables (compiling twice). By the way: if it's sure that all tables have \textwidth, there would be an easier solution with hardcoding the table head width (omitting the tikzmark). Commented Sep 16, 2014 at 15:44
  • @nagylzs I'm glad I could help :-) Commented Sep 17, 2014 at 8:18
  • 1
    @nagylzs I've added an alternative which works for l and c also. Commented Sep 18, 2014 at 12:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .