宏:兼容 1)自动着色 & 2)自动计数和标题标签

宏:兼容 1)自动着色 & 2)自动计数和标题标签

这个问题是关于要求有以下 2“工作流程”(如果你允许我这样称呼它们)能够在 1 个 Tex 文档中共存:


  1. 工作流自动着色(≅用户 Mico 的回答在 OP 中自动为所有出现的某些字符串着色(不同的词组使用不同的颜色)

工作流自动着色平均能量损失

% !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. 工作流自动计数和标题标签(≅用户 Christian Hupfer 的回答在 OP 中如何将 \nameref 引用到计数器的某个实例(用作伪标题),在 \nameref 中使用数字并为伪标题添加标题/描述

工作流自动计数和标题标签MWE(编译两次)

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

工作流程 1 和 2 结合在一起(更改buff, "Candle Wisdom"buff, "sCandle Wisdom",因为否则无法编译)

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

您是否注意到Candle Wisdom(来自第二个工作流程)目前\labelText{The poem Candle Wisdom}{candle}Candle Wisdombuff = string.gsub ( buff, "Candle Wisdom", "\\textcolor{blue}{%0}" )目前与来自第一个工作流程的(因此我不得不改成buff, "sCandle Wisdom"

尽管如此,我们希望字符串Candle Wisdom在输出中始终显示为蓝色。但与此同时,我们需要Candle Wisdom包含文本字符串\labelText{The poem Candle Wisdom}{candle}(因为\labelText将是引文的标题,需要引用诗歌蜡烛智慧)。

提示(但也许会产生误导,例如):有人建议我解决方案可能是“保护”命令(有时可以使用\string\yourcommand);但那是在发布这个更精确的问题之前。

答案1

这是一个利用\hypertarget/的解决方案\hyperlink机制的解决方案超链接包来创建交叉引用链接。\hypertarget宏放置“锚点”,\hyperlink宏生成指向 生成的锚点的调出(以超链接的形式) 。这两个宏都接受两个参数:第一个是“锚点文本”(类似于“标准 LaTeX”中的和\hypertarget参数),第二个是将要排版的文本字符串。\label\ref

\hypertarget和的第二个参数\hyperlink可以包含(几乎)任意文本 - 包括字符串“Candle Wisdom”。:-) 相反,字符串“Candle Wisdom”应该不是\hypertarget出现在和的第一个参数中\hyperlink


在此处输入图片描述


在此处输入图片描述


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

答案2

这是一个很大程度上取决于用户 Mico 的答案中所做工作的答案,但相反;每当插入\hyperlink(链接到某个\hypertarget)时,该目标的标题(的第二个参数\hypertarget)就会自动用作该的第二个参数\hyperlink,通过添加以下代码摘录:

\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

平均能量损失

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

相关内容