Question

Is it possible to write a conditional statement for commands that change them based on where they appear in the document?

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
  2. for the body

Sample Code

\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}

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

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

Question Extension

I have extended this question here to include titlesec/titletoc/tocloft compatibility.

up vote 7 down vote accepted

Hook in the commands that produce the section titles, the easiest way is with sectsty:

\documentclass{article}
\usepackage{sectsty}
\usepackage{xcolor}

\allsectionsfont{\colorkeywordstrue}
\newif\ifcolorkeywords

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

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

enter image description here

How does sectsty work? It adds hooks to the sectioning commands (it's not important to know how). These hooks are called

  • \allsectionsfont, which affects all titles
  • \<level>font which affects the specified level (\sectionfont, \subsectionfont and so on).

The names are due to the fact that the package was thought for easily changing the formatting of titles, but the hooks are simply executed at the right moment and can contain anything.

In this case, we add the declaration \colorkeywordstrue, that makes the conditional \ifcolorkeywords return true. By default it returns false; the hooks are executed in a group, so the meaning of \ifcolorkeywords will be restored as soon as the section title has been typeset.

  • I like having this alternate approach. It does wonders for my understanding of latex! Unlike Werner's answer, I do not understand your code yet. This is likely because I have not read the documentation for sectsty. Your answer does seem more "conditional"-based. – Jonathan Komar Jan 26 '15 at 8:49
  • Your code seems to break titlesec, which is essential for my work. :) This was not in my minimal working example, but I wish I had put it in there now. – Jonathan Komar Jan 27 '15 at 8:25
  • @macmadness86 How can I know it, since you don't even mention titlesec in your question? It's obvious that sectsty and titlesec conflict with each other. – egreg Jan 27 '15 at 8:29
  • It was certainly not obvious to me that including titlesec would be an issue until after I tested it. Afterall, it is called a "minimal" working example. Of course I realize that this was not a criterion for my original question, hence my previous comment. – Jonathan Komar Jan 27 '15 at 8:43

sectsty provides hooks into sectional units where you can change the font. We could sneek in a redefinition of \keyword as well:

enter image description here

\documentclass{article}
\usepackage{xcolor,sectsty}

\newcommand{\sectionkeyword}{% Update \keyword in sectional unit
  \renewcommand{\keyword}[1]{\textcolor{red}{\bfseries ##1}}}
\newcommand{\keyword}[1]{\textbf{#1}}
\sectionfont{\normalfont\Large\bfseries\sectionkeyword}

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

This can be extended to other sectional units in a very similar way. See the sectsty documentation for information on the other units' font-macros.

  • Is this answer compatible with \titlesec and custom sectioning commands? I could figure this out on my own. Maybe you know off-hand? – Jonathan Komar Jan 26 '15 at 8:55
  • 2
    @macmadness86: No (although not tested). titlesec uses its own macro setup for constructing sectional titles; completely different from that of sectsty. – Werner Jan 26 '15 at 15:26

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.