替换功能在文档主体中起作用,而不是在宏中起作用

替换功能在文档主体中起作用,而不是在宏中起作用

在主体中调用时,替换函数\MakeURLHyperTargetFromSectionName{Replace all spaces with dashes} 有效,但调用时\def\currenthypertarget{\MakeURLHyperTargetFromSectionName{A string with spaces} }它不执行任何操作(即 currenthypertarget="带有空格的字符串"

[顺便说一句,替换函数“marian_replace”是我能找到的最简单的函数。实际上我想使用来自自动标签,自动生成密钥 但任何尝试将 mwe 与 regex_replace_all 一起使用的尝试都会引发错误]

平均能量损失

\documentclass[english,headings=optiontotocandhead]{scrartcl}
\makeatletter

\usepackage[colorlinks=true]{hyperref}
\usepackage{pdfcomment}
\usepackage{lipsum}
\usepackage{expl3,xparse}
%----------
\FamilyStringKey[.section]{KOMAarg}{hypertarget}{\currenthypertarget}
\newcommand*{\currenthypertarget}{}
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \ifx\currenthypertarget\@empty  %hypertarget has not been given
   % \def\currenthypertarget{#1.\csname the#1\endcsname} %use default e.g "section.1
    \def\currenthypertarget{\MakeURLHyperTargetFromSectionName{A string with spaces} } %automatically generate url from heading
  \fi  
  \hypertarget{\currenthypertarget}{}
  \@hangfrom{\hskip #2#3}{\pdftooltip{#4}{\#\currenthypertarget}}%
  \global\let\currenthypertarget\@empty
  \hyperref[toc]{\hphantom{M}} %make an invisible link back to TOC at end of section heading (colorlinks=true)
}
%---------------------
\ExplSyntaxOn
\NewDocumentCommand{\MakeURLHyperTargetFromSectionName}{m}
 {
  \marian_replace:nnn {#1} % {#2} {#3}
 }

\tl_new:N \l_marian_input_text_tl

\cs_new_protected:Npn \marian_replace:nnn #1 % #2 #3
 {
  \tl_set:Nn \l_marian_input_text_tl { #1 }
  \tl_replace_all:Nnn \l_marian_input_text_tl { ~ } { - }
  \tl_use:N \l_marian_input_text_tl
 }
\ExplSyntaxOff

\makeatother

\usepackage{babel}
\begin{document}

\section{Lyx Section1}

\MakeURLHyperTargetFromSectionName{Replace all spaces with dashes}

\subsection{Lyx subSection1}

\section{Lyx Sec Same Name}

\section{Lyx Sec Same Name}

\section[hypertarget={First.Section.Name}]{First Section Name} 
\lipsum[1] 
\section[hypertarget={Example.Section}]{Example Section} 
\lipsum[2] 
\section[hypertarget={anb}]{Final Section} 

See \hyperlink{First.Section.Name}{the first section} or 
\hyperlink{Example.Section}{the second section} or 
\hyperlink{Final.Section}{this final section}.

\newpage{}

\tableofcontents{}\label{toc}
\end{document}

答案1

首先#4\sectionlinesformat不仅是标题,而且是格式化的标题(有关此信息,请参阅 KOMA-Script 手册)。因此,您不仅要替换空格。另一种方法是使用\@currentlabelname(generated by hyperrefresp. namerefresp. scrartcl)。

其次,\marian_replace:nnn这是错误的,因为名称表示三个参数(属于 expl3 类型n),但只有一个。我不会在我的示例中使用它,因此,expl3如果您想知道正确的命名方案,请参阅手册以获取更多信息。

最后但同样重要的一点是,我认为,您想要的\currenthypertarget是扩展的替换而不是替换的定义。

为了解决最后一个问题,可以简单地替换空格,例如

\documentclass[headings=optiontotocandhead]{scrartcl}
\usepackage{hyperref}
\usepackage[draft]{pdfcomment}
\usepackage{lipsum}
\usepackage{expl3}

\FamilyStringKey[.section]{KOMAarg}{hypertarget}{\currenthypertarget}
\newcommand*{\currenthypertarget}{}
\makeatletter
\ExplSyntaxOn
\renewcommand*{\sectionlinesformat}[4]{%
  \ifx\currenthypertarget\@empty
    \let\currenthypertarget\@currentlabelname
    \tl_replace_all:Nnn\currenthypertarget { ~ } { - }
  \fi  
  \hypertarget{\currenthypertarget}{}\@hangfrom{\hskip
    #2#3}{\pdftooltip{#4}{\#\currenthypertarget}}%
  \global\let\currenthypertarget\@empty
}
\ExplSyntaxOff
\makeatother

\begin{document}
\section{First Section Name}
\lipsum[1]
\section[hypertarget={Example.Section}]{Example Section}
\lipsum[2]
\section[hypertarget={Final.Section}]{Final Section}
See \hyperlink{First-Section-Name}{the first section} or
\hyperlink{Example.Section}{the second section} or
\hyperlink{Final.Section}{this final section}.
\end{document}

要发挥全部魔力,定义一个\generatehypertarget类似于答案的标签生成命令上述链接的问题

\documentclass[headings=optiontotocandhead]{scrartcl}
\usepackage{hyperref}
\usepackage[draft]{pdfcomment}
\usepackage{lipsum}
\usepackage{xparse}

\FamilyStringKey[.section]{KOMAarg}{hypertarget}{\currenthypertarget}
\newcommand*{\currenthypertarget}{}
\ExplSyntaxOn
\tl_new:N \l_magguu_compactlabel_tl
\cs_new_protected:Nn \magguu_compactlabel:nn
 {
  \tl_set:Nn \l_magguu_compactlabel_tl { #2 }
  \show \l_magguu_compactlabel_tl
  % remove non alphabetic/space characters
  \regex_replace_all:nnN {[^a-zA-Z\s]} {} \l_magguu_compactlabel_tl
  % remove words less than three letter long
  \regex_replace_all:nnN {\b[a-zA-Z]{1,2}\b} {} \l_magguu_compactlabel_tl
  % remove leading spaces
  \regex_replace_once:nnN {\A\s+} {} \l_magguu_compactlabel_tl
  % remove trailing spaces
  \regex_replace_once:nnN {\s+\Z} {\ } \l_magguu_compactlabel_tl
  % change runs of space into an underscore
  \regex_replace_all:nnN {\s+} {_} \l_magguu_compactlabel_tl
  % truncate to #1 characters
  \regex_replace_all:nnN {(^\w{1,#1})\w*} {\1} \l_magguu_compactlabel_tl
  % remove a possible trailing underscore
  \regex_replace_once:nnN {_\Z} {} \l_magguu_compactlabel_tl
  % lowercase the string
  \tl_set:Nx \l_magguu_compactlabel_tl { \str_lower_case:f { \tl_use:N \l_magguu_compactlabel_tl } }
}
\NewDocumentCommand{\generatehypertarget}{ O{20} m }{%
  \magguu_compactlabel:nn { #1 } { #2 }
  \tl_set_eq:NN\currenthypertarget\l_magguu_compactlabel_tl
}  
\ExplSyntaxOff
\makeatletter
\renewcommand*{\sectionlinesformat}[4]{%
  \ifx\currenthypertarget\@empty
    \expandafter\generatehypertarget\expandafter{\@currentlabelname}%
  \fi  
  \hypertarget{\currenthypertarget}{}\@hangfrom{\hskip
    #2#3}{\pdftooltip{#4}{\#\currenthypertarget}}%
  \global\let\currenthypertarget\@empty
}
\makeatother

\begin{document}
\section{First Section Name}
\lipsum[1]
\section[hypertarget={Example.Section}]{Example Section}
\lipsum[2]
\section[hypertarget={Final.Section}]{Final Section}
See \hyperlink{first_section_name}{the first section} or
\hyperlink{Example.Section}{the second section} or
\hyperlink{Final.Section}{this final section}.
\end{document}

相关内容