Theses, Books, Title pagesBackground color on all Section titles

Classicthesis, Bachelor and Master thesis, PhD, Doctoral degree
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Background color on all Section titles

Post by bstex »

Hi.
I'm using The Legrand Orange Book and I would like to ask how can I modify the "structure.tex" file, so all section titles have a background color (or highlighted).

I don't know if i'm right, but I found that the part of "structure.tex" that has to do with the section titles, is this:

Code: Select all

\makeatletter
\renewcommand{\@seccntformat}[1]{\llap{\textcolor{lightblue4}{\csname the#1\endcsname}\hspace{1em}}}                    
\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries}}
I have already managed to highlight the section titles on the "main.tex" file, by entering the command \colorbox before EVERY single section title, but I would like to do it in the "structure.tex" for easiest and simultaneous modification of all sections together.

Thank you very much.

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Background color on all Section titles

Post by Stefan Kottwitz »

Hi,
bstex wrote:by entering the command \colorbox before EVERY single section title, but I would like to do it in the "structure.tex" for easiest and simultaneous modification of all sections together.
how is the specific \colorbox code you used with sections, standard, or tcolorbox, and how? Perhaps we can use that to join with the section definition.

Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Background color on all Section titles

Post by bstex »

Hi.
I wrote this:

Code: Select all

\section*{\colorbox{red}{Example 1}}\index{Paragraphs of Text}
and a red highlight/background appeared on my text, which is exactly what I need.
But I have many sections on my book, so it would be very useful to define this background color for all sections together and not for every single one.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Background color on all Section titles

Post by Stefan Kottwitz »

This patch should do it, inserted to structure.tex:

Code: Select all

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@sect}{#8}{\colorbox{red}{#8}}{}{}
\makeatletter
Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Background color on all Section titles

Post by bstex »

Unfortunately, this code doesn't work.

Is it possible to use the \colorbox code inside these lines?

Code: Select all

\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries}}
I have already tried to insert the \colorbox code inside these lines, but nothing worked. Maybe I don't use it correctly.
This is the exact part of the code which refers to the Sections' names of my book. I know that, because when I change the last line, my Sections' names change.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Background color on all Section titles

Post by Stefan Kottwitz »

bstex wrote:Unfortunately, this code doesn't work.
It worked in my code when I tried it with that template.
bstex wrote:Is it possible to use the \colorbox code inside these lines?

Code: Select all

\renewcommand{\section}{\@startsection{section}{1}{\z@}
{-4ex \@plus -1ex \@minus -.4ex}
{1ex \@plus.2ex }
{\normalfont\large\sffamily\bfseries}}
I have already tried to insert the \colorbox code inside these lines, but nothing worked. Maybe I don't use it correctly.
This is the exact part of the code which refers to the Sections' names of my book. I know that, because when I change the last line, my Sections' names change.
It's not possible, since \@startsection implicitly uses more parameters that are not provided here. You see, \renewcommand{\section} is not followed by an argument number, like it commonly has to be. One would expect it like \renewcommand{\section}[1]{..} for 1 argument or \renewcommand{\section}[2]... for 2 arguments, remember the optional \section argument. So, the arguments a picked up by the last macro within \@startsection, and that's the internal macro \@sect that I patched above. It's like changing this:

Code: Select all

\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M #8\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec #8}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
to this:

Code: Select all

\def\@sect#1#2#3#4#5#6[#7]#8{%
  \ifnum #2>\c@secnumdepth
    \let\@svsec\@empty
  \else
    \refstepcounter{#1}%
    \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  \fi
  \@tempskipa #5\relax
  \ifdim \@tempskipa>\z@
    \begingroup
      #6{%
        \@hangfrom{\hskip #3\relax\@svsec}%
          \interlinepenalty \@M \colorbox{red}{#8}\@@par}%
    \endgroup
    \csname #1mark\endcsname{#7}%
    \addcontentsline{toc}{#1}{%
      \ifnum #2>\c@secnumdepth \else
        \protect\numberline{\csname the#1\endcsname}%
      \fi
      #7}%
  \else
    \def\@svsechd{%
      #6{\hskip #3\relax
      \@svsec \colorbox{red}{#8}}%
      \csname #1mark\endcsname{#7}%
      \addcontentsline{toc}{#1}{%
        \ifnum #2>\c@secnumdepth \else
          \protect\numberline{\csname the#1\endcsname}%
        \fi
        #7}}%
  \fi
  \@xsect{#5}}
My small patch above applied \colorbox to the argument #8 that is the section title. #7 is the optional title that goes to the table of contents.

And now you say it doesn't work. ;-) Well, provide details: error message, warning, output error or does simply nothing happen, perhaps with a minimal example.

Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Background color on all Section titles

Post by bstex »

OMG!
It works. I just had inserted an * on the title of my first section

Code: Select all

\section*{Section 1}
and that's why I couldn't see the change!
Well, now I have to find out how I am going to remove the numbers in front of the sections' titles, but I guess I'll find it soon. ;)
Thank you SO much.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Background color on all Section titles

Post by Stefan Kottwitz »

You can add for supporting starred sections too:

Code: Select all

\patchcmd{\@ssect}{#5}{\colorbox{red}{#5}}{}{}
Stefan
LaTeX.org admin
bstex
Posts: 69
Joined: Mon Oct 23, 2017 10:22 am

Background color on all Section titles

Post by bstex »

You spoil me. :D
Thank you again so much!
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Background color on all Section titles

Post by Stefan Kottwitz »

Sorry that it looks so like hacking, and it's patching internals. But it doesn't break things though.

The better way would be
  • using scrbook instead of book as document class (more customizable)
  • or using the titlesec package for customizing section headings.
That would mean using an interface instead of patching internals. But I guess switching the document class of a template has consequences to that template's design, and introducing titlesec would conflict with the template's own section hacks. So just to mention this. The point is anyway, to get it quickly working for you, not to make a better template for the future. Luckily, open source LaTeX allows such modifications once we know that we are looking for.

Stefan
LaTeX.org admin
Post Reply