两个平行的 tcolorbox 表

两个平行的 tcolorbox 表

我尝试在页面的一部分放置两个平行的 tcolorbox 表:

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

但我失败了。有没有办法用 multicols 或其他工具来做到这一点?

我倾向于保持 mytable 环境命令不变。

答案1

您可能 -- 或者您真的不想让环境浮动。

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

修改后的答案:如果您不想更改mytable环境,那么一个简单的方法是将表格包装在命令中,如下面的\ccolumns命令。由于nobeforeafterkey from ,您可以懒得处理虚假空格tcolorbox,但无论如何,更小心可能是一个好习惯。

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

答案2

我的最终代码:

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

在此处输入图片描述

相关内容