This question is about asking to have the following 2 "workflows" (if you permit me to call them as such) to be able to coexist together in 1 Tex document:
- Workflow Auto-colorization (≅ User Mico's answer in OP Automatically colourize all occurrences of certain strings (different colouring for different sets of words):
Workflow Auto-colorization MWE
% !TEX TS-program = lualatex
\documentclass{book}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman} % choose various fonts, as needed
\usepackage{xcolor} % for "\textcolor" and "\colorlet" macros
\colorlet{BLUE}{blue}
\usepackage{lipsum} % filler text
\usepackage{luacode} % for "luacode" environment and "\luastring" macro
%% Lua-side code
\begin{luacode}
function colorize ( buff )
buff = string.gsub ( buff, "Candle Wisdom", "\\textcolor{blue}{%0}" )
return buff
end
\end{luacode}
%% TeX-side code
\newcommand\colorizeOn{\directlua{luatexbase.add_to_callback
( "process_input_buffer" , colorize , "colorize" )}}
\newcommand\colorizeOff{\directlua{luatexbase.remove_from_callback
( "process_input_buffer" , "colorize" )}}
\AtBeginDocument{\colorizeOn} % turn Lua function on by default
\begin{document}
\chapter{All quotes which deal with "Candle Wisdom"}\label{Quotes}
\end{document}
- Workflow Auto-counter-and-title-to-label (≅ User Christian Hupfer's answer in OP How to \nameref to a certain instance of a counter (used as pseudo-caption), use number in \nameref and add a title/description to the pseudo-caption:
Workflow Auto-counter-and-title-to-label MWE (compile twice)
\documentclass{book}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{hyperref}
\usetikzlibrary{calc}
%%%%% quoter environment
\newcounter{quote}
\renewcommand{\theHquote}{quote.\thequote}
\makeatletter
\newcommand{\labelText}[2]{%
#1%
\immediate\write\@auxout{%
\string\newlabel{#2}{{1}{\thepage}{{Quote \thequote: #1}}{quote.\theHquote}{}}%
}%
}
\makeatother
\newmdenv[
hidealllines=true,
innertopmargin=16pt,
innerbottommargin=10pt,
leftmargin=0cm,
rightmargin=0cm,
skipabove=10pt,
skipbelow=10pt,
settings={\refstepcounter{quote}},
singleextra={
\coordinate (aux) at ( $ (O)!0.5!(P) $ );
\fill[rounded corners=6pt,line width=1pt,blue!30]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) {[sharp corners] --
([yshift=-6pt]P) --
([yshift=-6pt]O|-P) } -- cycle;
\draw[rounded corners=6pt,line width=1pt,blue]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) --
(P|-O) --
(O) -- cycle;
\node at ([xshift=2cm,yshift=3pt]aux|-P)
{\large Quote~\thequote} ;
},
firstextra={
\coordinate (aux) at ( $ (O)!0.5!(P|-O) $ );
\fill[rounded corners=6pt,line width=1pt,blue!30,overlay]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) {[sharp corners] --
([yshift=-6pt]P) --
([yshift=-6pt]O|-P) } -- cycle;
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O) --
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) --
(P|-O);
\node[overlay] at ([xshift=2cm,yshift=3pt]aux|-P)
{\large Quote~\thequote} ;
},
middleextra={
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O|-P) --
(O);
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(P) --
(P|-O);
},
secondextra={
\coordinate (aux) at ( $ (O)!0.5!(P|-O) $ );
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O|-P) --
(O) --
(P|-O) --
(P);
},
]{quoter}
%\string\newlabel{#2}{{1}{\thepage}{{#1}}{mylabelcounter.\number\value{mylabelcounter}}{}}
\begin{document}
If you are interested in this, please see \textbf{\nameref{candle}}.
\clearpage
\begin{quoter} %\label{quoter}
\begin{center}
\textbf{\labelText{The poem Candle Wisdom}{candle}}
\vspace{0.5em}
\hrule
\vspace{1em}
\end{center}
If you knew\\
what you will know\\
when your candle\\
has burnt low,\\
it would greatly\\
ease your plight\\
while your candle\\
still burns bright.\footnote{Piet Hein.}
\end{quoter}
\end{document}
Workflows 1 and 2 together (changed buff, "Candle Wisdom" to buff, "sCandle Wisdom", since doesn't compile otherwise)
% !TEX TS-program = lualatex
\documentclass{book}
\usepackage{fontspec}
\setmainfont{Latin Modern Roman} % choose various fonts, as needed
\usepackage{xcolor} % for "\textcolor" and "\colorlet" macros
\colorlet{BLUE}{blue}
\usepackage{lipsum} % filler text
\usepackage{luacode} % for "luacode" environment and "\luastring" macro
%% Lua-side code
\begin{luacode}
function colorize ( buff )
buff = string.gsub ( buff, "sCandle Wisdom", "\\textcolor{blue}{%0}" )
return buff
end
\end{luacode}
%% TeX-side code
\newcommand\colorizeOn{\directlua{luatexbase.add_to_callback
( "process_input_buffer" , colorize , "colorize" )}}
\newcommand\colorizeOff{\directlua{luatexbase.remove_from_callback
( "process_input_buffer" , "colorize" )}}
\AtBeginDocument{\colorizeOn} % turn Lua function on by default
\usepackage[framemethod=tikz]{mdframed}
\usepackage{hyperref}
\usetikzlibrary{calc}
%%%%% quoter environment
\newcounter{quote}
\renewcommand{\theHquote}{quote.\thequote}
\makeatletter
\newcommand{\labelText}[2]{%
#1%
\immediate\write\@auxout{%
\string\newlabel{#2}{{1}{\thepage}{{Quote \thequote: #1}}{quote.\theHquote}{}}%
}%
}
\makeatother
\newmdenv[
hidealllines=true,
innertopmargin=16pt,
innerbottommargin=10pt,
leftmargin=0cm,
rightmargin=0cm,
skipabove=10pt,
skipbelow=10pt,
settings={\refstepcounter{quote}},
singleextra={
\coordinate (aux) at ( $ (O)!0.5!(P) $ );
\fill[rounded corners=6pt,line width=1pt,blue!30]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) {[sharp corners] --
([yshift=-6pt]P) --
([yshift=-6pt]O|-P) } -- cycle;
\draw[rounded corners=6pt,line width=1pt,blue]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) --
(P|-O) --
(O) -- cycle;
\node at ([xshift=2cm,yshift=3pt]aux|-P)
{\large Quote~\thequote} ;
},
firstextra={
\coordinate (aux) at ( $ (O)!0.5!(P|-O) $ );
\fill[rounded corners=6pt,line width=1pt,blue!30,overlay]
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) {[sharp corners] --
([yshift=-6pt]P) --
([yshift=-6pt]O|-P) } -- cycle;
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O) --
(O|-P) --
(aux|-P) --
([yshift=12pt]aux|-P) --
([yshift=12pt,xshift=4cm]aux|-P) --
([xshift=4cm]aux|-P) --
(P) --
(P|-O);
\node[overlay] at ([xshift=2cm,yshift=3pt]aux|-P)
{\large Quote~\thequote} ;
},
middleextra={
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O|-P) --
(O);
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(P) --
(P|-O);
},
secondextra={
\coordinate (aux) at ( $ (O)!0.5!(P|-O) $ );
\draw[rounded corners=6pt,line width=1pt,blue,overlay]
(O|-P) --
(O) --
(P|-O) --
(P);
},
]{quoter}
%\string\newlabel{#2}{{1}{\thepage}{{#1}}{mylabelcounter.\number\value{mylabelcounter}}{}}
\begin{document}
\chapter{All quotes which deal with "Candle Wisdom"}\label{Quotes}
If you are interested in this, please see \textbf{\nameref{candle}}.
\clearpage
\begin{quoter} %\label{quoter}
\begin{center}
\textbf{\labelText{The poem Candle Wisdom}{candle}}
\vspace{0.5em}
\hrule
\vspace{1em}
\end{center}
If you knew\\
what you will know\\
when your candle\\
has burnt low,\\
it would greatly\\
ease your plight\\
while your candle\\
still burns bright.\footnote{Piet Hein.}
\end{quoter}
I had to change \textbf{buff, "Candle Wisdom"} into \textbf{buff, "sCandle Wisdom"}, since it doesn't compile otherwise.
\end{document}
Can you notice how the Candle Wisdom in \labelText{The poem Candle Wisdom}{candle} (from the second workflow) is currently not compatible with the Candle Wisdom in buff = string.gsub ( buff, "Candle Wisdom", "\\textcolor{blue}{%0}" ) from the first workflow (hence I had to change to buff, "sCandle Wisdom")?
Nevertheless, we would like the string Candle Wisdom to ALWAYS be coloured blue in the output. But, at the same time, we need to have the text string Candle Wisdom in \labelText{The poem Candle Wisdom}{candle} (since that \labelText will be the title of the quote, which will need to refer to the poem Candle Wisdom).
Hint (but perhaps misleading, in casu): I have been given the suggestion that a solution might be to "protect" a commands (as can be sometimes done using \string\yourcommand); but that was before posting this more precise question.


hyperrefpackage will always be loaded, or is such an assumption invalid? – Mico Apr 23 '16 at 07:07hyperrefpackage provides the macros\hypertargetand\hyperlink, which are (sort of) generalizations of\labeland\ref. This mechanism should offer a workable solution; I just wanted to make sure that it's OK to assume that thehyperrefpackage will be loaded by default. – Mico Apr 23 '16 at 12:00