1

I need to change the sections, subsections, etc., styles. And I did. The problem is I need the table of contents to keep the default style.

The text colour in my new style is white, so the TOC appears to have no section titles. And the tocloft \renewcommand\cftsecfont{\color{black}} is not overriding that change for the TOC.

\documentclass{article}

\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{tocloft}

\renewcommand\cftsecfont{\color{black}}

\newcommand*{\newSection}[1]{
    \begin{tcolorbox}[
        center title,
        left=0pt,
        right=0pt,
        top=0pt,
        bottom=0pt,
        colback=blue!75,
        colframe=white,
        width=\dimexpr\textwidth\relax,
        enlarge left by=0mm,
        boxsep=5pt,
        arc=0pt,outer arc=0pt]
        \section{\textcolor{white}{#1}}
    \end{tcolorbox}
}

\let\oldthesection\thesection
\renewcommand{\thesection}{\textcolor{white}{\oldthesection}}

\begin{document}
  \tableofcontents
  \newSection{Section}
  \newSection{Another Section}
  \newSection{And One More}
\end{document}

enter image description here

2 Answers 2

1

The package titlesec makes it much easier to change the formatting of the section titles without affecting the TOC or other locations:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tcolorbox}
\usepackage{tocloft}
\usepackage{titlesec}

\newtcolorbox{sectionbox}{
    center title,
    left=0pt,
    right=0pt,
    top=0pt,
    bottom=0pt,
    colback=blue!75,
    colframe=white,
    width=\dimexpr\textwidth\relax,
    enlarge left by=0mm,
    boxsep=5pt,
    arc=0pt,
    outer arc=0pt
}

\titleformat{\section}[block]
    {\begin{sectionbox}\normalfont\Large\bfseries\color{white}}
    {\thesection}
    {1em}
    {}
    [\end{sectionbox}]

\begin{document}
\tableofcontents

\section{Section}
\section{Another Section}
\section{And One More}
\end{document}

The resulting document

1

Without using tocloft or titlesec, just providing the correct option handling and redefining \@seccntformat which is to be preferred concerning the change of the numbering format of section titles.

\documentclass{article}

\usepackage{xcolor}
\usepackage[most]{tcolorbox}

\makeatletter
\let\@old@seccntformat\@seccntformat
\NewDocumentCommand{\newSection}{O{#2}mO{}}{
  \begin{tcolorbox}[
   enhanced,
    frame hidden,
    center title,
    left=0pt,
    right=0pt,
    top=0pt,
    bottom=0pt,
    colback=blue!75,
    colframe=white,
    boxsep=5pt,
    sharp corners,
    #3
    ]
    \begingroup
    \renewcommand{\@seccntformat}[1]{%
      \ifnum0=\pdfstrcmp{##1}{section}%
      \textcolor{white}{\@old@seccntformat{##1}}%
      \else
      \@old@seccntformat{##1}%
      \fi
    }
    \section[\color{black}#1]{\textcolor{white}{#2}}%
    \endgroup
  \end{tcolorbox}
}
\makeatother


\begin{document}
  \tableofcontents
  \newSection{Section}
  \newSection{Another Section}
  \newSection{And One More}
  \newSection{Don't screw up the layout}
\end{document}

enter image description here

You must log in to answer this question.

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