1

I am currently trying to use the comment package to conditionnaly hide some custom tcolorbox, but I keep getting errors. I suspect this answer addresses the root of the problem, but I don't understand how to apply the suggested fix to my own use of newtcolorbox...

Here is a MWE (inspired by but a lot more simplified than my current code, of course):

\documentclass[a4paper, 11pt]{article}
\usepackage{comment}
\usepackage{tcolorbox}

\newtcolorbox{myTestTCB}[1][title={default title}]{
    #1
}

\excludecomment{myTestTCB}

\begin{document}

\begin{myTestTCB}[title=custom title]
    hidden content
\end{myTestTCB}

\end{document}

which gives me the following error (this is an extract, but as far as I know, it is the first useful message in the log):

[...] Excluding 'myTestTCB' comment.
/.../mwe.tex:12: Undefined control sequence.
\endtcolorbox ->\tcb@insert@after@part 
                                       \end {tcb@savebox}\tcb@reset@color \t...
l.12 \end{myTestTCB}
                    
/.../mwe.tex:12: Too many }'s.
\endtcb@savebox ...efalse \color@endgroup \egroup 
                                                  \expandafter \@iiiparbox \...
l.12 \end{myTestTCB}
[...]

This MWE works fine when not using excludecomment. How should I proceed to fix this?

1 Answer 1

3

You could achieve your goal without the comment package. Simply redefine the myTestTCB environment, so that it do nothing. For this, you need to use \RenewDocumentEnvironment (from xparse package, but you don't need to load it if you have recent LaTeX, as it is yet in his kernel) instead of renewenvironment because the former let you to capture the material in the environment, so we can just ignore it in order to not display it.

\documentclass[a4paper, 11pt]{article}

\usepackage{tcolorbox}

\newtcolorbox{myTestTCB}[1][title={default title}]{
    #1
}

\RenewDocumentEnvironment{myTestTCB}{o +b}{}{}

\begin{document}

Text

\begin{myTestTCB}[title=custom title]
    hidden content
\end{myTestTCB}

\end{document}

If you need a more advanced solution, you could define 2 commands (\deactivatemyTestTCB and \activatemyTestTCB). Example:

\documentclass[a4paper, 11pt]{article}
\usepackage{comment}
\usepackage{tcolorbox}

\newtcolorbox{myTestTCB}[1][title={default title}]{
    #1
}

\newcommand{\deactivatemyTestTCB}{\RenewDocumentEnvironment{myTestTCB}{o +b}{}{}}

\newcommand{\activatemyTestTCB}{\renewtcolorbox{myTestTCB}[1][title={default title}]{##1}}

\begin{document}

Text

\deactivatemyTestTCB

\begin{myTestTCB}[title=custom title]
    hidden content
\end{myTestTCB}

\activatemyTestTCB

\begin{myTestTCB}[title=custom title]
    visible content
\end{myTestTCB}

\end{document}

Note that you need to use ##1 instead of #1 in definition of \activatemyTestTCB because the parameter is embedded.

With this later code, you obtain only the "visible content".

enter image description here

1
  • Thank you. I had tried something with renewenvironment but due to my lack of experience, it didn't work. With your code, it does! And since my real tcolorbox has a way longer declaration, the activatemyTestTCB env would need to redeclare it all... but I don't have a use for it anyway. Thanks again!
    – Yosko
    Commented Feb 3, 2023 at 8:04

You must log in to answer this question.

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