用图标作为章节标题的前缀

用图标作为章节标题的前缀

我正在尝试用特定图标标记某些部分。我希望图标仅显示在文本本身中,而不是目录中(尽管部分本身应该显示),也不显示在部分的名称引用中。它应该位于同一行上的部分标题之前。我下面使用的这个命令大部分都按我想要的方式工作:

\documentclass{article}

\usepackage{hyperref}
\usepackage{graphbox}

\graphicspath{{icons/}{images/}}

\newcommand{\invisiblesection}[1]{
  \refstepcounter{section}
  \phantomsection
  \addcontentsline{toc}{section}{#1}
  \sectionmark{#1}\phantom{}
}


\newcommand{\noticesubsection}[1]{
  \vspace{1em}
  \includegraphics[align=b, width=1em, height=1em]{example-image}~\textbf{\large #1}\invisiblesection{#1}
  \vspace{0.5em} \\
}

\begin{document}
    \tableofcontents

    \noticesubsection{Sample}\label{samepleref}
    Some Text and a sample Name Reference: \nameref{samepleref}

\end{document}

请注意,我的文档中不使用带编号的章节,因此如果编号不显示,对我来说也没关系。但我使用了很多按章节名称引用的引用,但在这种情况下这些引用不起作用。我相信有一种方法可以通过使用 \subsection 命令本身来实现,这样如果更改常规章节标题格式,它也会适用于此。我该怎么做呢?

答案1

我建议如下。\noticesection用法如下\section——包括带星号的形式,表示“未编号”——除了它在末尾需要一个额外的可选参数,即要使用的标签(如果不为空)。你必须使用此参数来定义“值得注意的”部分的标签(即以 开头的部分\noticesection);如果您\label在调用 之后才使用\noticesection,它将不起作用。

如下例所示:

  • 正如您所要求的,“值得注意的部分”总是列在目录中;

  • 支持编号和未编号的“显著部分”;

  • 它们可以与普通\sections (也与\section*) 很好地配合使用。

\documentclass{article}
\usepackage{graphicx}
% \usepackage{xparse}           % only needed for LaTeX older than 2020-10-01
% 'expand' is needed for the \GetTitleStringDisableCommands technique
\usepackage[expand]{gettitlestring}
% Hyperref options: only for aesthetics
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}

\makeatletter
\ExplSyntaxOn

\cs_new_protected:Npn \__onepie_insert_icon:
  {
    \makebox[0pt][r]
      {
        \includegraphics[height=1.5ex]{example-image}
        \skip_horizontal:n { 1em }
      }
  }

% Make sure the commands inserting the icon don't leak inside the nameref of
% unnumbered sections.
\GetTitleStringDisableCommands
  { \cs_set_eq:NN \__onepie_insert_icon: \c_empty_tl }

% #1: optional star for an unnumbered “noticeable section”
% #2: optional title used in headers and table of contents
% #3: title used in text
% #4: optional label
\NewDocumentCommand \noticesection { s O{#3} m O{} }
  {
    \IfBooleanTF {#1}
      { % unnumbered
        \section*{\__onepie_insert_icon: #3}
        \addcontentsline{toc}{section}{#2}
        \sectionmark{#2}
      }
      { % numbered
        \cs_set_eq:NN \__onepie_orig_@seccntformat:n \@seccntformat % back up

        \cs_set_protected:Npn \@seccntformat ##1
          {
            \__onepie_insert_icon:      % before the section number
            \use:c { the##1 } \quad
          }
        \section[#2]{#3}
        \cs_set_eq:NN \@seccntformat \__onepie_orig_@seccntformat:n % restore
      }

    \tl_if_empty:nF {#4} { \label{#4} }
  }

\ExplSyntaxOff
\makeatother

\begin{document}

\tableofcontents

\section{Normal section}
\label{sec:normal-first}

\noticesection*{An unnumbered ``noticeable'' section}[sec:unnum-nsection]

\section{Other normal section}
\label{sec:normal-second}

\noticesection{A numbered ``noticeable'' section}[sec:num-nsection]

\leavevmode\indent              % only for aesthetics
%
First normal section is number~\ref{sec:normal-first},
named \nameref{sec:normal-first}.

Second normal section is number~\ref{sec:normal-second},
named \nameref{sec:normal-second}.

The unnumbered ``noticeable'' section is named \nameref{sec:unnum-nsection}.

The numbered ``noticeable'' section is number~\ref{sec:num-nsection} and named
\nameref{sec:num-nsection}.

\end{document}

截屏

如果您希望目录中未编号的“值得注意的部分”按如下方式缩进,请将定义\addcontentsline{toc}{section}{#2}中的替换为\noticesection以下内容:

\addcontentsline{toc}{section}{\protect\numberline{}#2}

截屏

最后,如果您使用一个类,比如book在标题中打印章节和部分标题,并希望看到未编号的“明显部分”:

在此处输入图片描述

而不是:

在此处输入图片描述

(其中 1.2 是之前-numbered 部分),您可以使用上面代码的这个变体:

\documentclass{book}
\usepackage{graphicx}
% \usepackage{xparse}           % only needed for LaTeX older than 2020-10-01
\usepackage{lipsum}
% 'expand' is needed for the \GetTitleStringDisableCommands technique
\usepackage[expand]{gettitlestring}
% Hyperref options: only for aesthetics
\usepackage[colorlinks=true,linkcolor=blue]{hyperref}

\makeatletter
\ExplSyntaxOn

\int_new:N \l__onepie_prev_secnumdepth_int

\cs_new_protected:Npn \__onepie_insert_icon:
  {
    \makebox[0pt][r]
      {
        \includegraphics[height=1.5ex]{example-image}
        \skip_horizontal:n { 1em }
      }
  }

% Make sure the commands inserting the icon don't leak inside the nameref of
% unnumbered sections.
\GetTitleStringDisableCommands
  { \cs_set_eq:NN \__onepie_insert_icon: \c_empty_tl }

% #1: optional star for an unnumbered “noticeable section”
% #2: optional title used in headers and table of contents
% #3: title used in text
% #4: optional label
\NewDocumentCommand \noticesection { s O{#3} m O{} }
  {
    \IfBooleanTF {#1}
      { % unnumbered
        \section*{\__onepie_insert_icon: #3}
        \addcontentsline{toc}{section}{\protect\numberline{}#2}
        \int_set_eq:NN \l__onepie_prev_secnumdepth_int \c@secnumdepth
        \setcounter{secnumdepth}{0} % disable numbering in header
        \sectionmark{#2}
        % Restore secnumdepth
        \int_gset_eq:NN \c@secnumdepth \l__onepie_prev_secnumdepth_int
      }
      { % numbered
        \cs_set_eq:NN \__onepie_orig_@seccntformat:n \@seccntformat % back up

        \cs_set_protected:Npn \@seccntformat ##1
          {
            \__onepie_insert_icon:      % before the section number
            \use:c { the##1 } \quad
          }
        \section[#2]{#3}
        \cs_set_eq:NN \@seccntformat \__onepie_orig_@seccntformat:n % restore
      }

    \tl_if_empty:nF {#4} { \label{#4} }
  }

\ExplSyntaxOff
\makeatother

\begin{document}

\tableofcontents

\chapter{First chapter}

\section{Normal section}
\label{sec:normal-first}

\lipsum[1-2]

\section{Other normal section}
\label{sec:normal-second}

\lipsum[1-7]

\noticesection*{An unnumbered ``noticeable'' section}[sec:unnum-nsection]

\lipsum[1-7]

\noticesection{A numbered ``noticeable'' section}[sec:num-nsection]

\leavevmode\indent              % only for aesthetics
%
First normal section is number~\ref{sec:normal-first},
named \nameref{sec:normal-first}.

Second normal section is number~\ref{sec:normal-second},
named \nameref{sec:normal-second}.

The unnumbered ``noticeable'' section is named \nameref{sec:unnum-nsection}.

The numbered ``noticeable'' section is number~\ref{sec:num-nsection} and named
\nameref{sec:num-nsection}.

\end{document}

相关内容