3

I think my problem might be really basic, but I went through the documentation and could not figure out how to force text to wrap inside a tcolorbox title, when the title is too long.

\documentclass[a4paper,11pt]{book}

\usepackage{lipsum}
\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}

\tcbset{
    thmbox/.style={
        enhanced,
        breakable,
        sharp corners=all,
        fonttitle=\bfseries\normalsize,
        fontupper=\normalsize\itshape,
        top=0mm,
        bottom=0mm,
        right=0mm,
        colback=white,
        colframe=white,
        colbacktitle=white,
        coltitle=black,
        attach boxed title to top left,
        boxed title style={empty, size=minimal, bottom=1.5mm},
        overlay unbroken ={
            \draw (title.south west)--(title.south east);
            \draw ([xshift=3.5mm]frame.north west)|-%
                  (frame.south east)--(frame.north east);},
        overlay first={
            \draw (title.south west)--(title.south east); 
            \draw ([xshift=3.5mm]frame.north west)--  ([xshift=3.5mm]frame.south west);
            \draw (frame.north east)--(frame.south east);},
        overlay middle={
            \draw ([xshift=3.5mm]frame.north west)--([xshift=3.5mm]frame.south west);
            \draw (frame.north east)--(frame.south east);},
        overlay last={
            \draw ([xshift=3.5mm]frame.north west)|-%
                  (frame.south east)--(frame.north east);},
    },
}

\newtcbtheorem[number within=section]{questionbox}{Question}{thmbox}{qst}

\begin{document}

\begin{questionbox}{\lipsum[66]}{qst:1}
\lipsum[1]
\end{questionbox}
\end{document}
  • At the moment your example does not even compile – user31729 Sep 12 '16 at 5:26
  • Try \newrobustcmd{\mytitle}{\parbox[t]{0.7\textwidth}{\lipsum[1]}} to make a robust title box and use \mytitle instead of \lipsum[66] as title. The title in a theorem is just a \hbox which does neither wrap etc, unless using an inner parbox. It will not break like the real tcolorbox content. Just reduce your title -- that's the best option. – user31729 Sep 12 '16 at 5:40
  • That's strange. I just copied the text into a new document and it compiled. Where in my code do I add \newrobustcmd{\mytitle}{\parbox[t]{0.7\textwidth}{\lipsum[1]‌​}} Wouldn't that print the same title every time? i.e. the contents of \lipsum[1]? I am still trying to figure out how things work. What I want to do, though, is to enter questions of variable size in the title (possibly up to 3 or 4 lines) and the answer in the body of the theorem box. I guess I'm trying to merge the theorem and the proof environments in the two boxes of tcolorbox, if that makes any sense – Mahoma Sep 12 '16 at 6:14
  • @Mahoma You can use one box for both questions and answers. Questions are included in upper part and answers in lower. tcolorbox documentation shows several examples like these. Two special examples are shown in my answers to how-to-defer-content-to-a-later-part-of-the-document and hide-content-to-show-later where solutions are shown later on in your document. – Ignasi Sep 12 '16 at 7:41
  • @Mahoma: Of course you must change the \mytitle box each time. But the two - box approach is the better one. – user31729 Sep 12 '16 at 13:12
2

As Christian explained, tcolorbox title is not wrapped. It's supposed to be some word and not a complete exercise. But if you want to mix questions and answers into the same color box, based on you format a possible solution could be to use a short title (or an empty one) and place the question in upper tcolorbox part while solution in placed in the lower part. Something like

enter image description here

\documentclass[a4paper,11pt]{book}

\usepackage{lipsum}
\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{lmodern}

\tcbset{
    thmbox/.style={
        enhanced,
        breakable,
        sharp corners=all,
        fonttitle=\bfseries\normalsize,
        fontupper=\normalsize,
        fontlower=\normalsize\itshape,
        before lower={\itshape Solution:\ },
        top=0mm,
        bottom=0mm,
        right=0mm,
        colback=white,
        colframe=white,
        colbacktitle=white,
        coltitle=black,
        attach boxed title to top left,
        boxed title style={empty, size=minimal, bottom=1.5mm},
        overlay unbroken ={
            \draw (title.south west)--(title.south east);
            \draw ([xshift=3.5mm]frame.north west)|-%
                  (frame.south east)--(frame.north east);},
        overlay first={
            \draw (title.south west)--(title.south east); 
            \draw ([xshift=3.5mm]frame.north west)--  ([xshift=3.5mm]frame.south west);
            \draw (frame.north east)--(frame.south east);},
        overlay middle={
            \draw ([xshift=3.5mm]frame.north west)--([xshift=3.5mm]frame.south west);
            \draw (frame.north east)--(frame.south east);},
        overlay last={
            \draw ([xshift=3.5mm]frame.north west)|-%
                  (frame.south east)--(frame.north east);},
    },
}

\newtcbtheorem[number within=section]{questionbox}{Question}{thmbox}{qst}

\begin{document}

\begin{questionbox}{A nice question}{qst:1}
\lipsum[1]
\tcblower
\lipsum[2]
\end{questionbox}
\end{document}
  • I really like the solutions in your links! The only problem now is that when I print the list off tcbtheorems I no longer get the questions to be listed. I've been trying to figure out how to collect the contents of the tcolorbox rather than the title, since the question is no longer a title. I tried using this tex.stackexchange.com/questions/184764/… but I am not really understanding the options to adapt them to the colorbox – Mahoma Sep 13 '16 at 13:19

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.