1

I tried to place two parallel tcolorbox tables in part of a page:

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


%mytable constuction:
\tcbset
{
    ponyotab/.style=
    {
         enhanced,
         fonttitle=\bfseries,
         fontupper=\normalsize\sffamily‍‍,
         colback=white!,
         colframe=black!65!white,
         colbacktitle=gray!20!white,
         coltitle=black,center title
    }
}
\newtcolorbox{mytable}[3][]
{
     ponyotab,
     tabular*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.0}}{#2},% change 1.0  to change cell heights
     title=table \ref{#3},
     before={\begin{table}[htb]\refstepcounter{table}\label{#3}\centering},
     after={\end{table}},
     #1
}


\makeatletter
\tcbset
    {
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\def\arraystretch{1.1}#1%
    \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother

\begin{document}

\begin{multicols}{2}

\begin{mytable}{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00 
\end{mytable}


\begin{mytable}{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00 
\end{mytable}

\end{multicols}

\end{document}

But I failed. Is there a way to do this with multicols or other tools?

I prefer to keep mytable environment command unchanged.

  • The problem is probably the before={\begin{table}[htb] and after={\end{table}} getting stuffed into a multicolumn environment. Do you want the tables to float or not? I probably also wouldn't use multicolumn at all, but something like the minipage environment. – jon Oct 25 '15 at 18:44
  • I tried minipage but it failed for me. – user62181 Oct 25 '15 at 18:45
  • Did you do my first suggestion? Also, you need to use the key nobeforefafter. (And then the minipage environment is probably unneeded.) – jon Oct 25 '15 at 19:08
  • Well I don't know how to change the code. – user62181 Oct 25 '15 at 19:12
3

You probably -- or do you? -- don't want to float the environment.

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


%mytable constuction:
\tcbset
{
    ponyotab/.style=
    {
         enhanced,
         fonttitle=\bfseries,
         fontupper=\normalsize\sffamily‍‍,
         colback=white!,
         colframe=black!65!white,
         colbacktitle=gray!20!white,
         coltitle=black,center title
    }
}
\newtcolorbox{mytable}[3][]
{
     ponyotab,
     tabular*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.0}}{#2},% change 1.0  to change cell heights
     title=table \ref{#3},
%     before={\begin{table}[htb]\refstepcounter{table}\label{#3}\centering},
%     after={\end{table}},
     nobeforeafter, % <-- added
     #1
}


\makeatletter
\tcbset
    {
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\def\arraystretch{1.1}#1%
    \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother

\begin{document}

% \begin{multicols}{2}

\noindent
\begin{mytable}{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}
%
\quad
%
\begin{mytable}{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}

%\end{multicols}

\end{document}

Revised answer: If you don't want to change the mytable environment, then one easy way is to wrap your tables in a command like the below \ccolumns command. You can be lazy about spurious spaces because of the nobeforeafter key from tcolorbox, though it's probably a good habit to be more careful anyway.

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


%mytable constuction:
\tcbset
{
    ponyotab/.style=
    {
         enhanced,
         fonttitle=\bfseries,
         fontupper=\normalsize\sffamily‍‍,
         colback=white!,
         colframe=black!65!white,
         colbacktitle=gray!20!white,
         coltitle=black,center title
    }
}
\newtcolorbox{mytable}[3][]
{
     ponyotab,
     tabular*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.0}}{#2},% change 1.0  to change cell heights
     title=table \ref{#3},
%     before={\begin{table}[htb]\refstepcounter{table}\label{#3}\centering},
%     after={\end{table}},
     nobeforeafter, % <-- added
     #1
}


\makeatletter
\tcbset
    {
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\def\arraystretch{1.1}#1%
    \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother

\newcommand{\ccolumns}[2]%
  {\par\noindent
    \begin{minipage}[t]{0.5\textwidth}%
      \begin{center}#1\end{center}\end{minipage}
    \begin{minipage}[t]{0.5\textwidth}%
      \begin{center}#2\end{center}\end{minipage}
}

\begin{document}

% \begin{multicols}{2}
\lipsum

\noindent
\begin{minipage}[t]{0.5\textwidth}
\begin{center}
\begin{mytable}{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}
%
\end{center}
\end{minipage}
%
\begin{minipage}[t]{0.5\textwidth}
\begin{center}
%
\begin{mytable}{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}
\end{center}
\end{minipage}

% \end{multicols}

\ccolumns{
\begin{mytable}{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}

}{

\begin{mytable}{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\end{mytable}

}

\end{document}
  • but I want each table be in the center of each half page. As if they are in two columns. – user62181 Oct 25 '15 at 19:23
  • @user62181 -- Do you mean the centre of each "column" where the dividing line of the two columns is itself the centre of the textblock? – jon Oct 25 '15 at 19:33
  • Yes, if I understand your words correctly. – user62181 Oct 25 '15 at 19:38
  • Thanks. But still there is a problem. The commented out line used to add table number. How can I restore the Table number? – user62181 Oct 25 '15 at 20:01
  • @user62181 -- Try this: \newtcolorbox[auto counter]{mytable}[3][]{ ponyotab, tabular*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.0}}{#2}, title={table \thetcbcounter}, nobeforeafter, #1 } in place of the original definition. I think your way was overly complicated. – jon Oct 25 '15 at 20:20
1

My final code:

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

%table
%mytable constuction:
\tcbset
{
    ponyotab/.style=
    {
         enhanced,
         fonttitle=\bfseries,
         fontupper=\normalsize\sffamily,
         colback=white!,
         colframe=black!65!white,
         colbacktitle=gray!20!white,
         coltitle=black,center title
    }
}
\makeatletter
\tcbset
    {
tabular*/.style 2 args={%
    boxsep=0pt,top=0pt,bottom=0pt,leftupper=0pt,rightupper=0pt,
    toptitle=1mm,bottomtitle=1mm,boxrule=0.5mm,hbox,
    before upper={\def\arraystretch{1.1}#1%
    \tcb@hack@currenvir\tabular{#2}},
    after upper=\endtabular\arrayrulecolor{black}},
    }
\makeatother

\newtcolorbox[auto counter,number within=chapter]{ptable}[3][]
{
     ponyotab,
     tabular*={\arrayrulecolor{black}\renewcommand{\arraystretch}{1.0}}{#2},% change 1.0  to change cell heights
     title=Table \ref{#3},
     label=#3,
%     before={\begin{table}[htb]\refstepcounter{table}\label{#3}\centering},
%     after={\end{table}},
     nobeforeafter, % <-- added
     #1
}

\makeatletter


\newcommand{\twocols}[2]%
  {\par\noindent
    \begin{minipage}[t]{0.5\textwidth}%
      \begin{center}#1\end{center}\end{minipage}
    \begin{minipage}[t]{0.5\textwidth}%
      \begin{center}#2\end{center}\end{minipage}
}

\newcommand{\onecol}[1]%
  {\par\noindent
    \begin{minipage}[t]{\textwidth}%
      \begin{center}#1\end{center}\end{minipage}
}

\def\btable#1#2#3\ntable
{
    \onecol
    {
        \begin{ptable}{#1}{#2}
        #3
        \end{ptable}
    }
}
%end table second type





\begin{document}

\chapter{oo}
\btable{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\ntable

% \end{multicols}

\twocols
{
\btable{c|c}{kk}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\ntable
}
{
\btable{c|c}{jj}
group & one      \\\hline
red   & 1000.00  \\\hline
green & 2000.00  \\\hline
blue  & 3000.00  \\\hline
sum   & 6000.00
\ntable
}

\end{document}

enter image description here

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.