1

I have customized the appearance of the \section command using the titlesec package, and I have also customized the appearance of the table of contents using the titletoc package.

However, it appears that the new definition for \section treats both the table of contents and references list as sections, resulting in incorrect section labels being assigned to them. Specifically, the table of contents displays a redundant zero label, while the references list takes on the same number as the last section in the document rather than incrementing the counter by one.

To illustrate this issue, I have provided a minimal working example. I would appreciate any help or suggestions to resolve this issue. Thank you for your expertise.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{titlesec,titletoc}
\usepackage{tikz}

%======= ToC Contents Style  ========%
%=======                     ========%
\renewcommand{\contentsname}{%
    \tikz[baseline={([yshift=-1ex]current bounding box.center)}]
    \node[
    rectangle,
    rounded corners=6pt,
    fill=black!85,
    inner sep=5pt,
    minimum width=30pt, minimum height=25pt]
    {\textcolor{white}{Contents} };
}

%======= Section Style  ========%
%=======                ========%
\titleformat{\section}[block]{\Large\bfseries}{}{0pt}{%
        \tikz[baseline={([yshift=-1ex]current bounding box.center)}]
        \node[
        rectangle,
        rounded corners=7pt,
        fill=cyan!30!blue,
        inner sep=6pt,
        minimum width=25pt,
        minimum height=8pt]
        {
        \textcolor{white}{\textbf{\thesection}}
        };
    }

\numberwithin{equation}{section}
%\numberwithin{figure}{section}
%\numberwithin{table}{section}


\begin{document}
        
    \tableofcontents

    \section{Sec 1}
    \begin{equation}
        1+1=2
    \end{equation}

    \section{Sec 2}
    ...
    
    \begin{thebibliography}{1}
        \bibitem{Ref} Reference
    \end{thebibliography}

\end{document}

With the following output :

enter image description here

1 Answer 1

1

Usual problem: you need a separate setting for unnumbered sections.

Meanwhile, I suggest some improvements to the code: separating the fancy formatting in specialized macros helps in keeping the \titleformat code readable.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{titlesec,titletoc}
\usepackage{tikz}

%======= ToC Contents Style  ========%
%=======                     ========%
\renewcommand{\contentsname}{%
    \tikz[baseline={([yshift=-1ex]current bounding box.center)}]
    \node[
    rectangle,
    rounded corners=6pt,
    fill=black!85,
    inner sep=5pt,
    minimum width=30pt, minimum height=25pt]
    {\textcolor{white}{Contents} };
}

%======= Section Style  ========%
%=======                ========%
\titleformat{\section}[block]{\Large\bfseries}{}{0pt}{%
        \tikz[baseline={([yshift=-1ex]current bounding box.center)}]
        \node[
        rectangle,
        rounded corners=7pt,
        fill=cyan!30!blue,
        inner sep=6pt,
        minimum width=25pt,
        minimum height=8pt]
        {
        \textcolor{white}{\textbf{\thesection}}
        };
    }

\numberwithin{equation}{section}
%\numberwithin{figure}{section}
%\numberwithin{table}{section}


\begin{document}
        
    \tableofcontents

    \section{Sec 1}
    \begin{equation}
        1+1=2
    \end{equation}

    \section{Sec 2}
    ...
    
    \begin{thebibliography}{1}
        \bibitem{Ref} Reference
    \end{thebibliography}

\end{document}

enter image description here

I believe that you want all unnumbered section titles to be typeset in the same way as “Contents”. You achieve this by modifying the code above into the following. The trick is that the last mandatory argument to \titleformat can end with a command expecting an argument: it will be passed the section title.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{titlesec,titletoc}
\usepackage{tikz}

%======= ToC Contents Style  ========%
%=======                     ========%

\newcommand{\whiteonblack}[1]{%
  \begin{tikzpicture}[baseline={([yshift=-1ex]current bounding box.center)}]
    \node[
      rectangle,
      rounded corners=6pt,
      fill=black!85,
      inner sep=5pt,
      minimum width=30pt,
      minimum height=25pt
    ]{\textcolor{white}{#1}};
  \end{tikzpicture}%
}
\newcommand{\whiteonblue}[1]{%
  \begin{tikzpicture}[baseline={([yshift=-1ex]current bounding box.center)}]
    \node[
      rectangle,
      rounded corners=7pt,
      fill=cyan!30!blue,
      inner sep=6pt,
      minimum width=25pt,
      minimum height=8pt,
   ]{\textcolor{white}{#1}};
  \end{tikzpicture}%
}

%======= Section Style  ========%
%=======                ========%
\titleformat{\section}[block]
  {\Large\bfseries}
  {}
  {0pt}
  {\whiteonblue{\textbf{\thesection}} }
\titleformat{name=\section,numberless}[block]
  {\Large\bfseries}
  {}
  {0pt}
  {\whiteonblack}

\numberwithin{equation}{section}
%\numberwithin{figure}{section}
%\numberwithin{table}{section}


\begin{document}
        
    \tableofcontents

    \section{Sec 1}
    \begin{equation}
        1+1=2
    \end{equation}

    \section{Sec 2}
    ...
    
    \begin{thebibliography}{1}
        \bibitem{Ref} Reference
    \end{thebibliography}

\end{document}

enter image description here

3
  • Thank you very much for your helpful insight. If it's not too much trouble, could you please elaborate in more detail how to achieve the custom look for both the "Contents" and "References" titles, as you kindly demonstrated earlier, after removing the line you told me to remove? Apr 29, 2023 at 8:32
  • 1
    @aglobalphenomenon I added the full example code.
    – egreg
    Apr 29, 2023 at 8:44
  • Thank you very much for the further clarification, I now understand what you meant. I truly appreciate your help. Apr 29, 2023 at 8:51

You must log in to answer this question.

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