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.

Anton
  • 23
  • 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 07 '23 at 18:32

1 Answers1

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