在章节、段落等的标题后附加后缀,但在引用时省略

在章节、段落等的标题后附加后缀,但在引用时省略

我想在小段落标题后添加一个后缀,其定义如下:

\renewcommand{\subparagraph}{\@startsection{subparagraph}{5}{\z@}%
  {0.3\baselineskip \@plus1ex \@minus .2ex}%
  {0.75ex plus 0.1ex}% space after heading
  {\normalfont\mdseries\itshape}}

因此我认为以下修改可以做到这一点:

\renewcommand{\subparagraph}[1][]{\@startsection{subparagraph}{5}{\z@}%
  {0.3\baselineskip \@plus1ex \@minus .2ex}%
  {0.75ex plus 0.1ex}% space after heading
  {\normalfont\mdseries\itshape #1}}

但正如您所见,后缀作为前缀出现:

在此处输入图片描述

我需要将占位符放在哪里#1才能将后缀放在标题后面?还是说这是错误的方法?我也不希望在引用名称时包含后缀,这就是为什么我不只是将后缀包含在实际标题中。

有什么提示吗?


平均能量损失

\documentclass{scrreprt}

\usepackage{hyperref}

\setcounter{secnumdepth}{6} 

\makeatletter
\renewcommand{\subparagraph}[1][]{\@startsection{subparagraph}{5}{\z@}%
  {0.3\baselineskip \@plus1ex \@minus .2ex}%
  {0.75ex plus 0.1ex}% space after heading
  {\normalfont\mdseries\itshape #1}}
\renewcommand{\thesubparagraph}{\noindent AP \arabic{subsection}-\arabic{subparagraph}}
\makeatother

\begin{document}

\subparagraph[MySuffix]{Hello World!}\label{subpar}

I also reference my subparagraph, but I don't want the suffix to be included: \ref{subpar}\quad\nameref{subpar}

\end{document}

答案1

这会将后缀添加到 的标题\subparagraph,但省略命令ToC中的 和\nameref

不建议以可选第一个参数的形式添加此类后缀,并且需要一些\if...查询,附加大多数时候是更好的主意。

\documentclass{scrreprt}

\usepackage{xparse}
\makeatletter

\setcounter{tocdepth}{5}


\AtBeginDocument{%
\let\latex@@subparagraph\subparagraph

\NewDocumentCommand{\subparagraphtitleformat}{mo}{%
  \IfValueTF{#2}{%
    #1 -- {\normalfont\textit{#2}}
  }{%
    #1%
  }%
}   

\RenewDocumentCommand{\subparagraph}{somo}{%
  \IfBooleanTF{#1}{%
    \latex@@subparagraph{#3}%
  }{%
    \IfValueTF{#2}{%
      \latex@@subparagraph[#2]{\subparagraphtitleformat{#3}[#4]}%
    }{%
      \latex@@subparagraph[#3]{\subparagraphtitleformat{#3}[#4]}%
    }%
  }%
}

}
\makeatother



\usepackage{hyperref}

\setcounter{secnumdepth}{6} 

%\renewcommand{\subparagraph}[1][]{\@startsection{subparagraph}{5}{\z@}%
%  {0.3\baselineskip \@plus1ex \@minus .2ex}%
%  {0.75ex plus 0.1ex}% space after heading
%  {\normalfont\mdseries\itshape #1}}
\renewcommand{\thesubparagraph}{\noindent AP \arabic{subsection}-\arabic{subparagraph}}
%\makeatother

\begin{document}
\tableofcontents

\clearpage
\subparagraph{Hello World!}[MySuffix]\label{subpar}

\subparagraph{Hello World Again!}\label{othersubpar}

I also reference my subparagraph, but I don't want the suffix to be included: \ref{subpar}\quad\nameref{subpar}

\end{document}

在此处输入图片描述

答案2

轻微的补丁\@sect可以插入后缀:

在此处输入图片描述

\documentclass{scrartcl}

\usepackage{xparse,etoolbox}

\makeatletter

\renewcommand{\subparagraph}{\@startsection{subparagraph}{5}{\z@}%
  {0.3\baselineskip \@plus1ex \@minus .2ex}%
  {0.75ex plus 0.1ex}% space after heading
  {\normalfont\mdseries\itshape}}

\patchcmd{\@sect}{\@svsec{#8}}{\@svsec{#8 \csname suffix@title\endcsname}}{}{}

\let\oldsubparagraph\subparagraph

\subparagraph[*][<toc>]{<title>}[<suffix>]
\RenewDocumentCommand{\subparagraph}{somo}{{%
  \IfValueT{#4}{\def\suffix@title{#4}}% Suffix supplied
  \IfBooleanTF{#1}
    {\oldsubparagraph*{#3}}% \subparagraph*
    {\IfValueTF{#2}
       {\oldsubparagraph[#2]{#3}}% \subparagraph[.]{..}
       {\oldsubparagraph{#3}}% \subparagraph{..}
    }%
}}

\makeatother

\usepackage{hyperref}

\setcounter{tocdepth}{6}
\setcounter{secnumdepth}{6}

\renewcommand{\thesubparagraph}{AP \arabic{subsection}-\arabic{subparagraph}}

\begin{document}

\tableofcontents

\section{A section}

\subsection{A subsection}

\subsubsection{A subsubsection}

\paragraph{A paragraph}

\subparagraph{A subparagraph}[suffix]\label{spar:subpar}

\subparagraph{Another subparagraph}\label{spar:othersubpar}

I also reference my subparagraph, but I don't want the suffix to be included: \ref{spar:subpar}\quad\nameref{spar:subpar}

\end{document}

相关内容