5

I want to make a tcolorbox that looks roughly like this

-----------------------------------------------------------------------------------
            |          Subtitle (If the subtitle is too long, like it is here, 
TITLE       |          then it should wrap like this, and the title on the left
            |          hand side should be centered relative to the subtitle)
-----------------------------------------------------------------------------------
Here is the content of the tcolorbox.
-----------------------------------------------------------------------------------

The lines should be solid and connected, and the vertical alignment should be as described above. It should be possible to specify the distance from the left margin to the horizontal separator, and from the horizontal separator to the block of subtitle text (this will be used for all tcolorboxes, which may have titles of different lengths. It should be possible to independently specify the fonts used for the title and subtitle.

Here is a minimal example:

\documentclass{report}
\usepackage{tcolorbox}

\newlength{\normalparindent}
\AtBeginDocument{\setlength{\normalparindent}{\parindent}}
\tcbset{before upper={\setlength{\parindent}{\normalparindent}}}

\tcbuselibrary{skins,raster,breakable}
\tcbset{
    enhanced,
    frame hidden,interior hidden,
    sharp corners,
    boxrule=0pt,
    left=-0.1cm,right=-0.1cm,top=0.20cm,bottom=0.35cm,
    toptitle=0.35cm+1pt,
    bottomtitle=0.00cm+1pt,
    colframe=white,colback=white,coltitle=black,
    bottomrule=1pt,
    borderline north={1pt}{0pt}{black},
    borderline south={1pt}{0pt}{black},
    fonttitle=\bfseries,fontupper=\normalsize,
    before skip=0.375cm+2pt,after skip=0.475cm+2pt
}

\begin{document}

\begin{tcolorbox}[title=TITLE]
\noindent Content.
\end{tcolorbox}

\end{document}
8

Something like that?

enter image description here

In my example code, I made a new environment mybox which takes tcolorbox options as optional argument, then the title and subtitle text as normal arguments.

The new length settings are done by

  my rule=1pt,
  left to separator=3cm,
  separator to subtitle=1cm,

where my rule stores the rule width and the two other options store the distances to the vertical line as you requested.

\documentclass{report}
\usepackage{tcolorbox,lipsum}
\tcbuselibrary{skins,raster,breakable}

\newlength{\normalparindent}
\AtBeginDocument{\setlength{\normalparindent}{\parindent}}
\tcbset{before upper={\setlength{\parindent}{\normalparindent}}}

\tcbset{
  my rule/.store in=\myrule,
  left to separator/.store in=\mylefttoseparator,
  separator to subtitle/.store in=\myseparatortosubtitle,
}

\newtcolorbox{mybox}[3][]{%
  my rule=1pt,
  left to separator=3cm,
  separator to subtitle=1cm,
  enhanced,
  frame hidden,interior hidden,
  sharp corners,
  boxrule=\myrule,boxsep=0pt,
  left=0pt,right=0pt,top=0.35cm,bottom=0.35cm,
  toptitle=0.35cm,bottomtitle=0.35cm,
  colframe=black,colback=white,coltitle=black,
  borderline north={\myrule}{0pt}{black},
  borderline south={\myrule}{0pt}{black},
  fonttitle=\bfseries,fontupper=\normalsize,
  before skip=0.375cm+2pt,after skip=0.475cm+2pt,
  lefttitle=\mylefttoseparator+\myrule+\myseparatortosubtitle,
  title={\raggedright\mbox{}#3},
  underlay unbroken and first={
    \node[right,inner sep=0pt,outer sep=0pt,
      font=\large\bfseries,% <-- title font
      text width=\mylefttoseparator-5mm] at (title.west) {#2};
    \draw[tcbcol@frame,line width=\myrule]
      ([xshift=-\myrule,yshift=-\myrule/2]title.south west)--([xshift=\myrule,yshift=-\myrule/2]title.south east)
      ([xshift=\mylefttoseparator+\myrule/2]title.south west)--([xshift=\mylefttoseparator+\myrule/2]title.north west);
  },
  #1,
}


\begin{document}

\begin{mybox}{TITLE}{SUBTITLE}
\noindent\lipsum[2]
\end{mybox}

\begin{mybox}{TITLE}{Subtitle (If the subtitle is too long, like it is here,
     then it should wrap like this, and the title on the left
     hand side should be centered relative to the subtitle)}
\noindent\lipsum[3]
\end{mybox}

\end{document}
  • Definitely a feature to be built in for the next release ;-) – user31729 Jul 3 '15 at 6:37
  • Thanks, it works perfectly. One more thing: is it possible to give the subtitle a more "ragged" justification, to reduce hyphenation? I can get by with manually adding a line break, but this might be nicer. – Randy Randerson Jul 3 '15 at 8:03
  • 2
    @RandyRanderson Yes, just replace title={#3}, by title={\raggedright #3},. – Thomas F. Sturm Jul 3 '15 at 11:11
  • 1
    @RandyRanderson I also edited my answer to replace underlay by underlay unbroken and first. This would be needed for a breakable box. – Thomas F. Sturm Jul 3 '15 at 11:12
  • 1
    @RandyRanderson (1) I don't understand this since the middle vertical line merges with the horizontal lines... (?) ... (2) Yes :-) (3) I edited the code to insert a \mbox{} before the subtitle is used. Thereby, an empty subtitle should cause no problems anymore. – Thomas F. Sturm Jul 6 '15 at 5:51

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.