1

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:


  1. 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}

  1. 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.

  • Should/may it be assumed that the hyperref package will always be loaded, or is such an assumption invalid? – Mico Apr 23 '16 at 7:07
  • @Mico Can you clarify what you mean please? I think I can answer your question by saying that some form of an automatic clickable hyperlink should be created with every reference given to a certain quote (in casu). – Vincent Mia Edie Verheyen Apr 23 '16 at 10:17
  • The hyperref package provides the macros \hypertarget and \hyperlink, which are (sort of) generalizations of \label and \ref. This mechanism should offer a workable solution; I just wanted to make sure that it's OK to assume that the hyperref package will be loaded by default. – Mico Apr 23 '16 at 12:00
1

Here's a solution that makes use of the \hypertarget/\hyperlink machinery of the hyperref package to create cross-referencing links. The \hypertarget macro places the "anchor", and the \hyperlink macro generates a call-out (in the form of a hyperlink) to the anchor generated by \hypertarget. Both macros take two arguments: the first is the "anchor text" (akin to the argument of \label and \ref in "standard LaTeX"), and the second is a text string that will be typeset.

The second arguments of \hypertarget and \hyperlink can contain (nearly) arbitrary text -- including the string "Candle Wisdom". :-) Conversely, the string "Candle Wisdom" should not occur in the first arguments of \hypertarget and \hyperlink.


enter image description here


enter image description here


% !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

    \usepackage[framemethod=tikz]{mdframed}
    \usetikzlibrary{calc}
    \usepackage[colorlinks,
                linkcolor=black  % set hyperlink color
               ]{hyperref}

    %%%%% 'quoter' environment is set up with help of 'mdframed' package
    \newcounter{quote}
    \renewcommand{\theHquote}{quote.\thequote}
    \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}


\begin{document}

\chapter{Quotes regarding ``Candle Wisdom''} \label{chap:Quotes}

If you are interested in this, please see the poem \hyperlink{ccc}{\bfseries Candle Wisdom}.

\vspace{1.5cm}\hrule\vspace{1.5cm} % for visual separation

    \begin{quoter} \label{q:CW}
    \begin{center}
    \hypertarget{ccc}{\bfseries The poem Candle Wisdom}
    \end{center}
    \vspace{0.5em}
    \hrule
    \vspace{1em}

    \obeylines
    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}

\noindent
A cross-reference to Quote~\ref{q:CW} located in Chapter~\ref{chap:Quotes}.

\lipsum[1-2]  % filler text
\end{document}
  • So, I guess to fully reproduce the original MWE's text, one should go for please see the poem \hyperlink{ccc}{\bfseries Quote~\ref{q:CW}: The poem Candle Wisdom}. The only dis-advantage I see is that you know have to manually type in the text for \hyperlink's second argument, where-as in OP's workflow 2, that was automatically generated (from \labelText) from a title of a quote: to minimize spelling mistakes, and save time. I guess this answer can find a solution for that: tex.stackexchange.com/questions/168832/…. – Vincent Mia Edie Verheyen Apr 23 '16 at 20:40
  • @VincentVerheyen - I actually see it as an advantage that the two text snippets -- the one in the anchor, the other in the hypertarget -- needn't be the same in the proposed solution. One more degree of freedom to influence what you're writing about and how it's displayed. :-) – Mico Apr 23 '16 at 20:44
  • Another proposed purpose is that one would wish to count the instances throughout the text of Candle Wisdom or so, but one wishes not to include the arguments of references to labels (or hyperlinks to targets). Since, they are kind of meta. Or, let's say one writes another macro to generate a list of the instances of Candle Wisdom at the end of the document (and e.g. also add hyperlinks at the entry of the list, to the pages were that entry occurs in-text): I am just fearing for other incompatibility issues, when that new macro would mess up the second argument of \hyperlink. – Vincent Mia Edie Verheyen Apr 23 '16 at 20:47
  • But at the same time, I can understand your point of view. I also suppose, for example, that it could be more easy for some macro's to catch up with the second argument if it is completely spelled out again manually. Such could e.g. be the case, in the following proposed workflow: tex.stackexchange.com/questions/305237/…. -- But one has to acknowledge that OP asked for Auto-counter-and-title-to-label, meaning (even though written badly) the title should be automatically recuperated in a link to it. – Vincent Mia Edie Verheyen Apr 23 '16 at 20:54
  • 1
    @VincentVerheyen - Ah, so you're on an older system that doesn't automatically set the option Ligatures=TeX when loading fonts. Please add this option to the option list when executing \setmainfont, \setsansfont, etc. You really ought to bring your entire TeX distribution up to date, by the way. – Mico Apr 24 '16 at 0:44
0

Here is an answer which depends heavily on the work done in User Mico's answer, but instead; whenever a \hyperlink (linking to a certain \hypertarget) is inserted, the title of that target (the second argument of \hypertarget) is automatically used as a second argument for that \hyperlink, by adding the following excerpt of code:

\makeatletter
\let\oldhypertarget\hypertarget
\renewcommand{\hypertarget}[2]{%
  \oldhypertarget{#1}{#2}%
    \protected@write\@mainaux{}{%
        \string\expandafter\string\gdef
          \string\csname\string\detokenize{#1}\string\endcsname{#2}%
    }%
  }
\newcommand{\myhyperlink}[1]{%
  \hyperlink{#1}{{\bfseries Quote~\ref{q:quotecounter}: \csname #1\endcsname}}%
  }
\makeatother

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{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

    \usepackage[framemethod=tikz]{mdframed}
    \usetikzlibrary{calc}
    \usepackage[colorlinks,
                linkcolor=black  % set hyperlink color
               ]{hyperref}

    %%%%% 'quoter' environment is set up with help of 'mdframed' package
    \newcounter{quote}
    \renewcommand{\theHquote}{quote.\thequote}
    \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}

\makeatletter
\let\oldhypertarget\hypertarget
\renewcommand{\hypertarget}[2]{%
  \oldhypertarget{#1}{#2}%
    \protected@write\@mainaux{}{%
        \string\expandafter\string\gdef
          \string\csname\string\detokenize{#1}\string\endcsname{#2}%
    }%
  }
\newcommand{\myhyperlink}[1]{%
  \hyperlink{#1}{{\bfseries Quote~\ref{q:quotecounter}: \csname #1\endcsname}}%
  }
\makeatother

\begin{document}

\chapter{Quotes regarding ``Candle Wisdom''} \label{chap:Quotes}
If you are interested in this, please see the poem \myhyperlink{ccc} or \myhyperlink{ddd}.

\vspace{1.5cm}\hrule\vspace{1.5cm} % for visual separation

    \begin{quoter} \label{q:quotecounter}
    \begin{center}
    \hypertarget{ccc}{\bfseries The poem Candle Wisdom}
    \end{center}
    \vspace{0.5em}
    \hrule
    \vspace{1em}

    \obeylines
    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}

    \begin{quoter} \label{q:quotecounter}
    \begin{center}
    \hypertarget{ddd}{\bfseries Whether I like Candle Wisdom}
    \end{center}
    \vspace{0.5em}
    \hrule
    \vspace{1em}

    \obeylines
    I like Candle Wisdom.\footnote{Unknown.}
    \end{quoter}

\noindent
A cross-reference to Quote~\ref{q:quotecounter} located in Chapter~\ref{chap:Quotes}.

\end{document}
  • @Mico I still have a problem with the counters there (when adding multiple quotes). Please take a look at my answer: it outputs: [...] please see the poem Quote 2: The poem [...] or Quote 2: Wheter I [...]. It should actually say: [...] Quote 1 [...] or Quote 2 [...]: do you know how to achieve that please? – Vincent Mia Edie Verheyen Apr 24 '16 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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