The TOC of the book I translate, includes mandatory sections (required for study), for example, 1.1, 1.2, 1.3, optional sections denoted by a * before section number, for example, *1.4 and sections with no numbers (questions, problems, etc) .. the TOC aligns the section numbers as in the attached figure .. the five main cases are as follows : (for chapter 16, for example)

*16- 8 Section A   the 1-digit section number is aligned with the righmost
16- 9 Section B   digit of a section number of 2 digits

16-10 Section C   no * in the beginning - section number with 2 digits
*16-11 Section D   a * in the beginning - section number with 2 digits
Questions  (unnumbered section)
Problems   (unnumbered section)


Is it possible to implement such an alignment? i use memoir class

I provide a minimal working example that shows the current situation, and i would like to modify the code to give the result described above .. thanks a lot for your support (if it is not possible to get EXACTLY this result, i hope we can at least right justify the section numbers and the unnumbered sections under the numbered ones)

\documentclass[a4paper,twoside,10pt]{memoir}
%\usepackage[american,greek]{babel}
%\usepackage[iso-8859-7]{inputenc}
\usepackage{alphabeta}
\def\eng#1{\textlatin{#1}}
%\newcommand{\sg}{\selectlanguage{greek}}
%\newcommand{\sa}{\selectlanguage{american}}
%\usepackage{textgreek}
%\usepackage{lua-visual-debug}
%\usepackage{pst-solides3d} % 3D drawing
%\usepackage{pgfplots}      % 3D drawing
\usepackage{textcase}
\usepackage{silence}
\usepackage{kerkis}
\usepackage{color}
\usepackage[svgnames,x11names,named]{xcolor}
\WarningFilter*{mdframed}{You got a bad break}
\WarningFilter{latex}{Text page}
\usepackage[usetwoside]{mdframed}
\usepackage[LGR,T1]{fontenc}
\usepackage{anyfontsize}
\usepackage{lipsum}

\def\wlog#1{}
\DeclareRobustCommand{\GenericInfo}[2]{}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{systeme}
\usepackage{wrapfig}
\usepackage{afterpage}
\usepackage{tocloft}
\usepackage[figuresright]{rotating}
\usepackage{parskip}
\usepackage{caption}
\usepackage{enumitem}
\usepackage{titletoc}
\usepackage{calc}
%\usepackage{subcaption}
\usepackage{pstricks}
\usepackage{bbm}
%\usepackage{newlfont}
\usepackage{textcomp}
\usepackage{upgreek}
\usepackage{ifthen}
\usepackage{extramarks}
\usepackage{bm}
%\usepackage{pdfcolmk}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{colortbl}
\usepackage[symbol]{footmisc}

\setlength{\cftsectionnumwidth}{3.5em}% Width of \section numbers in ToC
\renewcommand{\cftsectionpresnum}{\hfill}% Inserted before \section numbers in ToC
\renewcommand{\cftsectionaftersnum}{\quad}% Inserts after \section numbers in ToC

\xdef\Sep{;}

\newcommand{\optionalsection}[2][]{%
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\ifx#1\empty
\section{#2}
\else
\StrBefore{#1}{\Sep}[\First]
\StrBehind{#1}{\Sep}[\Second]
\ifx\First\empty
\ifx\Second\empty
\section{#2}
\else
\section[#2][\Second]{#2}
\fi
\else
\ifx\Second\empty
\section[\First]{#2}
\else
\section[\First][\Second]{#2}
\fi
\fi
\fi
\renewcommand\thesection{\arabic{chapter}.\arabic{section}}%
}

\begin{document}
\tableofcontents
\section [textForTOC1][textForHeader1]{Section Title 1}
\lipsum[1-2]
\section [textForTOC2][textForHeader2]{Section Title 2}
\lipsum[1-2]
\optionalsection [textForTOC3;textForHeader3]{Section Title 3}
\lipsum[1-2]
\section [textForTOC4][textForHeader4]{Section Title 4}
\lipsum[1-2]
\section [textForTOC5][textForHeader5]{Section Title 5}
\lipsum[1-2]
\section [textForTOC6][textForHeader6]{Section Title 6}
\lipsum[1-2]
\section [textForTOC7][textForHeader7]{Section Title 7}
\lipsum[1-2]
\section [textForTOC8][textForHeader8]{Section Title 8}
\lipsum[1-2]
\section [textForTOC9][textForHeader9]{Section Title 9}
\lipsum[1-2]
\section [textForTOC10][textForHeader10]{Section Title 10}
\lipsum[1-2]
\optionalsection [textForTOC11;textForHeader11]{Section Title 11}
\lipsum[1-2]
\optionalsection [textForTOC12;textForHeader12]{Section Title 12}
\lipsum[1-2]
\section * {Unnumbered Section 1}
\lipsum[1-2]
\section * {Unnumbered Section 2}
\lipsum[1-2]
\end{document}


The \optionalsection command was implemented after my request by coleygr and it can be found here

A modified section command

• So 16-9 denotes Chapter 16, section 9? And you're using \chapter and \section... not some other contrived macro for this? What about your preamble? What sectional unit packages do you load? As a matter of fact, how about creating a minimal example showing what you currently use. We want to copy-and-paste-and-compile and see what you're seeing at the moment. – Werner Sep 13 '18 at 20:28
• i added above a minimal working example that shows the current situation ... – Athanasios Margaris Sep 13 '18 at 21:01
• For future reference, this is what is meant by a minimal example: I've just been asked to write a minimal example, what is that? – Werner Sep 13 '18 at 21:17
• @AthanasiosMargaris you have better code to add now from me and from egreg. Egreg's solution seems to respect this answer's request too. Mine doesn't even if improved to handle everything else in your previour question – koleygr Sep 13 '18 at 22:35

Here is an option that uses A modified section command as foundation:

\documentclass{memoir}

\usepackage{xparse,etoolbox}
\usepackage{kantlipsum}

\NewDocumentCommand{\optionalsection}{O{#3}om}{%
\renewcommand{\thesection}{\optasterisk\standardthesection}%
\optzerotrue
\IfNoValueTF{#2}{\section[#1]{#3}}{\section[#1][#2]{#3}}%
\optzerofalse
\renewcommand{\thesection}{\standardthesection}%
}
\AtBeginDocument{\let\standardthesection\thesection}

\NewDocumentCommand{\optasterisk}{}{%
\ifoptzero\makebox[0pt][r]{*}\else*\fi
}
\newif\ifoptzero

\makeatletter
\newcommand{\thesectionToC}{%
\thechapter.%
\ifnum\value{section}<10 \protect\phantom{0}\fi
\@arabic\c@section
}
\patchcmd
{\M@sect} % <cmd>
{\protect\numberline{\csname the#1\endcsname}} % <search>
{\ifnum\pdfstrcmp{#1}{section}=0
\protect\numberline{\ifoptzero\makebox[0pt][r]{*}\fi\csname the#1ToC\endcsname}
\else
\protect\numberline{\ifoptzero\makebox[0pt][r]{*}\fi\csname the#1\endcsname}
\fi} % <replace>
{}{}
\makeatother

\begin{document}

\frontmatter

\optzerotrue
\tableofcontents*
\optzerofalse

\mainmatter

\chapter{A chapter}

\section[first section][FIRST SECTION]{First section}

\optionalsection[second section][SECOND SECTION]{Second section}\label{opt}

This is a reference to \ref{opt}.

\kant % to see the header title

\setcounter{section}{8}

\section{Third section}
\optionalsection{Fourth section}
\section{Fifth section}

\setcounter{chapter}{9}

\chapter{Another chapter}

\section[first section][FIRST SECTION]{First section}

\optionalsection[second section][SECOND SECTION]{Second section}
\kant % to see the header title

\setcounter{section}{8}

\section{Third section}
\optionalsection{Fourth section}
\section{Fifth section}

\end{document}


The idea is to write a different counter value to the ToC than what is displayed in the main document. This different value inserts a \phantom{0} if the section number is less than 10, effectively printing it as a two-digit number padded on the left with a space.

• this works fine, thanks. It is possible for the unnumbered sections to align under the numbered sections and not under the section numbers? this is the only remaining issue – Athanasios Margaris Sep 14 '18 at 7:22

A solution using my solution in your previous question and a white asterisk:

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[english,greek]{babel}
\usepackage{xcolor}
\definecolor{WHITE}{rgb}{0.,0.,0.}
\makeatletter
\def\msection{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have optional parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}
\def\@StarredWith[#1]{%
\@ifnextchar[%
{\@StarredWithWith[#1]}
{\@StarredWithWithout[#1]}
}
\def\@StarredWithWithout[#1]#2{%
\ifx#1\empty\relax%
\else
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\fi
\section*[#1]{#2}%
}
\def\@StarredWithWith[#1][#2]#3{%
\ifx#1\empty\relax%
\else
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\fi
\section*[#1][#2]{#3}%
}
\def\@StarredWithout#1{%
\section*{#1}%
}
\def\@nonStarred{%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]{%
\@ifnextchar[%
{\@nonStarredWithWith[#1]}
{\@nonStarredWithWithout[#1]}%
}
\def\@nonStarredWithWith[#1][#2]#3{%
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section[#1][#2]{#3}%
}
\def\@nonStarredWithWithout[#1]#2{%
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section[#1]{#2}%
}

\def\@nonStarredWithout#1{%
\renewcommand\thesection{{\color{white}*}\arabic{chapter}.\arabic{section}}%
\section{#1}%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{\textlatin{Chapter}}
\msection{Εδώ είναι το \textlatin{Section} 1}
\lipsum[1-19]
\msection[ΠεριεχομεναΚαι\textlatin{HEADER}]{Δευτερη ενότητα Δηλ: \textlatin{No 2}}
\lipsum[1-8]
\msection[Τεστ $\frac{5}{x}$,Περιεχομενα][\textlatin{TEST5HEAD}]{Τεστ \textlatin{Section} 5}