Page Layout\section{} within a tcolorbox

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
almcgowan
Posts: 4
Joined: Mon Feb 03, 2020 9:19 pm

\section{} within a tcolorbox

Post by almcgowan »

Hi everyone,

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. to (1) 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:

Code: Select all

\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?

Code: Select all

\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}
Last edited by cgnieder on Sun Feb 09, 2020 1:46 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

\section{} within a tcolorbox

Post by Ijon Tichy »

One suggestion would be to use either a package like titlesec or a more configurable class like scrartcl to realize different formatting of sections. There a lots of questions and answers for this, so I won't show it now.

Another suggestion would be to patch the original \section and replace the font-argument of the internal used \@startsection by using the \sectionbox:
\documentclass{article}
\usepackage[utf8]{inputenc}% Not needed with LaTeX since 2018-04-01.
\usepackage{tcolorbox}
\usepackage{mwe}% combination of lipsum and blindtext and additions

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

\usepackage{xpatch}
\xpatchcmd{\section}{\normalfont\Large\bfseries}{\sectionbox}{}{\PatchFailed}

\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,
                 ]
    \MakeUppercase{#1}
\end{tcolorbox} 
} %

\begin{document}
\maketitle

\tableofcontents
\blinddocument

\end{document}
Image
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
almcgowan
Posts: 4
Joined: Mon Feb 03, 2020 9:19 pm

\section{} within a tcolorbox

Post by almcgowan »

Thank you, this is perfect!!

However, now a new problem arose: it crashes with titlesec, which I had used to define the subsections (no tcolorboxes there). How would I go about changing the color of \subsection without using titlesec?
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

\section{} within a tcolorbox

Post by Ijon Tichy »

:?: If you are already using titlesec, why don't you configure the section headings using this package? :?:

However, you can use the patch method also for subsections:

Code: Select all

\xpatchcmd{\subsection}{\normalfont\large\bfseries}{\normalfont\large\bfseries\color{green}}{}{\PatchFailed}
If you have to patch other commands too, please have a look into article.cls to see the original definition and which code sequence has to be changed. And please read the manual of xpatch.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
almcgowan
Posts: 4
Joined: Mon Feb 03, 2020 9:19 pm

\section{} within a tcolorbox

Post by almcgowan »

That did the job, thank you!

As for your question, I tried formatting the boxes with titlesec in the beginning (along with a billion other packages), but there were always clashes between packages or limitations in the design that made it impossible. With some packages (I think incl. titlesec) I could get a frame, but couldn't figure out how to fill the frame, and with others I could make it look right design-wise, but the ToC got all kinds of messed up. Please do note that I'm very much a beginner here, I've learnt through trial and error and still have much to learn with regards to writing the code from scratch.

Just out of interest: do you happen to have a solution that creates the same look and function using titlesec too?
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

\section{} within a tcolorbox

Post by Ijon Tichy »

I'm not using titlesec, becaue I follow my own recommendation an always use a more configurable class like scrartcl, that does not need such a package. So I always have to read a lot in the manual, before showing a solution using titlesec. So if you are interesting in using titlesec, please have a look into the manual on your own. An if you fail, please show us a Infominimal working example, that shows, what you already have tried.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply