Recursive \peek_meaning inside section title
\documentclass{article}
\usepackage{xcolor}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand \blubb { }
{
\peek_meaning_remove:NTF \blubb { \colorTwo } { \colorOne }
}
\ExplSyntaxOff
\NewDocumentCommand \colorOne { m } {\textcolor{red}{#1}}
\NewDocumentCommand \colorTwo { m } {\textcolor{blue}{#1}}
\begin{document}
Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}
\section{Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}}
% ^ does not work with hyperref without warnings
\end{document}
But note the console output:
Package hyperref Warning: Token not allowed in a PDF string (PDFDocEncoding):
(hyperref) removing `\blubb' on input line 139.
This is because hyperref
makes a bookmark for the \section
which cannot include the \blubb
macro. See hyperref
's documentation for details. We can use \texorpdfstring{}{}
to avoid the problem.
\documentclass{article}
\usepackage{xcolor}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{hyperref}
\ExplSyntaxOn
\NewDocumentCommand \blubb { }
{
\peek_meaning_remove:NTF \blubb { \colorTwo } { \colorOne }
}
\ExplSyntaxOff
\NewDocumentCommand \colorOne { m } {\textcolor{red}{#1}}
\NewDocumentCommand \colorTwo { m } {\textcolor{blue}{#1}}
\begin{document}
Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}
\section{Lorem Ipsum \texorpdfstring{\blubb}{}{is red} Dolor Sit \texorpdfstring{\blubb\blubb}{}{is blue}}
\end{document}
A more convenient way to avoid the warning, however, maybe to use hyperref
's hook to disable \blubb
when necessary. For example (and also using the recommended bookmark
package):
\documentclass{article}
\usepackage{xparse,xcolor}
\usepackage{hyperref}
\usepackage{bookmark}
\ExplSyntaxOn
\NewDocumentCommand \blubb { }
{
\peek_meaning_remove:NTF \blubb { \colorTwo } { \colorOne }
}
\ExplSyntaxOff
\NewDocumentCommand \colorOne { m } {\textcolor{red}{#1}}
\NewDocumentCommand \colorTwo { m } {\textcolor{blue}{#1}}
\makeatletter
\pdfstringdefDisableCommands{%
\let\blubb\@firstofone
}
\makeatother
\begin{document}
Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}
\section{Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}}
\end{document}
A different approach.
\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\def\blubb#1{%
\ifx\blubb#1\relax%
\def\blubbcolor{blue}\expandafter\blubbhelp%
\else
\def\blubbcolor{red}\blubbhelp{#1}%
\fi%
}
\def\blubbhelp#1{\textcolor{\blubbcolor}{#1}}
\begin{document}
Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}
Lorem Ipsum \blubb{is red}Dolor Sit \blubb\blubb{is blue}without trailing spaces.
\section{Lorem Ipsum \blubb{is red} Dolor Sit \blubb\blubb{is blue}}
% ^ does not work with hyperref
\end{document}
As cfr rightly notes, \blubb
generates warnings when used in section titles using hyperref
, for the purposes of creating bookmarks. It can be overcome with \texorpdfstring
, us as follows: \section{Lorem Ipsum \texorpdfstring{\blubb{is red}}{is red} Dolor Sit \texorpdfstring{\blubb\blubb{is blue}}{is blue}}
, for example.