LaTeX forum ⇒ Theses, Books, Title pagesBackground 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

Postby bstex » Mon Oct 23, 2017 10:41 am

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:
  1. \makeatletter
  2. \renewcommand{\@seccntformat}[1]{\llap{\textcolor{lightblue4}{\csname the#1\endcsname}\hspace{1em}}}
  3. \renewcommand{\section}{\@startsection{section}{1}{\z@}
  4. {-4ex \@plus -1ex \@minus -.4ex}
  5. {1ex \@plus.2ex }
  6. {\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.

Tags:

User avatar
Stefan Kottwitz
Site Admin
Posts: 8537
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Oct 26, 2017 4:07 am

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
Site admin

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am

Postby bstex » Thu Oct 26, 2017 2:42 pm

Hi.
I wrote this:

  1. \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: 8537
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Oct 26, 2017 5:54 pm

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

  1. \usepackage{etoolbox}
  2. \makeatletter
  3. \patchcmd{\@sect}{#8}{\colorbox{red}{#8}}{}{}
  4. \makeatletter


Stefan
Site admin

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am

Postby bstex » Thu Oct 26, 2017 9:19 pm

Unfortunately, this code doesn't work.

Is it possible to use the \colorbox code inside these lines?
  1. \renewcommand{\section}{\@startsection{section}{1}{\z@}
  2. {-4ex \@plus -1ex \@minus -.4ex}
  3. {1ex \@plus.2ex }
  4. {\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: 8537
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Oct 26, 2017 9:55 pm

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?
  1. \renewcommand{\section}{\@startsection{section}{1}{\z@}
  2. {-4ex \@plus -1ex \@minus -.4ex}
  3. {1ex \@plus.2ex }
  4. {\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:

  1. \def\@sect#1#2#3#4#5#6[#7]#8{%
  2. \ifnum #2>\c@secnumdepth
  3. \let\@svsec\@empty
  4. \else
  5. \refstepcounter{#1}%
  6. \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  7. \fi
  8. \@tempskipa #5\relax
  9. \ifdim \@tempskipa>\z@
  10. \begingroup
  11. #6{%
  12. \@hangfrom{\hskip #3\relax\@svsec}%
  13. \interlinepenalty \@M #8\@@par}%
  14. \endgroup
  15. \csname #1mark\endcsname{#7}%
  16. \addcontentsline{toc}{#1}{%
  17. \ifnum #2>\c@secnumdepth \else
  18. \protect\numberline{\csname the#1\endcsname}%
  19. \fi
  20. #7}%
  21. \else
  22. \def\@svsechd{%
  23. #6{\hskip #3\relax
  24. \@svsec #8}%
  25. \csname #1mark\endcsname{#7}%
  26. \addcontentsline{toc}{#1}{%
  27. \ifnum #2>\c@secnumdepth \else
  28. \protect\numberline{\csname the#1\endcsname}%
  29. \fi
  30. #7}}%
  31. \fi
  32. \@xsect{#5}}


to this:

  1. \def\@sect#1#2#3#4#5#6[#7]#8{%
  2. \ifnum #2>\c@secnumdepth
  3. \let\@svsec\@empty
  4. \else
  5. \refstepcounter{#1}%
  6. \protected@edef\@svsec{\@seccntformat{#1}\relax}%
  7. \fi
  8. \@tempskipa #5\relax
  9. \ifdim \@tempskipa>\z@
  10. \begingroup
  11. #6{%
  12. \@hangfrom{\hskip #3\relax\@svsec}%
  13. \interlinepenalty \@M \colorbox{red}{#8}\@@par}%
  14. \endgroup
  15. \csname #1mark\endcsname{#7}%
  16. \addcontentsline{toc}{#1}{%
  17. \ifnum #2>\c@secnumdepth \else
  18. \protect\numberline{\csname the#1\endcsname}%
  19. \fi
  20. #7}%
  21. \else
  22. \def\@svsechd{%
  23. #6{\hskip #3\relax
  24. \@svsec \colorbox{red}{#8}}%
  25. \csname #1mark\endcsname{#7}%
  26. \addcontentsline{toc}{#1}{%
  27. \ifnum #2>\c@secnumdepth \else
  28. \protect\numberline{\csname the#1\endcsname}%
  29. \fi
  30. #7}}%
  31. \fi
  32. \@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
Site admin

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am

Postby bstex » Thu Oct 26, 2017 10:08 pm

OMG!
It works. I just had inserted an * on the title of my first section
  1. \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: 8537
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Oct 26, 2017 10:15 pm

You can add for supporting starred sections too:

  1. \patchcmd{\@ssect}{#5}{\colorbox{red}{#5}}{}{}


Stefan
Site admin

bstex
Posts: 27
Joined: Mon Oct 23, 2017 10:22 am

Postby bstex » Thu Oct 26, 2017 10:23 pm

You spoil me. :D
Thank you again so much!

User avatar
Stefan Kottwitz
Site Admin
Posts: 8537
Joined: Mon Mar 10, 2008 9:44 pm
Location: Hamburg, Germany
Contact:

Postby Stefan Kottwitz » Thu Oct 26, 2017 10:41 pm

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
Site admin


Return to “Theses, Books, Title pages”

Who is online

Users browsing this forum: No registered users and 2 guests