3

I'm working on a LaTeX template for an article appendix that will be used by a whole team. My current challenge is creating a color-filled box around \section{} headings, while maintaining the qualities of the \section{} command.

I've addressed this task by creating a tcolorbox, the look of which I'm happy with, but the functionality of the \section{} command has been lost. I need some help remedying this problem.

I've named the tcolorboxes "sectionboxes" as seen in the working example below, and would like to simply be able to search my colleagues' files and replace all \section{} with \sectionbox{} to apply this layout.

I need the \sectionboxes to have the same functions as the \section commands, i.e.

  1. to display the section count in the in-text title itself (e.g. "1. Section title" and not just "Section title"), and

  2. for the \sectionbox{} content to be displayed in the table of contents with its corresponding section count.

To create the boxes, I use the package tcolorbox and the command below.

In-text, where I would normally write \section, I write:

\stepcounter{section}
\addcontentsline{toc}{section}{SECTION_NAME}
\sectionbox{\MakeUppercase{SECTION_NAME}}

This at least adds the \sectionbox content to the ToC, but without the count number. It is also too many lines to be efficient.

I feel like I've tried everything, including numerous other ways of creating the boxes, but I just can't make it work. Does anyone have a solution?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\usepackage{lipsum}

\title{title}
\author{author}
\date{\today}

\newcommand{\sectionbox}[1] {
\begin{tcolorbox}
             [
              colback=purple!100,% background
              colframe=purple,% frame colour
              coltext=white, % text color
              width=\linewidth,%
              height=0.7cm, 
              halign=center,
              valign=center,
              fontupper=\large\bfseries,
              arc=0mm, auto outer arc,
             ]
    {#1}
\end{tcolorbox} 
} %

\begin{document}
\maketitle

\sectionbox{Contents}
\vspace{-1cm}
\renewcommand{\contentsname}{}

\tableofcontents
\newpage

\stepcounter{section}
\addcontentsline{toc}{section}{SECTION-NAME}
\sectionbox{\MakeUppercase{SECTION-NAME}}

\lipsum[1-2]

\end{document}
moewe
  • 175,683
  • Welcome to TeX.SX! Thank you for taking the time to post a nice illustrative example document along with your question. – moewe Feb 03 '20 at 20:21
  • 2
    You may be interested in some alternative/related approaches to boxing section headings discussed in https://tex.stackexchange.com/q/179019/35864, https://tex.stackexchange.com/q/11998/35864, https://tex.stackexchange.com/q/34288/35864, https://tex.stackexchange.com/q/318637/35864, https://tex.stackexchange.com/q/431142/35864, https://tex.stackexchange.com/q/254318/35864 (especially the last one seems interesting) – moewe Feb 03 '20 at 20:25
  • 1
    In addition to those mentioned by @moewe, here's a solution I did for the memoir class: https://tex.stackexchange.com/a/522778/87678 – David Purton Feb 04 '20 at 04:26
  • Thanks everyone, these are great! Also got a terrific answer in another forum, using an xpatch command: https://latex.org/forum/viewtopic.php?f=47&t=33217 – almcgowan Feb 05 '20 at 11:08

1 Answers1

1

All you need is \refstepcounter and \numberline. Hint, use a normal \section command and compare the entries in the aux file.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tcolorbox}
\usepackage{lipsum}

\title{title}
\author{author}
\date{\today}

\newcommand{\sectionbox}[1] {
\begin{tcolorbox}
             [
              colback=purple!100,% background
              colframe=purple,% frame colour
              coltext=white, % text color
              width=\linewidth,%
              height=0.7cm, 
              halign=center,
              valign=center,
              fontupper=\large\bfseries,
              arc=0mm, auto outer arc,
             ]
    {#1}
\end{tcolorbox} 
} %

\begin{document}
\maketitle

\sectionbox{Contents}
\vspace{-1cm}
\renewcommand{\contentsname}{}

\tableofcontents
\newpage

\refstepcounter{section}
\addcontentsline{toc}{section}{\string\numberline{\thesection}SECTION-NAME}
\sectionbox{\MakeUppercase{SECTION-NAME}}

\lipsum[1-2]

\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120