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}
4
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}

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.