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]
\msection[ΠεριεχομεναΚαι\textlatin{HEADER}]{Δευτερη ενότητα Δηλ: \textlatin{No 2}}
\lipsum[1-8]
\msection[\textlatin{Test3HEADandTOC}]{Τριτη ενότητα}
\lipsum[1-8]
\msection[Περιεχομενα 4][\textlatin{TEST4HEAD}]{Τεστ \textlatin{Section} 4}
\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]
\msection[Test2TOCANDHEAD,]{Test Section 2}
\lipsum[1-8]
\msection[,Test3HEAD]{Test Section 3}
\lipsum[1-8]
\msection[Test4TOC,TEST4HEAD]{Test Section 4}
\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]
\msection[Test2TOCANDHEAD,]{Test Section 2}
\lipsum[1-8]
\msection[,Test3HEAD]{Test Section 3}
\lipsum[1-8]
\msection[Test4TOC,TEST4HEAD]{Test \textlatin{Section} 4}
\lipsum[1-8]
\xdef\Sep{@}
\msection[TEST$\frac{5}{x}$,TOC@%\foreignlanguage{english}{
TEST5HEAD%}
]{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]
\msection[ΠεριεχομεναΚαιHEADER,]{Δευτερη ενότητα Δηλ: Νο 2}
\lipsum[1-8]
\msection[,Test3HEAD]{Τριτη ενότητα}
\lipsum[1-8]
\msection[Περιεχομενα4,TEST4HEAD]{Τεστ Section 4}
\lipsum[1-8]
\def\Sep{@}
\msection[Τεστ $\frac{5}{x}$,Περιεχομενα@TEST5HEAD]{Τεστ Section 5}
\lipsum[1-8]
\end{document}
\reffor the optional sections? In this case, should the asterisk appear? – egreg Sep 13 '18 at 21:45