对于我正在编写的手册类文档,我需要将两个附加项目与每个部分一起保存。为此,我创建了一个自定义命令,该命令在该部分下创建一个子部分,并使用\hfill
:分隔这两个项目
\NewDocumentCommand{\customSectionWithMetadata}{m o o}
{\section{#1}%
\subsection{%
\IfValueTF{#2}{#2}{}%
\IfValueTF{#3}{%
\hfill \ (#3)%
}{}%
}
}
此外,我还实施了这个答案隐藏\hfill
目录中的文本。现在我不想将子节中的文本显示为附加项,而是将其附加到节文本中,最好是小一些且颜色稍浅一些。
请注意,我不一定需要将别名和元数据作为子节;如果有更好的方法来保存它们并将它们显示在目录中和章节标题下,那么我会非常乐意切换到它。
(相当大)最小工作示例:
\documentclass[a4paper,11pt, onecolumn,openany]{memoir}
%%%
% LOAD PACKAGES
%%%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[final]{microtype}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{kpfonts}
\setlrmarginsandblock{0.15\paperwidth}{*}{1} % Left and right margin
\setulmarginsandblock{0.2\paperwidth}{*}{1} % Upper and lower margin
\checkandfixthelayout
%%%
% CHAPTER, SECTION AND SUBSECTION FORMATTING
%%%
\maxsecnumdepth{part} % Disable numbering for chapters, sections and subsections
\makeatletter %
\makechapterstyle{standard}{
\setlength{\beforechapskip}{0\baselineskip}
\setlength{\midchapskip}{1\baselineskip}
\setlength{\afterchapskip}{8\baselineskip}
\renewcommand{\chapterheadstart}{\vspace*{\beforechapskip}}
\renewcommand{\printchaptername}{} % No chapter name
\renewcommand{\printchapternum}{} % no chapter number
\renewcommand{\chaptitlefont}{\centering\bfseries\LARGE}
\renewcommand{\printchaptertitle}[1]{\chaptitlefont ##1}
\renewcommand{\afterchaptertitle}{\par\nobreak\vskip \afterchapskip}
}
\makeatother
\chapterstyle{standard}
\setsecheadstyle{\normalfont\large\bfseries}
\setsecnumformat{}
\setsubsecheadstyle{\normalfont\normalsize\bfseries\color{gray}}
\setparaheadstyle{\normalfont\normalsize\bfseries}
\setparaindent{0pt}\setafterparaskip{0pt}
%%%
% TABLE OF CONTENTS FORMATTING
%%%
\makeatletter
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother
\maxtocdepth{subsection} % Only parts, chapters and sections in the table of contents
\settocdepth{subsection}
\AtEndDocument{\addtocontents{toc}{\par}} % Add a \par to the end of the TOC
%%%
% CUSTOM COMMANDS
%%%
\DeclareRobustCommand*{\IgnoreTOC}[1]{#1}
% \customSectionWithAlias{Name des Vorgehens}[opt.: Abkürzung oder alt. Name]
\NewDocumentCommand{\customSectionWithAlias}{m o}
{\section{#1%
\IfValueTF{#2}{%
\ (also: \textit{#2})}%
{}}%
}
% \customSectionWithMetadata{Name der Eigenschaft}[opt.: Abkürzung oder alt. Name][opt.: Benutzt in folgenden Vorgehen]
\NewDocumentCommand{\customSectionWithMetadata}{m o o}
{\section{#1}%
\subsection{%
\IfValueTF{#2}{#2}{}%
\IfValueTF{#3}{%
\IgnoreTOC\hfill \ (#3)%
}{}%
}
}
\usepackage{memhfixc}
\author{Author}
\title{Document Title}
\begin{document}
\frontmatter
\begingroup
\renewcommand*{\IgnoreTOC}[1]{}%
\tableofcontents*
\endgroup
\mainmatter
\chapter{Chapter 1}
\customSectionWithAlias{Custom Section No. 1}
\customSectionWithAlias{Custom Section No. 2}[alias]
\chapter{Chapter 2}
\customSectionWithMetadata{Custom Section No. 3}[alias][metadata]
\customSectionWithMetadata{Custom Section No. 4}[alias][metadata]
\chapter{Chapter 3}
\customSectionWithMetadata{Custom Section No. 5}[alias][metadata]
\end{document}
答案1
您的代码的主要问题是您没有使用内置方法将章节标题与目录中的内容分开。但是,标准分段命令有一个用于目录文本的可选参数,memoir
实际上有两个可选参数,一个用于目录文本,一个用于页眉文本。由于您无论如何都会提供新的分段命令,因此您不需要链接问题中的代码。
因此我们可以使用它来大大简化您的代码。正如您所怀疑的那样,无需通过添加\subsection
命令来实现这一点。相反,我们只需定义一个部分标题,并在新行上添加元数据和别名数据。
也无需使用两个不同的自定义分段命令:如果您提供 2 个可选参数,那么您将获得元数据格式,如果您提供一个可选参数,那么您将获得“(also: alias)”格式。这大大简化了用户界面。
\documentclass[a4paper,11pt, onecolumn,openany]{memoir}
%%%
% LOAD PACKAGES
%%%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[final]{microtype}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{kpfonts}
\setlrmarginsandblock{0.15\paperwidth}{*}{1} % Left and right margin
\setulmarginsandblock{0.2\paperwidth}{*}{1} % Upper and lower margin
\checkandfixthelayout
%%%
% CHAPTER, SECTION AND SUBSECTION FORMATTING
%%%
\maxsecnumdepth{part} % Disable numbering for chapters, sections and subsections
\makeatletter %
\makechapterstyle{standard}{
\setlength{\beforechapskip}{0\baselineskip}
\setlength{\midchapskip}{1\baselineskip}
\setlength{\afterchapskip}{8\baselineskip}
\renewcommand{\chapterheadstart}{\vspace*{\beforechapskip}}
\renewcommand{\printchaptername}{} % No chapter name
\renewcommand{\printchapternum}{} % no chapter number
\renewcommand{\chaptitlefont}{\centering\bfseries\LARGE}
\renewcommand{\printchaptertitle}[1]{\chaptitlefont ##1}
\renewcommand{\afterchaptertitle}{\par\nobreak\vskip \afterchapskip}
}
\makeatother
\chapterstyle{standard}
\newcommand*{\metacolor}{\color{gray}} % ADDED SINCE USED IN TWO PLACES
\setsecheadstyle{\metastyle} % NOW USES TWO LINE FORMAT
\setparaheadstyle{\normalfont\normalsize\bfseries}
\setparaindent{0pt}\setafterparaskip{0pt}
% TWO COMMANDS TO FORMAT THE METADATA SECTION STYLE
\newcommand*\metainfo{}
\newcommand{\metastyle}[1]{%
\sethangfrom{##1}
\normalfont\large\bfseries#1\par%
\normalfont\normalsize\bfseries\metacolor
\metainfo\par}
%%%
% TABLE OF CONTENTS FORMATTING
%%%
\makeatletter
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother
\maxtocdepth{subsection} % Only parts, chapters and sections in the table of contents
\settocdepth{subsection}
\AtEndDocument{\addtocontents{toc}{\par}} % Add a \par to the end of the TOC
%%%
% CUSTOM COMMANDS
%%%
%\DeclareRobustCommand*{\IgnoreTOC}[1]{#1} % THIS COMMAND NOT NEEDED
% THIS COMMAND COMBINED WITH THE NEXT ONE
% \customSectionWithAlias{Name des Vorgehens}[opt.: Abkürzung oder alt. Name]
%\NewDocumentCommand{\customSectionWithAlias}{m o}
%{\section{#1%
% \IfValueTF{#2}{%
% \ (also: \textit{#2})}%
% {}}%
%}
% \customSectionWithAlias{Name der Eigenschaft}[opt.: Abkürzung oder alt. Name][opt.: Benutzt in folgenden Vorgehen]
\NewDocumentCommand{\customSectionWithAlias}{m o o}
{\IfValueTF{#3}{
\renewcommand{\metainfo}{#2\hfill#3}
\section[#1 \metacolor#2 #3][#1]{#1}% change [#1] to the same as the first for headers if needed
}
{\IfValueTF{#2}
{\section{#1 (also: \textit{#2})}}
{\section{#1}}}
}
\usepackage{memhfixc}
\author{Author}
\title{Document Title}
\begin{document}
\frontmatter
% CAN NOW USE REGULAR \tableofcontents command
\tableofcontents*
\mainmatter
\chapter{Chapter 1}
\customSectionWithAlias{Custom Section No. 1}
\customSectionWithAlias{Custom Section No. 2}[alias]
\chapter{Chapter 2}
\customSectionWithAlias{Custom Section No. 3}[alias][metadata]
\customSectionWithAlias{Custom Section No. 4}[alias][metadata]
\chapter{Chapter 3}
\customSectionWithAlias{Custom Section No. 5}[alias][metadata]
\end{document}