2

I'd like to redefine \section to produce something like the following: (requiring package tcolorbox)

\begin{tcolorbox}
  1. \hspace*{\fill} Title of the Section \hspace*{\fill}
\end{tcolorbox}

the desired format of title I assumed I could use the titlesec package to do so. I tried doing the following:

\titleformat{\section}[display]% It's not clear to me why, but any other format except [display] breaks!
    {}{}{0em}%
    {\begin{tcolorbox}\thesection.\hspace*{\fill}}% 'before' code.
    [\hspace*{\fill}\end{tcolorbox}]% 'after' code.

The compiler accepts this, but not in the way that I expected it to. \section{Title of the Section} produces the following:an image of a faulty title, with incorrect spacing

I know that titlesec does provide a boxed title option, but it is not in the format that I would like to use. In particular I would prefer for the label to also be in the box, and I would like the control over the box that tcolorbox provides me.

I thought perhaps I could define the contents of this section title box in the titlesec regime, then perhaps define \edef\oldsection\section and redefine \section as I like, but it's unclear to me how I might pass along the arguments that \section takes.

TL;DR I'd like to put \section titles inside a \tcolorbox as in the first example, but it doesn't work.

1
  • 1
    Welcome to TeX.sx. Instead of just posting code fragments, edit your question to include a small compilable example that people can play with.
    – Alan Munn
    Jun 7 at 18:32

1 Answer 1

4

You may want to use load the tcolorbox packge with the explicit option, which allows you to use #1 as placeholder for the section title in the defintion of the \section command. Also, I think the block format is better suited here:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage[explicit]{titlesec}

\titleformat{\section}[block]
    {}{}{0em}
    {\begin{tcolorbox}
        \thesection. \hspace*{\fill} #1 \hspace*{\fill}
    \end{tcolorbox}}

\begin{document}

\section{Title of the Section}

\end{document}

enter image description here


To make this work even for starred \section commands:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage[explicit]{titlesec}

\titleformat{\section}[block]
    {}{}{0em}
    {\begin{tcolorbox}
        \thesection. \hspace*{\fill} #1 \hspace*{\fill}
    \end{tcolorbox}}

\titleformat{name=\section,numberless}[block]
    {}{}{0em}
    {\begin{tcolorbox}
        \hspace*{\fill} #1 \hspace*{\fill}
    \end{tcolorbox}}

\begin{document}

\section*{Title of the Section}

\section{Title of the Section}

\section*{Title of the Section}

\end{document}

enter image description here


As has been pointed out in the comments, this solution does not work nicely with longer titles. Since you are using the tcolorbox package already, you could enhance your box a bit to make it work for longer titles as well:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\usepackage[explicit]{titlesec}

\NewTColorBox{tcsectionbox}{ o }{%
  enhanced,
  halign=center,
  left=\IfValueTF{#1}{10mm}{4mm},
  right=4mm,
  top=3mm,
  bottom=3mm,
  boxsep=0mm,
  overlay={
    \IfValueT{#1}{
        \node[anchor=north west, inner xsep=4mm, inner ysep=3mm] 
        at (interior.north west) {#1};
    }
  }
}

\titleformat{\section}[block]
    {}{}{0em}
    {\begin{tcsectionbox}[\thesection.]
        #1
    \end{tcsectionbox}}

\titleformat{name=\section,numberless}[block]
    {}{}{0em}
    {\begin{tcsectionbox}
        #1
    \end{tcsectionbox}}

\begin{document}

\section{A ridiculously long section title that is only used to show that even long titles are working fine}

\section*{A ridiculously long section title that is only used to show that even long titles are working fine}

\section{A short title}

\end{document}

enter image description here

6
  • Many thanks. May I add, how can I adjust this to account for the label-free \section*{...} command?
    – Anton
    Jun 7 at 18:36
  • @Anton See my edit. For extensions of this, maybe have a look at the manual Jun 7 at 18:42
  • 3
    +1 but for long titles the title doesn't wrap in the centre. I would instead wrap the #1 as \begin{minipage}{.8\linewidth}\centering #1\end{minipage}.
    – Alan Munn
    Jun 7 at 19:20
  • 1
    That's a lot of code for a minipage :) (But fair enough to use proper tcolorbox methods.)
    – Alan Munn
    Jun 7 at 20:05
  • 2
    Using the nostruts option with titlesec removes some unnecessary spacing that titlesec adds here, as I have learned. See this question.
    – Anton
    Jun 8 at 14:24

You must log in to answer this question.

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