3

Apparently, the boxrule command has not effect with an enhanced tcolorbox having a tabularx inside.

How can I specify the width of the frame arount the tabularx ?

Here is the MWE :

\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{array,tabularx}
\usepackage{colortbl}
\begin{document}

% \usepackage{array,tabularx}
% \usepackage{colortbl} - or - \usepackage[table]{xcolor}
\tcbset{enhanced,fonttitle=\bfseries\large,fontupper=\normalsize\sffamily,
    colback=yellow!10!white,colframe=red!50!black,colbacktitle=red!30!white,
    coltitle=black,center title,
    boxrule=10pt, % NO EFFECT
}
\begin{tcolorbox}[tabularx*={\arrayrulewidth0.5mm}{X|X|X},title=My table]
    One
    & Two
    & Three \\\hline\hline
    1000.00 & 2000.00 & 3000.00\\\hline
    2000.00 & 3000.00 & 4000.00
\end{tcolorbox}

% EDIT : with a multicolumn as 1st cell this does not compile anymore
\begin{tcolorbox}[boxrule=2pt,tabularx*={\arrayrulewidth2pt}{YY|YY|YY},
    title=My table
    ]
    \multicolumn{2}{c|}{title}
    &   \multicolumn{2}{c|}{title}
    &   \multicolumn{2}{c}{title} \\\hline
    2 & d & 3 & e & 4 & f \\\hline
    title & \multicolumn{3}{|l|}{h} & \multicolumn{1}{c|}{title} & j \\
\end{tcolorbox}

\end{document}

EDIT : the first answer is alright for the 1st table in the MWE, but I'm using a multicolumn as 1st cell and this brings an Misplaces \omit error. MWE has been modified with the problematic table.

2
  • Should be possible to set it for individual boxes; would it be sufficient or not? Aug 20, 2020 at 10:11
  • In the modified MWE, tabular column Y is undefined. Aug 21, 2020 at 0:54

2 Answers 2

6

The definition of the tabularx style has as setting for the boxrule to be 0.5mm. You can overwrite the setting by copying the style from the source and removing this part:

\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{array,tabularx}
\usepackage{colortbl}
\begin{document}

% \usepackage{array,tabularx}
% \usepackage{colortbl} - or - \usepackage[table]{xcolor}
\tcbset{enhanced,fonttitle=\bfseries\large,fontupper=\normalsize\sffamily,
    colback=yellow!10!white,colframe=red!50!black,colbacktitle=red!30!white,
    coltitle=black,center title,
    boxrule=10pt, % NO EFFECT
}
\makeatletter
\tcbset{tabularx*/.style 2 args={%
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,% boxrule setting here removed
    before upper={\arrayrulecolor{tcbcol@frame}\def\arraystretch{1.1}#1%
      \tcb@hack@currenvir\tabularx{\linewidth}{#2}},
    after upper=\endtabularx\arrayrulecolor{black}}}
\makeatother
\begin{tcolorbox}[tabularx*={\arrayrulewidth0.5mm}{X|X|X},title=My table]
    One
    & Two
    & Three \\\hline\hline
    1000.00 & 2000.00 & 3000.00\\\hline
    2000.00 & 3000.00 & 4000.00
\end{tcolorbox}
\end{document}

Result:

enter image description here

Edit: in more recent versions of tcolorbox the definition is slightly changed. In that case the new definition should be as follows:

\tcbset{tabularx*/.style 2 args={%
    boxsep=\z@,top=\z@,bottom=\z@,leftupper=\z@,rightupper=\z@,
    toptitle=1mm,bottomtitle=1mm,
    before upper*={%
      \let\tcb@CT@arc@save\CT@arc@%
      \arrayrulecolor{tcbcolframe}\def\arraystretch{1.1}#1%
      \tcb@hack@currenvir\tabularx{\linewidth}{#2}},
    after upper*={\endtabularx\global\let\CT@arc@\tcb@CT@arc@save}%
  }
}
7
  • Thanks ! However tcbcol@frame -> tcbcolframe to be able to compile the MWE ;)
    – 3isenHeim
    Aug 20, 2020 at 12:34
  • This does not work when the 1st cell of the table is a multicolumn... With this tcolorbox I get a Misplaced \omit due to the 1st multicolumn. \begin{tcolorbox}[boxrule=2pt,tabularx*={\arrayrulewidth2pt}{YY|YY|YY}, title=My table ] \multicolumn{2}{c|}{title} & \multicolumn{2}{c|}{title} & \multicolumn{2}{c}{title} \\\hline 2 & d & 3 & e & 4 & f \\\hline title & \multicolumn{3}{|l|}{h} & \multicolumn{1}{c|}{title} & j \\ \end{tcolorbox}
    – 3isenHeim
    Aug 20, 2020 at 12:56
  • @3isenHeim I get that error also without the redefinition of the tabular style, so that is a separate issue. You could ask a new question about it.
    – Marijn
    Aug 20, 2020 at 13:45
  • I don''t get the error when I remove the redefinition.
    – 3isenHeim
    Aug 20, 2020 at 14:12
  • 1
    Then it is probably a version problem (I use a slightly older version of tcolorbox). The second part of the definition in the latest (4.31 2020/07/31) version is before upper*={% \let\tcb@CT@arc@save\CT@arc@% \arrayrulecolor{tcbcolframe}\def\arraystretch{1.1}#1% \tcb@hack@currenvir\tabularx{\linewidth}{#2}}, after upper*={\endtabularx\global\let\CT@arc@\tcb@CT@arc@save}%, note the * at before and after upper, tcbcolframe without @, and the parts about CT@arc that are not present in my version.
    – Marijn
    Aug 20, 2020 at 14:53
0

If the need is to change value of boxrule specified by option tabularx*, then tabularx*/.append style={boxrule=<dimen>} is simpler, in which.append style is one of predefined pgfkeys handlers.

Full example:

\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{array,tabularx}
\usepackage{colortbl}

\tcbset{
    enhanced,
    fonttitle=\bfseries\large,
    fontupper=\normalsize\sffamily,
    colback=yellow!10!white,
    colframe=red!50!black,
    colbacktitle=red!30!white,
    coltitle=black,
    center title,
    % change boxrule set by "tabularx*", by default it's 0.5mm
    tabularx*/.append style={boxrule=10pt}
}

\begin{document}
\begin{tcolorbox}[tabularx*={\arrayrulewidth0.5mm}{X|X|X}, title=My table]
    One
    & Two
    & Three \\\hline\hline
    1000.00 & 2000.00 & 3000.00\\\hline
    2000.00 & 3000.00 & 4000.00
\end{tcolorbox}
\end{document}

enter image description here

Update

Just for fun. Here a new pgfkeys handler .patch code={<search>}{<replace>} which internally used \xpatchcmd from xpatch package, is defined.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xpatch}

\usepackage{array,tabularx}
\usepackage{colortbl}

\makeatletter
% usage:
%   <key>/.patch code={search}{replace}
% similar to def of /handlers/.add code/.code 2 args={...}
\pgfkeys{/handlers/.patch/.code 2 args=%
  % Find out, whether with args or not.
  \pgfkeysifdefined{\pgfkeyscurrentpath/.@args}%
  {% Yes, so add to body and reuse args
    \pgfkeyspatchvalue{\pgfkeyscurrentpath/.@body}{#1}{#2}%
    % Redefine code
    {%
      \pgfkeysgetvalue{\pgfkeyscurrentpath/.@args}{\pgfkeys@tempargs}%
      \pgfkeysgetvalue{\pgfkeyscurrentpath/.@body}{\pgfkeys@tempbody}%
      \def\pgfkeys@marshal{\expandafter\gdef\expandafter\pgfkeys@global@temp\pgfkeys@tempargs}%
      \expandafter\pgfkeys@marshal\expandafter{\pgfkeys@tempbody}%
    }%
    \pgfkeysifdefined{\pgfkeyscurrentpath/.@@body}{%
      % support for \pgfkeysndefargs:
      \pgfkeyslet{\pgfkeyscurrentpath/.@@body}{\pgfkeys@global@temp}%
    }{%
      % support for \pgfkeysdefargs:
      \pgfkeyslet{\pgfkeyscurrentpath/.@cmd}{\pgfkeys@global@temp}%
    }%
  }{% No, so single argument (simple \pgfkeysdef). Redefine accordingly.
    \edef\pgf@expanded@path{\pgfkeyscurrentpath}%
    {%
      \pgfkeysifdefined{\pgf@expanded@path/.@cmd}{%
        \expandafter\expandafter\expandafter\pgfkeys@temptoks
        \expandafter\expandafter\expandafter{%
        \csname pgfk@\pgf@expanded@path/.@body\endcsname}%
      }{%
        \expandafter\pgfkeys@temptoks\expandafter{%
        \expandafter\pgfkeyssetvalue\expandafter{\pgf@expanded@path}{##1}}%
      }%
      \edef\pgfkeys@temp{\the\pgfkeys@temptoks}%
      \pgfkeyspatchvalue\pgfkeys@temp{#1}{#2}%
      \global\let\pgfkeys@global@temp\pgfkeys@temp
    }%
    \def\pgf@temp{\pgfkeyssetvalue{\pgf@expanded@path/.@body}}%
    \expandafter\pgf@temp\expandafter{\pgfkeys@global@temp}%
    \expandafter\long\expandafter\def\expandafter\pgfkeys@temp\expandafter##\expandafter1\expandafter\pgfeov\expandafter{\pgfkeys@global@temp}%
    \pgfkeyslet{\pgf@expanded@path/.@cmd}\pgfkeys@temp
  }%  
}

% usage:
%   \pgfkeyspatchvalue{<key>}{<search>}{<replace>}
% similar to \pgfkeysaddvalue
\long\def\pgfkeyspatchvalue#1#2#3{%
  \pgfkeysifdefined{#1}{%
    \expandafter\xpatchcmd\csname pgfk@#1\endcsname{#2}{#3}{}
      {\PackageError{pgfkeys}{Patching for key #1 fails}}%
  }{}%
}
\makeatother


\begin{document}

\tcbset{
  colframe=black!25,
  colbacktitle=black!75
}

\begin{tcolorbox}[boxrule=5pt, tabularx*={\arrayrulewidth0.5mm}{X|X|X}, title=Before patching]
  One     & Two     & Three   \\ \hline\hline
  1000.00 & 2000.00 & 3000.00 \\ \hline
  2000.00 & 3000.00 & 4000.00
\end{tcolorbox}

% patch option "tabularx*", remove "boxrule=0.5mm"
\tcbset{
  tabularx*/.patch={boxrule=0.5mm}{}
}

\begin{tcolorbox}[boxrule=5pt, tabularx*={\arrayrulewidth0.5mm}{X|X|X}, title=After patching]
  One     & Two     & Three   \\ \hline\hline
  1000.00 & 2000.00 & 3000.00 \\ \hline
  2000.00 & 3000.00 & 4000.00
\end{tcolorbox}

\end{document}

example of new pgfkeys handler ".patch code"

2
  • Thanks, but then it's hard-coded, and you loose the capacity of specifying it as you define the tcolorbox...
    – 3isenHeim
    Aug 21, 2020 at 7:59
  • @3isenHeim Yes. I've added a new-pgfkeys-handler approach to my answer. Aug 21, 2020 at 9:57

You must log in to answer this question.

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