5

I have an if statement below that toggles whether something is visible or not. I'm using this inside a tcolorbox as solutions to an example question.

What I'm hoping to do is be able to change the \ShowSoltrue to \ShowSolFalse and have the height of the tcolourbox unaffected. That is, I want it to be blank but the same height as if it were full.

Is there a way to achieve this?

\documentclass[12pt]{report}
\usepackage[most]{tcolorbox} 

\usepackage {lipsum}

\newtcolorbox{examplebox}[1][]{}

\newif\ifShowSol
\newcommand{\showsol}[1]{\ifShowSol #1 \fi}

\ShowSoltrue


\begin{document}

\begin{examplebox}
\textbf{Example 1.1}\\
\showsol{\lipsum[1-1]}
\end{examplebox}

\end{document}

enter image description here

Here is what currently happens when toggled to false - the box shrinks in height.

\documentclass[12pt]{report}
\usepackage[most]{tcolorbox} 

\usepackage {lipsum}

\newtcolorbox{examplebox}[1][]{}

\newif\ifShowSol
\newcommand{\showsol}[1]{\ifShowSol #1 \fi}

\ShowSolfalse


\begin{document}

\begin{examplebox}
\textbf{Example 1.1}\\
\showsol{\lipsum[1-1]}
\end{examplebox}

\end{document}

enter image description here

2
  • 1
    Try to write the solution with the same bkcolor of the box. So the same text is here but invisible. Commented Mar 16, 2023 at 8:06
  • in that case, text text might be selectable in the PDF isn't printed or compressed as an image.
    – anis
    Commented Mar 16, 2023 at 8:21

3 Answers 3

4

When I started working on this, Ignasi's answer (+1) was not there yet, so I'll just add mine although it is quite similar to Ignasi's (. The main difference is that I keep your idea of \ifShowSol.

\documentclass[12pt]{report}

\newif\ifShowSol
\ShowSolfalse
\usepackage{lipsum}
\usepackage[most]{tcolorbox} 
\newtcolorbox{examplebox}[1][]{%
    fonttitle=\bfseries,%
    lowerbox=\ifShowSol visible\else invisible\fi,%
    #1,%
}

\begin{document}
    Solution is invisible because of \verb|\ShowSolfalse| in preamble:
    \begin{examplebox}[title={Example}]
        \textbf{Question:} \lipsum[1][1-3]
        \tcblower
        \textbf{Answer:} \lipsum[2][1-3]
    \end{examplebox}
    
    \ShowSoltrue
    Solution is visible because of \verb|\ShowSolfalse| above:
    \begin{examplebox}[title={Example}]
        \textbf{Question:} \lipsum[3][1-3]
        \tcblower
        \textbf{Answer:} \lipsum[4][1-3]
    \end{examplebox}
\end{document}

Result

3

tcolorbox offers upperbox=invisible to hide the contents while keep the space. What follows is not good code, but it can help to start.

\documentclass[12pt]{report}
\usepackage[most]{tcolorbox} 

\usepackage {lipsum}

\tcbset{visibility/.style={}}

\newtcolorbox[auto counter, number within=chapter]{examplebox}[1][]{title=Example~\thetcbcounter, visibility, #1}

\newcommand{\hidesol}{\tcbset{visibility/.style={upperbox=invisible}}}

\begin{document}

\begin{examplebox}
\lipsum[1-1]
\end{examplebox}

{\hidesol
\begin{examplebox}
\lipsum[1-1]
\end{examplebox}
}

\begin{examplebox}
\lipsum[1-1]
\end{examplebox}

\end{document}

enter image description here

2

There are already some good answers, but I would add two things: If you move the visibility setting from \newtcolorbox to \tcbset you can overwrite the visibility manually.

Also, it appears to me, that you want the title of the box to be inside the box (not separated at the top). If so, you can separate the box into an upper and a lower box and either put the title in the upper box or use the attach title to upper option. To get the spacing right, you can set the option middle=0pt.

With this approach, you'd have to set the visibility of the lower box via lowerbox=.

\documentclass[12pt]{report}
\usepackage[most]{tcolorbox} 

\usepackage {lipsum}

\newif\ifShowSol
\ShowSolfalse

\newtcolorbox{newexamplebox}[1][]{lower separated=false, middle=0pt, fonttitle=\bfseries, coltitle=black,attach title to upper,#1}

\tcbset{lowerbox=\ifShowSol visible\else invisible\fi}


\begin{document}

\begin{newexamplebox}[title=Example 1.1, lowerbox=visible] % add , lowerbox=visible | invisible to overwrite the main visibilty setting
\tcblower
\lipsum[1-1]
\end{newexamplebox}

\begin{newexamplebox}  % add [, lowerbox=visible | invisible] to overwrite the main visibilty setting
\textbf{Example 1.1}
\tcblower
\lipsum[1-1]
\end{newexamplebox}

\end{document}

You could also get rid of the if block by simply changing the option \tcbset{lowerbox=visible/invisible} manually.

1
  • I didn't know how to use the conditional inside a parameter setting. Thank you.
    – Ignasi
    Commented Mar 16, 2023 at 11:52

You must log in to answer this question.

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