4

For a manual-like document I'm writing, I have the need to save two additional items together with each section. For this I have created a custom command that creates a subsection under the section and separates the two items with an \hfill:

\NewDocumentCommand{\customSectionWithMetadata}{m o o}
{\section{#1}%
    \subsection{%
        \IfValueTF{#2}{#2}{}%
        \IfValueTF{#3}{%
            \hfill \ (#3)%
        }{}%
    }
}

Additionally, I have also implemented this answer to hide the \hfill from the TOC. Now I don't want to have the text from the subsection displayed as an additional item, but rather appended to the section text, preferably smaller and in a slightly lighter color.

What I have right now: enter image description here

What I am trying to achieve: enter image description here

Please note that I don't necessarily need to have the alias and the metadata as a subsection; if there is a better way to save them and display them both in the TOC and under the section title, then I'd be more than glad to switch to that.

(pretty large) Minimal Working Example:

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

The main problem with your code is that you are not using the built-in methods for separating the section heading from the stuff that goes into your TOC. But a standard sectioning command in has an optional argument for the TOC text, and memoir in fact has two optional arguments, one for the TOC text and one for the page header text. Since you are providing new sectioning commands anyway, you don't need the code in the linked question.

So we can use this to simplify your code quite a bit. As you suspected, there's no need to do this by adding \subsection commands. Instead, what we do is simply define a section heading which adds the meta and alias data as well on a new line.

There's also no need to have two different custom sectioning commands: if you supply 2 optional arguments, then you get the metadata formatting, and if you supply one optional argument you get the "(also: alias)" formatting. This simplifies the user interface a lot.

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

TOC image headings image headings image

| improve this answer | |

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.