3

Problem:

Google is not being helpful so I turn to those better than Google. The problem is that I want to create a "function" that reads all \section and \subsection and if the function finds a pattern like <keyword> then it should be formatted with \texttt{}.

For instance:

\section{Headline <keyword>}

Should output as if it was:

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

Delimitation:

This should only apply to \section and \subsection.

Minimal Working Example (MWE):

I usually post a MWE but this time I could not find any guide that details how to create such a function. I would appreciate if someone could share documentation so I could enlighten myself.

Update (section and subsection code from .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
    To me this sounds more like a custom defined \keyword or a search-and-replace instead of modifying the \section and \subsection commands. – bombcar Mar 27 '17 at 18:24
3

No change to the default code and no category code change either.

The argument (or arguments in case of \section[x]{y}) are massaged looking for <(any tokens)> (lazy search) and substituting it with \texttt{<(any tokens)>}, then passed to the original \section or \subsection commands.

If you plan to use the double optional argument provided by memoir, some changes are needed in the scanning of the optional arguments.

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

enter image description here

In order to accommodate the double optional argument of memoir, some more work is needed:

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

More complicated is getting uppercase titles, but it's possible.

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

enter image description here

Note that page headers need special treatment, too.

  • This is a good solution, is it possible to make the text (not <keyword>) in a subsection uppercase? – kexxcream Apr 14 '17 at 18:21
  • Yes please, it's only the subsection. – kexxcream Apr 14 '17 at 19:19
  • @kexxcream added – egreg Apr 14 '17 at 20:03
  • For some reason, that distorted current headlines. Using your first option with latexsection and latexsubsection kept things as they are. Is it possible to integrate these instead of memoir? – kexxcream Apr 14 '17 at 20:16
  • 1
    @kexxcream Look for l3regex on the site, there are several examples. – egreg Apr 14 '17 at 20:31
3

enter image description here

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

the fragment added to the question appears to be from memoir so here's the same but added to memoir rather than article

\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}
  • Cheers @David, however if I want to make sure that < and > follows with the formatting, what would I need to change? – kexxcream Mar 27 '17 at 18:28
  • @kexxcream sorry I don't understand the comment – David Carlisle Mar 27 '17 at 19:06
  • My apologies for not being clear. I would like the output to include < and > around the keyword. Would it also be possible to not break the text formatting? – kexxcream Mar 27 '17 at 19:07
  • @kexxcream oh \texttt{\string<#1>} in that case – David Carlisle Mar 27 '17 at 19:29
  • Thanks, but it seems to me that existing formatting for section and subsection breaks, is there a way around this? Sorry for not stating this already in my original post. – kexxcream Mar 27 '17 at 19: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.