3

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)

enter image description here

  • 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
1

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}
  • 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
  • 1
    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
  • 1
    @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
  • 1
    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
3

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}

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

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

Table of contents

enter image description here

Titles

enter image description here

Header

enter image description here

  • the code works perfectly and solved all the problems but the * symbol is displayed in the margin and not in the main text body (see the attached image above). This is not a serious issue, however (i can live with it), but if can be solved all would be perfect... thanks for your help – Athanasios Margaris Sep 14 '18 at 7:09
  • 2
    @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

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.