1

For a bigger writing project of article type book, I would like to color the heading and the text of certain sections, subsections, and subsubsections in different colors.

  • Normal text is as usual in black (commands section, subsection, subsection)
  • Text and heading of what I wrote very recently should be red (e.g. via new commands sectionNew, subsectionNew, ...)
  • Text and heading of what has already been corrected several times and is approved by a native speaker I would like to display in green (e.g. sectionFinished, subsectionFinshed, etc.)

I can define the commands of the new sections like this

\newcommand\sectionNew[1]{\section{\color{red}#1}\color{red}}
\newcommand\subsectionNew[1]{\subsection{\color{red}#1}\color{red}}
...

\newcommand\sectionFinished[1]{\color{green}\section{#1}\color{green}}
...

One way to get the behavior I desire is to add { and } around the section[New|Finished] commands I defined and the following text.

{
 \sectionNew{Heading1}
     Lorem ipsum.
}

However, I would like to avoid this if possible, as the curly brackets IMHO make the text less readable in the LaTeX file.

Without the curly brackets, the above mentioned has the problem that all subsequent sections without a specific definition are colored red or green as well (until encountering another explicit color statement). I think maybe renewing the section command with explicitly defining black as the color could be the solution. Unfortunately, I can not figure out how to renew the section command in a generic way (taking over everything from the document type except an explicit color statement).

Is there any solution to this?

Or maybe is there a package that does this already for me (i.e. a kind of text management/versioning inside the latex document)?

UPDATE: One drawback of this is that it is not possible to use an alternative title for the page headings anymore (\section[Alt Title for Heading]{Normal Title}).

2
  • You could add \leavevmode: \newcommand\sectionNew[1]{\section{\color{red}#1}\color{red}\leavevmode} \newcommand\sectionFinished[1]{\color{green}\section{#1}\color{green}\leavevmode}. But do you really want to do that?
    – user121799
    Sep 4, 2018 at 14:05
  • @marmot: Thanks for your answer! However, it seems that it does not change anything in my case. Should it?
    – Exocom
    Sep 4, 2018 at 18:21

2 Answers 2

0

You can use environments instead of commands for sectioning:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{xcolor}
\usepackage{blindtext}
\newenvironment{sectionNew}[1]
  {\section[#1]{\textcolor{red}{#1}}\color{red}}
  {\normalcolor}
\newenvironment{subsectionNew}[1]
  {\subsection[#1]{\textcolor{red}{#1}}\color{red}}
  {\normalcolor}
\newenvironment{sectionFinished}[1]
  {\section[#1]{\textcolor{green}{#1}}\color{green}}
  {\normalcolor}
\let\endsection\relax
\let\endsubsection\relax

\begin{document}

\tableofcontents

\begin{sectionNew}{Foo}
\blindtext
\end{sectionNew}

\begin{section}{Bar}
\blindtext
\end{section}

\begin{subsectionNew}{Baz}
    \blindtext
\end{subsectionNew}

\begin{sectionFinished}{Foobar}
\blindtext
\end{sectionFinished}

\end{document}

enter image description here

0

After tinkering around a bit more, I think I found a solution:

\let\sectionOrig\section
\renewcommand\section[1]{\color{black}\sectionOrig{#1}}
....

Unfortunately, convincing chapter to work similarly is a bit more complicated, but can be achieved like this (solution is from here):

\let\chapterOrig\chapter
\renewcommand*\chapter{\color{black}\@ifstar{\starchapter}{\@dblarg\nostarchapter}}
\newcommand*\starchapter[1]{\chapterOrig*{#1}}
\def\nostarchapter[#1]#2{\chapterOrig[{#1}]{#2}}

The only thing that could be improved is the section numbering, as this does not change color. However, this does not affect the functionality in my case and I can live with that.

You must log in to answer this question.

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