7

I'm working with tcolorbox to produce boxes and highlight answers to a document with questions.

The following MWE shows the box I created, separating the title into another box.

\documentclass[12pt]{article}
\usepackage[papersize={5.5in,8.5in},margin=1.5cm]{geometry}

\usepackage[many]{tcolorbox}
\usetikzlibrary{calc}
\tcbuselibrary{skins}

\newtcolorbox{resp}[1][]{%
    enhanced jigsaw,%
    colback=gray!20!white,%
    colframe=gray!80!black,%
    size=small,%
    boxrule=1pt,%
    title=\textbf{\textit{Answer}},%
    halign title=flush center,%
    coltitle=black,%
    breakable,%
    drop shadow=black!50!white,%
    attach boxed title to top left={xshift=1cm,yshift=-\tcboxedtitleheight/2,yshifttext=-\tcboxedtitleheight/2},%
    minipage boxed title=3cm,%
    boxed title style={%
        colback=white,%
        size=fbox,%
        boxrule=1pt,%
        boxsep=2pt,%
        underlay={%
            \coordinate (dotA) at ($(interior.west) + (-0.5pt,0)$);
            \coordinate (dotB) at ($(interior.east) + (0.5pt,0)$);
            \begin{scope}[gray!80!black]
                \fill (dotA) circle (2pt);
                \fill (dotB) circle (2pt);
            \end{scope}
        }%
    },%
  #1%
}

\begin{document}
  \begin{resp}
    An exciting text {\ttfamily:)}
  \end{resp}
  \vspace{1cm}
  \begin{resp}[boxed title style={drop shadow=black!50!white}]
    Another box with shadow {\ttfamily:/}
  \end{resp}
\end{document}

Result of compiling the above code:

Result of compiling the above MWE code

Thanks to the documentation, I could add a shadow to the text box (the one without the title), and as the second box shows, I could do the same with the second box. Unfortunately, the title box shadow overlaps with the text box, and I wanted it to be at the same level to the text box shadow (i.e. behind the text box).

How can I apply a shadow to both boxes as a group, so the shadows of both the title and the text box could be one, and behind the text box?

5
  • 2
    You want to have a 'halo', not a 'shadow' ;-)
    – user31729
    Mar 5, 2017 at 7:15
  • @ChristianHupfer Mmmmm, that sounds good too, but what I mean with "shadow" is to also include the little one al the top right corner of the title box (see the second box of the MWE output picture).
    – user101590
    Mar 5, 2017 at 7:18
  • Do you want to have the title box shadow outside of the title box, i.e. directed to 'north east'?
    – user31729
    Mar 5, 2017 at 7:21
  • @ChristianHupfer I hope I understood your question. If you mean to have the title box shadow as a separate entity, so I could place it behind the text shadow, yes. If not, I'm sorry: could you specify a bit, please? :(
    – user101590
    Mar 5, 2017 at 7:24
  • 1
    The titlebox and the mainbox are drawn in two individual tikzpicture. Even if you apply pgfonlayer it will not help. But there is a workaround discussed in tex.stackexchange.com/questions/24140/…
    – Symbol 1
    Mar 5, 2017 at 17:19

1 Answer 1

5

Something like this? You can adjust the settings to improve the match. This is just an illustration of the method.

box with title and shadow

\documentclass[12pt]{article}
\usepackage[papersize={5.5in,8.5in},margin=1.5cm]{geometry}
\usepackage[many]{tcolorbox}
\usetikzlibrary{calc,shadows.blur}
\tcbuselibrary{skins}
\newtcolorbox{resp}[1][]{%
  enhanced jigsaw,
  colback=gray!20!white,%
  colframe=gray!80!black,
  size=small,
  boxrule=1pt,
  title=\textbf{\textit{Answer}},
  halign title=flush center,
  coltitle=black,
  breakable,
  drop shadow=black!50!white,
  attach boxed title to top left={xshift=1cm,yshift=-\tcboxedtitleheight/2,yshifttext=-\tcboxedtitleheight/2},
  minipage boxed title=3cm,
  boxed title style={%
    colback=white,
    size=fbox,
    boxrule=1pt,
    boxsep=2pt,
    underlay={%
      \coordinate (dotA) at ($(interior.west) + (-0.5pt,0)$);
      \coordinate (dotB) at ($(interior.east) + (0.5pt,0)$);
      \begin{scope}
        \clip (interior.north west) rectangle ([xshift=3ex]interior.east);
        \filldraw [white, blur shadow={shadow opacity=60, shadow yshift=-.75ex}, rounded corners=2pt] (interior.north west) rectangle (interior.south east);
      \end{scope}
      \begin{scope}[gray!80!black]
        \fill (dotA) circle (2pt);
        \fill (dotB) circle (2pt);
      \end{scope}
    },
  },
  #1,
}

\begin{document}
\begin{resp}
  An box with a shadow {\ttfamily:)}
\end{resp}
\end{document}
3
  • Interesting =) That's a good trick. In fact, it comes in handy with blur shadows. But I have to ask: is it possible to do the same without the blur effect?
    – user101590
    Mar 6, 2017 at 2:42
  • I got it. I only changed the clipping and rectangle positions as you said, and replaced the blur shadow with a drop one. Unfortunately, it seems tcolorbox uses a different rounded corners value for shadows than the given ones for the size shorcuts. By the way, I didn't know tikz could handle clipping like CorelDRAW or Photoshop :D
    – user101590
    Mar 6, 2017 at 3:26
  • 2
    @JBFWP286 Non-blur is actually standard. I just did it with blur to show that was possible too. Just load plain shadows for drop shadow. The blurred shadows sometimes look better. They sometimes look worse. And they occasionally look crazy ;). toclorbox uses rounded corners=\kvtcb@arc I think.
    – cfr
    Mar 6, 2017 at 22:27

You must log in to answer this question.