TransWikia.com

Tcolorbox rule width with table inside

TeX - LaTeX Asked on March 20, 2021

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=bfserieslarge,fontupper=normalsizesffamily,
    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 hlinehline
    1000.00 & 2000.00 & 3000.00hline
    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 Answers

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=bfserieslarge,fontupper=normalsizesffamily,
    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}defarraystretch{1.1}#1%
      tcb@hack@currenvirtabularx{linewidth}{#2}},
    after upper=endtabularxarrayrulecolor{black}}}
makeatother
begin{tcolorbox}[tabularx*={arrayrulewidth0.5mm}{X|X|X},title=My table]
    One
    & Two
    & Three hlinehline
    1000.00 & 2000.00 & 3000.00hline
    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*={%
      lettcb@CT@arc@saveCT@arc@%
      arrayrulecolor{tcbcolframe}defarraystretch{1.1}#1%
      tcb@hack@currenvirtabularx{linewidth}{#2}},
    after upper*={endtabularxgloballetCT@arc@tcb@CT@arc@save}%
  }
}

Correct answer by Marijn on March 20, 2021

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=bfserieslarge,
    fontupper=normalsizesffamily,
    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 hlinehline
    1000.00 & 2000.00 & 3000.00hline
    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}%
      defpgfkeys@marshal{expandaftergdefexpandafterpgfkeys@global@temppgfkeys@tempargs}%
      expandafterpgfkeys@marshalexpandafter{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.
    edefpgf@expanded@path{pgfkeyscurrentpath}%
    {%
      pgfkeysifdefined{pgf@expanded@path/.@cmd}{%
        expandafterexpandafterexpandafterpgfkeys@temptoks
        expandafterexpandafterexpandafter{%
        csname pgfk@pgf@expanded@path/.@bodyendcsname}%
      }{%
        expandafterpgfkeys@temptoksexpandafter{%
        expandafterpgfkeyssetvalueexpandafter{pgf@expanded@path}{##1}}%
      }%
      edefpgfkeys@temp{thepgfkeys@temptoks}%
      pgfkeyspatchvaluepgfkeys@temp{#1}{#2}%
      globalletpgfkeys@global@temppgfkeys@temp
    }%
    defpgf@temp{pgfkeyssetvalue{pgf@expanded@path/.@body}}%
    expandafterpgf@tempexpandafter{pgfkeys@global@temp}%
    expandafterlongexpandafterdefexpandafterpgfkeys@tempexpandafter##expandafter1expandafterpgfeovexpandafter{pgfkeys@global@temp}%
    pgfkeyslet{pgf@expanded@path/.@cmd}pgfkeys@temp
  }%  
}

% usage:
%   pgfkeyspatchvalue{<key>}{<search>}{<replace>}
% similar to pgfkeysaddvalue
longdefpgfkeyspatchvalue#1#2#3{%
  pgfkeysifdefined{#1}{%
    expandafterxpatchcmdcsname pgfk@#1endcsname{#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    hlinehline
  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    hlinehline
  1000.00 & 2000.00 & 3000.00  hline
  2000.00 & 3000.00 & 4000.00
end{tcolorbox}

end{document}

example of new pgfkeys handler ".patch code"

Answered by muzimuzhi Z on March 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP