在节和小节内格式化关键字

在节和小节内格式化关键字

问题:

Google 帮不上忙,所以我求助于那些比 Google 更好的人。问题是我想创建一个“函数”,读取所有\section\subsection,如果该函数找到类似的模式,<keyword>那么它应该被格式化\texttt{}

例如:

\section{Headline <keyword>}

输出应如下:

\section{Headline \texttt{<keyword>}}

划界:

这只适用于\section\subsection

最小工作示例(MWE):

我通常会发布 MWE,但这次我找不到任何详细说明如何创建此类函数的指南。如果有人可以分享文档以便我启发自己,我将不胜感激。

更新(.cls 中的章节和小节代码):

%
% Section headings
%
\raggedbottomsection
%
\setsecindent{0pt}
\setbeforesecskip{-8mm plus -1mm minus -1mm}
\setaftersecskip{1.411mm plus 0.2mm minus 0.2mm}
\setsecnumformat{\csname the#1\endcsname\hspace*{0.5em}}
\setsecheadstyle{\rmfamily\mdseries\fontsize{14}{14}\selectfont\raggedright\color{\colsection}}
%
\setsubsecindent{0pt}
\setbeforesubsecskip{-24pt plus -3pt minus -2pt}
\setaftersubsecskip{4pt plus 0pt}
\setsubsecheadstyle{\sffamily\bfseries\boldmath\fontsize{9.5}{14}\selectfont\raggedright\color{\colsubsection}\MakeUppercase}
%

答案1

默认代码没有变化,类别代码也没有变化。

对参数(或在 的情况下的参数\section[x]{y})进行处理以查找<(any tokens)>(惰性搜索)并用 替换它\texttt{<(any tokens)>},然后传递给原始\section\subsection命令。

如果您打算使用提供的双重可选参数memoir,则需要对可选参数的扫描进行一些更改。

\documentclass{article}

\usepackage{xparse,l3regex}
\usepackage{color}

\ExplSyntaxOn
\cs_set_eq:NN \latexsection \section
\cs_set_eq:NN \latexsubsection \subsection

\RenewDocumentCommand{\section}{}
 {
  \genericsection{\latexsection}
 }
\RenewDocumentCommand{\subsection}{}
 {
  \genericsection{\latexsubsection}
 }
\NewDocumentCommand{\genericsection}{msom}
 {
  \IfValueT{#3}
   {
    \kexx_section_keyword:Nn \l_kexx_section_texto_tl { #3 }
   }
  \kexx_section_keyword:Nn \l_kexx_section_textm_tl { #4 }
  \IfBooleanTF{#2}
   {
    #1*{\l_kexx_section_textm_tl}
   }
   {
    \IfValueTF{#3}
     {
      #1[ \l_kexx_section_texto_tl ]{ \l_kexx_section_textm_tl }
     }
     {
      #1{ \l_kexx_section_textm_tl }
     }
   }
 }

\tl_new:N \l_kexx_section_textm_tl
\tl_new:N \l_kexx_section_texto_tl

\cs_new_protected:Nn \kexx_section_keyword:Nn
 {
  \tl_set:Nn #1 { #2 }
  \regex_replace_all:nnN { (\<.*?\>) } { \c{texttt}\cB\{\1\cE\} } #1
 }

\ExplSyntaxOff

\begin{document}

\section{Headline <keyword> \textcolor{red}{\itshape{this}}}

\subsection{Headline  <keyword>}

\subsection{Headline \textcolor{red}{<keyword>}}

\end{document}

在此处输入图片描述

为了适应的双重可选参数memoir,还需要做一些工作:

\documentclass{memoir}

\usepackage{xparse,l3regex}
\usepackage{color}

\ExplSyntaxOn
\cs_set_eq:NN \memoirsection \section
\cs_set_eq:NN \memoirsubsection \subsection

\RenewDocumentCommand{\section}{}
 {
  \genericsection{\memoirsection}
 }
\RenewDocumentCommand{\subsection}{}
 {
  \genericsection{\memoirsubsection}
 }
\NewDocumentCommand{\genericsection}{msoom}
 {
  \IfValueTF{#4}
   {
    \kexx_section_keyword:Nn \l_kexx_section_textob_tl { #4 }
    \kexx_section_keyword:Nn \l_kexx_section_textoa_tl { #3 }
   }
   {
    \IfValueT{#3}
     {
      \kexx_section_keyword:Nn \l_kexx_section_textoa_tl { #3 }
     }
   }
  \kexx_section_keyword:Nn \l_kexx_section_textm_tl { #5 }
  \IfBooleanTF{#2}
   {
    #1*{\l_kexx_section_textm_tl}
   }
   {
    \IfValueTF{#4}
     {
      #1[ \l_kexx_section_textoa_tl ][ \l_kexx_section_textob_tl ]{ \l_kexx_section_textm_tl }
     }
     {
      \IfValueTF{#3}
       {
        #1[ \l_kexx_section_textoa_tl ]{ \l_kexx_section_textm_tl }
       }
       {
        #1{ \l_kexx_section_textm_tl }
       }
     }
   }
 }

\tl_new:N \l_kexx_section_textm_tl
\tl_new:N \l_kexx_section_textoa_tl
\tl_new:N \l_kexx_section_textob_tl

\cs_new_protected:Nn \kexx_section_keyword:Nn
 {
  \tl_set:Nn #1 { #2 }
  \regex_replace_all:nnN { (\<.*?\>) } { \c{texttt}\cB\{\1\cE\} } #1
 }

\ExplSyntaxOff

\begin{document}

\section{Headline <keyword> \textcolor{red}{\itshape{this}}}

\subsection{Headline  <keyword>}

\subsection[<key>][aaa<key>]{Headline \textcolor{red}{<keyword>}}

\end{document}

更复杂的是获得大写的标题,但这是可能的。

\documentclass{memoir}

\usepackage{xparse,l3regex}
\usepackage{color}

\ExplSyntaxOn
\cs_set_eq:NN \memoirchapter \chapter
\cs_set_eq:NN \memoirsection \section
\cs_set_eq:NN \memoirsubsection \subsection

\tl_put_right:Nn \l_tl_case_change_exclude_tl { \textttnocase }

\RenewDocumentCommand{\chapter}{}
 {
  \genericsection{\use:n}{\memoirchapter}
 }
\RenewDocumentCommand{\section}{}
 {
  \genericsection{\use:n}{\memoirsection}
 }
\RenewDocumentCommand{\subsection}{}
 {
  \genericsection{\tl_upper_case:n}{\memoirsubsection}
 }
\NewDocumentCommand{\genericsection}{mmsoom}
 {
  \IfValueTF{#5}
   {
    \kexx_section_keyword:Nn \l_kexx_section_textob_tl { #5 }
    \kexx_section_keyword:Nn \l_kexx_section_textoa_tl { #4 }
   }
   {
    \IfValueT{#4}
     {
      \kexx_section_keyword:Nn \l_kexx_section_textoa_tl { #4 }
     }
   }
  \kexx_section_keyword:Nn \l_kexx_section_textm_tl { #6 }
  \IfBooleanTF{#3}
   {
    \use:x
     {
      \exp_not:N #2 * { \exp_not:N #1 {\exp_not:V \l_kexx_section_textm_tl} }
     }
   }
   {
    \IfValueTF{#5}
     {
      \use:x
       {
        \exp_not:N #2
         [ \exp_not:N #1 {\exp_not:V \l_kexx_section_textoa_tl} ]
         [ \exp_not:N #1 {\exp_not:V \l_kexx_section_textob_tl} ]
         { \exp_not:N #1 {\exp_not:V \l_kexx_section_textm_tl} }
       }
     }
     {
      \IfValueTF{#4}
       {
        \use:x
         {
          \exp_not:N #2
           [ \exp_not:N #1 {\exp_not:V \l_kexx_section_textoa_tl} ]
           { \exp_not:N #1 {\exp_not:V \l_kexx_section_textm_tl} }
         }
       }
       {
        \use:x
         {
          \exp_not:N #2 { \exp_not:N #1 {\exp_not:V \l_kexx_section_textm_tl} }
         }
       }
     }
   }
 }

\tl_new:N \l_kexx_section_textm_tl
\tl_new:N \l_kexx_section_texto_tl

\cs_new_protected:Nn \kexx_section_keyword:Nn
 {
  \tl_set:Nn #1 { #2 }
  \regex_replace_all:nnN { (\<.*?\>) } { \c{textttnocase}\cB\{\1\cE\} } #1
 }

\ExplSyntaxOff

\NewDocumentCommand{\redtext}{m}{\textcolor{red}{#1}}
\NewDocumentCommand{\textttnocase}{m}{\NoCaseChange{\texttt{#1}}}

\begin{document}

\chapter{Headline <keyword>}

\section{Headline <keyword> \redtext{\itshape{this}}}

\subsection{Headline  <keyword>}

\subsection[<key>][aaa<key>]{Headline\"a \redtext{<keyword>}}

\end{document}

在此处输入图片描述

请注意,页眉也需要特殊处理。

答案2

在此处输入图片描述

\documentclass{article}
\usepackage{color}
\makeatletter
\renewcommand\section{\@startsection {section}{1}{\z@}%
                                   {-3.5ex \@plus -1ex \@minus -.2ex}%
                                   {2.3ex \@plus.2ex}%
                                   {\normalfont\Large\bfseries\zz}}
\renewcommand\subsection{\@startsection{subsection}{2}{\z@}%
                                     {-3.25ex\@plus -1ex \@minus -.2ex}%
                                     {1.5ex \@plus .2ex}%
                                     {\normalfont\large\bfseries\zz}}
\def\zz#1{\makeatletter\catcode`\<\active\scantokens{#1}}
{\catcode`\<\active\gdef<#1>{\texttt{#1}}}

\makeatother

\begin{document}

\section{Headline <keyword> \textcolor{red}{\itshape{this}}}

zzzzzz

\subsection{Headline  <keyword>}

zzzzzz

\end{document}

添加到问题的片段似乎来自memoir所以这是相同的,但添加到回忆录而不是文章中

\documentclass{memoir}
\usepackage[T1]{fontenc}
\usepackage{color}
\makeatletter
\protected\def\colsection{green}
\protected\def\colsubsection{blue}
\protected\def\colzz{red}
%
\setsecindent{0pt}
\setbeforesecskip{-8mm plus -1mm minus -1mm}
\setaftersecskip{1.411mm plus 0.2mm minus 0.2mm}
\setsecnumformat{\csname the#1\endcsname\hspace*{0.5em}}
\setsecheadstyle{\rmfamily\mdseries\fontsize{14}{14}\selectfont\raggedright\color{\colsection}\zz}
%
\setsubsecindent{0pt}
\setbeforesubsecskip{-24pt plus -3pt minus -2pt}
\setaftersubsecskip{4pt plus 0pt}
\setsubsecheadstyle{\sffamily\bfseries\boldmath\fontsize{9.5}{14}\selectfont\raggedright\color{\colsubsection}\zzz}
%

\protected\def\zz#1{\makeatletter\catcode`\<\active\scantokens{#1}}
\protected\def\zzz#1{\makeatletter\catcode`\<\active \scantokens{\MakeUppercase{#1}}}
{\catcode`\<\active\protected\gdef<#1>{\texttt{\string <#1>}}}

\makeatother

\begin{document}

\section{Headline <keyword> \textcolor{\colzz}{\itshape{this}}}

zzzzzz

\subsection{Headline  <keyword>}

zzzzzz

\end{document}

相关内容