1

I have made beautiful tables with the tabularx option to tcolorbox and everything was working great until I mixed it with some numbered boxed. Here is a MWE (that doesn't work):

\documentclass{memoir}

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

\newtcolorbox[auto counter]{simplebox}[2][]{%
title=Examp.~\thetcbcounter: #2,#1}

\begin{document}

\begin{simplebox}
This works!
\end{simplebox}

\begin{tcolorbox}[tabularx={X|X}]
No & 2\\\hline
3 & problem
\end{tcolorbox}

\begin{simplebox}{Some title}
\begin{tcolorbox}[tabularx={X|X}]
1 & 2\\\hline
3 & 4
\end{tcolorbox}
\end{simplebox}

\end{document}

The two boxes seems to work fine on their own but when one s placed inside another I just get a lot of errors. Can anybody give me a hint as to what I'm doing wrong?

Best regards, Mikkel

3

You can execute the inner table in a box register to avoid executing it in a nested context.

enter image description here

\documentclass{memoir}

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

\newsavebox\mybox
\newtcolorbox[auto counter]{simplebox}[2][]{%
title=Examp.~\thetcbcounter: #2,#1}

\begin{document}

\begin{simplebox}
This works!
\end{simplebox}

\begin{tcolorbox}[tabularx={X|X}]
No & 2\\\hline
3 & problem
\end{tcolorbox}

\savebox\mybox{%
\addtolength\linewidth{-40pt}% by eye...
\begin{tcolorbox}[tabularx={X|X}]
1 & 2\\\hline
3 & 4
\end{tcolorbox}%
}

\begin{simplebox}{Some title}%
\usebox\mybox
\end{simplebox}

\end{document}
  • Great thank you. Can you maybe give me an idea about what goes wrong and why this makes it better? – mstouby Apr 18 '17 at 6:34
  • 1
    @mstouby not exactly I didn't trace what went wrong but quite often environments need to make global settings to save things from inside inner boxes or table cells, and if they use global assignments they often do not nest well. if you save it in a box all macros and assignments are resolved before you nest so it's safer so i just tried that as an educated guess:-) – David Carlisle Apr 18 '17 at 6:56

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.