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:
- for any type of sectioning command (including those of titlesec)
- 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}

\titleformat{\section}[hang]{\color{blue}\huge\bfseries}{\thesection}{1em}{\colorkeywordstrue}. – Jan 27 '15 at 09:06