LaTeX forum ⇒ Theses, Books, Title pages ⇒ Background color on all Section titles Topic is solved

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

Background color on all Section titles

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:

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.

Tags:

Stefan Kottwitz
Posts: 8523
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am
Hi.
I wrote this:

`\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.

Stefan Kottwitz
Posts: 8523
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
This patch should do it, inserted to `structure.tex`:

`\usepackage{etoolbox}\makeatletter\patchcmd{\@sect}{#8}{\colorbox{red}{#8}}{}{}\makeatletter`

Stefan

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am
Unfortunately, this code doesn't work.

Is it possible to use the \colorbox code inside these lines?
`\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.

Stefan Kottwitz
Posts: 8523
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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?
`\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.


`\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:

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

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am
OMG!
It works. I just had inserted an * on the title of my first section
`\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.

Stefan Kottwitz
Posts: 8523
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
You can add for supporting starred sections too:

`\patchcmd{\@ssect}{#5}{\colorbox{red}{#5}}{}{}`

Stefan

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am
You spoil me.
Thank you again so much!

Stefan Kottwitz
Posts: 8523
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:
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