2

Question

Is it possible to write a conditional statement for commands that change them based on where they appear in the document, yet is fully compatible with the titlesec package and the tocloft package? This is an extension of this original question. The original question does not address the issue of titlesec/titletoc/tocloft, three packages that I need.

Example Situation

When command appears in the body, it should be

  • bold and black in color.

When it appears in a heading/title (sectioning commands: \section \subsection, \paragraph etc.),

  • make it bold and red.

In the following code, I use the command \keyword in the heading \section and in the body. I would like to appearance of the text to change based on where it is found. This means I would need access to two conditions:

  1. for any type of sectioning command (including those of titlesec)
  2. for the body

Sample Code

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc,tocloft}

\titleformat{\section}[hang]{\color{blue}\huge\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}[hang]{\color{blue}\bfseries}{\thesubsection}{1em}{}
\titlecontents{section}[0mm]{\color{gray}\large\bfseries\thecontentslabel\quad}{}{}{}
\titlecontents{subsection}[2em]{\color{gray}}{\thecontentslabel\quad}{}{\titlerule*[10pt]{.}\contentspage}

\newcommand{\keyword}[1]{\textbf{#1}}

\begin{document}
\section{Product \keyword{rabunza}}
You can find the information about \keyword{rabunza} on our website.
\end{document}
1
  • 1
    egreg's answer works here too. Try \titleformat{\section}[hang]{\color{blue}\huge\bfseries}{\thesection}{1em}{\colorkeywordstrue}.
    – user11232
    Jan 27, 2015 at 9:06

1 Answer 1

5

As Harish Kumar says in the comment, egreg's solution works here, as well.

It suffices to turn to true the "colorkeywords" conditional inside your commands:

\titleformat{\section}[hang]{\colorkeywordstrue\color{blue}\huge\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}[hang]{\colorkeywordstrue\color{blue}\bfseries}{\thesubsection}{1em}{}
\titlecontents{section}[0mm]{\colorkeywordstrue\color{gray}\large\bfseries\thecontentslabel\quad}{}{}{}
\titlecontents{subsection}[2em]{\colorkeywordstrue\color{gray}}{\thecontentslabel\quad}{}{\titlerule*[10pt]{.}\contentspage}

MWE:

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{titletoc,tocloft}

\newif\ifcolorkeywords

\DeclareRobustCommand{\keyword}[1]{%
  \textbf{\ifcolorkeywords\color{red}\fi #1}%
}

\titleformat{\section}[hang]{\colorkeywordstrue\color{blue}\huge\bfseries}{\thesection}{1em}{}
\titleformat{\subsection}[hang]{\colorkeywordstrue\color{blue}\bfseries}{\thesubsection}{1em}{}
\titlecontents{section}[0mm]{\colorkeywordstrue\color{gray}\large\bfseries\thecontentslabel\quad}{}{}{}
\titlecontents{subsection}[2em]{\colorkeywordstrue\color{gray}}{\thecontentslabel\quad}{}{\titlerule*[10pt]{.}\contentspage}

\begin{document}

\tableofcontents

\section{Product \keyword{rabunza}}
You can find the information about \keyword{rabunza} on our website.
\end{document} 

Output

enter image description here

2
  • I tried your code and it works fine. I attempt to implement it in a larger document and my headings turn black just like in the other question. The keywords are red as they should be. Jan 27, 2015 at 9:54
  • Nevermind, I forgot to remove the sectsty package, which was conflicting with titlesec Jan 27, 2015 at 9:59

You must log in to answer this question.

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