# Listing Subsection title in same TOC line as Section in Memoir

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:

What I am trying to achieve:

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

\setsecnumformat{}

%%%
%%%
\makeatletter
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother

\settocdepth{subsection}

%%%
% 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]
{\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}

\chapter{Chapter 3}
\end{document}


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

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

%%%
%%%
\makeatletter
\patchcmd{\M@sect}{\@hangfrom}{}{}{}
\makeatother

\settocdepth{subsection}

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