# A modified section command

I use the memoir \section command

\section[TOC title][Header title]{Main title}


together with a modified section command defined as

\newcommand{\optionalsection}[1]{%
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section{#1}%
\renewcommand\thesection{\arabic{chapter}.\arabic{section}}%
}


that simply puts an asterisk in front of the section number as a mark that this is an optional section. The problem is that the command

\optionalsection[TOC title][Header title]{Main title}


does not work. Is is possible to fix this problem?

EDIT: The code of egreg works perfectly but the * symbol is displayed in the margin (see the attached figure)

• Do you plan to use \ref for the optional sections? In this case, should the asterisk appear? – egreg Sep 13 '18 at 21:45
• no, there are no \ref commands – Athanasios Margaris Sep 13 '18 at 22:08

LAST EDIT RECOMMENDED AND WORKING SOLUTION:

It is based on my answer here: https://tex.stackexchange.com/a/380116/120578

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[english,greek]{babel}

\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{\arabic{chapter}.\arabic{section}}%
\section{#1}%
}
\makeatother

\begin{document}
\tableofcontents

\chapter{\textlatin{Chapter}}
\msection{Εδώ είναι το \textlatin{Section} 1}
\lipsum[1-19]
\lipsum[1-8]
\lipsum[1-8]
\lipsum[1-8]
\def\Sep{@}
\msection[Τεστ $\frac{5}{x}$,Περιεχομενα][\textlatin{TEST5HEAD}]{Τεστ \textlatin{Section} 5}
\lipsum[1-8]

\end{document}


(OLD) FIRST ANSWER (CAN NOT ACCEPT \textlatin inside the titles):

A solution with a change to the format of your command to: \msection[<TOC Title><Sep><HEADER Title>] where SEP is the separator that can be a comma (,) or whatever symbol you don't need in your titles.

\documentclass{memoir}
\usepackage{xstring}
\usepackage{lipsum}

\xdef\Sep{,}

\newcommand{\msection}[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
}

\begin{document}
\tableofcontents

\chapter{Chapter}
\msection{Test Section 1}
\lipsum[1-19]
\lipsum[1-8]
\lipsum[1-8]
\lipsum[1-8]
\xdef\Sep{@}
\msection[TEST$\frac{5}{x}$,TOC@Test5,HEAD]{Test Section 5}
\lipsum[1-8]
\end{document}


Be careful to give every time the separator if you use an optional argument.

(OLD) SECOND ANSWER (Can Accept \textlatin inside the title but NOT inside the optional arguments/(the short titles) ):

New code:

\documentclass{memoir}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[english,greek]{babel}

\newsavebox{\myfbox}
\newsavebox{\mysbox}
\newsavebox{\mytbox}
\xdef\Sep{,}
\newcommand{\msection}[2][]{%
\savebox\mytbox{\hbox{#2}}
\ifx#1\empty
\section{\usebox{\mytbox}}
\else
\StrBefore{#1}{\Sep}[\First]
\savebox\myfbox{\hbox{\First}}
\StrBehind{#1}{\Sep}[\Second]
\savebox\mysbox{\hbox{\Second}}
\IfStrEq{\First}{\empty}
{  \IfStrEq{\Second}{\empty}
{\section{\usebox{\mytbox}}
}
{\section[\usebox{\mytbox}][\usebox{\mysbox}]{\usebox{\mytbox}}
}
}
{  \IfStrEq{\Second}{\empty}
{\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section[\usebox{\myfbox}]{\usebox{\mytbox}}
}
{
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section[\usebox{\myfbox}][\usebox{\mysbox}]{\usebox{\mytbox}}
}
}
\fi
}

\begin{document}
\tableofcontents

\chapter{Chapter}
\msection{Test \textlatin{Section} 1}
\lipsum[1-19]
\lipsum[1-8]
\lipsum[1-8]
\lipsum[1-8]
\xdef\Sep{@}
\msection[TEST$\frac{5}{x}$,TOC@%\foreignlanguage{english}{
]{Test \textlatin{Section} 5}
\lipsum[1-8]
\end{document}


(OLD) THIRD ANSWER (A XeLaTeX working solution but the first in order (above) is better):

\documentclass{memoir}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage{polyglossia}
\setmainlanguage{greek}
%\setotherlanguage{english}
%
\usepackage{xgreek}
\newfontfamily\greekfont{Linux Libertine O}

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

\begin{document}
\tableofcontents

\chapter{Chapter}
\msection{Εδώ είναι το Section 1}
\lipsum[1-19]
\lipsum[1-8]
\lipsum[1-8]
\lipsum[1-8]
\def\Sep{@}
\msection[Τεστ $\frac{5}{x}$,Περιεχομενα@TEST5HEAD]{Τεστ Section 5}
\lipsum[1-8]
\end{document}

• When I use in the optional arguments the \textlatin{...} command for english words (i write in greek) an error message is displayed .. i can avoid the problem by using math mode - for example instead of \textlatin{Gauss} i write $Gauss$ .. this works, but the word is displayed as in math mode :-) – Athanasios Margaris Sep 13 '18 at 19:41
• I made it to work but english can only be inside the section command... not as an optional and I suppose you need it to work there too – koleygr Sep 13 '18 at 20:13
• @AthanasiosMargaris I added a XeLaTeX solution... I think you can give it a try. I use it very often for my Greek notes. You can use any (otf or ttf) font of your system. – koleygr Sep 13 '18 at 21:13
• Also I added another way that will not give a problem in starred sections. Check egreg's answer too. – koleygr Sep 13 '18 at 22:19

You can use xparse to avoid clumsy redefinitions of the internals. There will be no problem with \textlatin and similar commands.

\documentclass{memoir}

\usepackage{xparse}
\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

\begin{document}

\frontmatter

\optzerotrue
\tableofcontents*
\optzerofalse

\mainmatter
\chapter{Test}

\optionalsection[TOC title opt][Header title opt]{Main title opt}\label{opt}

This is a reference to \ref{opt}.

\kant % to see the header title

\end{document}


This way the asterisk will have zero width in the toc and in the section titles, but will take its space in \ref and in headers.

• @AthanasiosMargaris I did that on purpose; if you want differently, replace \ifoptzero\makebox[0pt][r]{*}\else*\fi with *% – egreg Sep 14 '18 at 9:32