5

In the following code

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}

%table
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\normalsize\sffamily‍‍,
colback=white!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}

\newcommand{\btable}[2]{\begin{table}
\refstepcounter{table}\label{#2}
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.7}}{#1},title=Table \ref{#2},boxrule=0.8pt]}

\newcommand{\ntable}{\end{tcolorbox} \end{table}}
%end ot table commands


\begin{document}

\btable{Y|Y|Y|Y|Y|Y}{kk}
group & one     & two     & three    & four     & sum      \\\hline
red   & 1000.00 & 2000.00 &  3000.00 &  4000.00 & 10000.00 \\\hline
green & 2000.00 & 3000.00 &  4000.00 &  5000.00 & 14000.00 \\\hline
blue  & 3000.00 & 4000.00 &  5000.00 &  6000.00 & 18000.00 \\\hline
sum   & 6000.00 & 9000.00 & 12000.00 & 15000.00 & 42000.00
%\end{tcolorbox}
%\end{table}
\ntable

\end{document}

the \ntable produces the error

! File ended while scanning use of \TX@get@body.

How can I fix it?

5
  • uhm I don't think is that good defining a command for the end of a group. Maybe it's this that causes you some problems; have you tried \newenvironment? Maybe that can work.
    – Moriambar
    Oct 23, 2015 at 7:36
  • Can \newenvironment get parameters? Oct 23, 2015 at 7:37
  • Yes, of course. You can find a bit of documentation here on page 121
    – Moriambar
    Oct 23, 2015 at 7:42
  • You can define a new tcolorbox itself. I will try to post something in some time.
    – user11232
    Oct 23, 2015 at 8:02
  • 1
    It's not a problem with the command, just the user:-) See the documentation of the tabularx package for ways to include tabularx in an environment definition. (It would be much better to use environment syntax than \btable \ntable environments are a core syntactic construct in latex, it is best not to hide them, although for tabularx the same restrictions apply whichever form you use. Oct 23, 2015 at 8:09

2 Answers 2

5

My simplest solution would be to load the environ package and to define a new environment

\usepackage{environ}
\NewEnviron{mytable}[2]%
{%
 \begin{table}%
 \refstepcounter{table}\label{#2}%
 \begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.7}}{#1},title=Table \ref{#2},boxrule=0.8pt]%
 \BODY%
 \end{tcolorbox}%
 \end{table}%
}

so that you can call your table as

\begin{mytable}{Y|Y|Y|Y|Y|Y}{kk}
... whatever ...
\end{mytable}

You could do this also with the plain \newenvironment, but it would be trickier...

Just out of completeness: If you really want to stick to the \btable{Y|Y|Y|Y|Y|Y}{kk} ... \ntable syntax you could use \def, as in

\def\btable#1#2#3\ntable{%
\begin{table}%
\refstepcounter{table}\label{#2}%
\begin{tcolorbox}[tab2,tabularx*={\renewcommand{\arraystretch}{1.7}}{#1},title=Table \ref{#2},boxrule=0.8pt]%
#3%
\end{tcolorbox}%
\end{table}%
}

but I rather discourage this solution.

7

No need of using another package. This can be done with tcolorbox itself.

\newtcolorbox{mytable}[3][]{
  tab2,
  tabularx*={\renewcommand{\arraystretch}{1.7}}{#2},
  title=Table \ref{#3},
  boxrule=0.8pt,
  before={\begin{table}[htb]\refstepcounter{table}\label{#3}},
  after={\end{table}},
  #1
}

Then the mytable environment will do the job.

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\tcbuselibrary{skins}

%table
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\tcbset{tab2/.style={enhanced,fonttitle=\bfseries,fontupper=\normalsize\sffamily‍‍,
colback=white!10!white,colframe=red!50!black,colbacktitle=Salmon!40!white,
coltitle=black,center title}}

\newtcolorbox{mytable}[3][]{
  tab2,
  tabularx*={\renewcommand{\arraystretch}{1.7}}{#2},
  title=Table \ref{#3},
  boxrule=0.8pt,
  before={\begin{table}[htb]\refstepcounter{table}\label{#3}},
  after={\end{table}},
  #1
}

\begin{document}

\begin{mytable}{Y|Y|Y|Y|Y|Y}{kk}
group & one     & two     & three    & four     & sum      \\\hline
red   & 1000.00 & 2000.00 &  3000.00 &  4000.00 & 10000.00 \\\hline
green & 2000.00 & 3000.00 &  4000.00 &  5000.00 & 14000.00 \\\hline
blue  & 3000.00 & 4000.00 &  5000.00 &  6000.00 & 18000.00 \\\hline
sum   & 6000.00 & 9000.00 & 12000.00 & 15000.00 & 42000.00
\end{mytable}

\end{document}

enter image description here

You must log in to answer this question.

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