我使用 memoir \section 命令
\section[TOC title][Header title]{Main title}
以及修改后的部分命令,定义为
\newcommand{\optionalsection}[1]{%
\renewcommand\thesection{*\arabic{chapter}.\arabic{section}}%
\section{#1}%
\renewcommand\thesection{\arabic{chapter}.\arabic{section}}%
}
这只是在节号前面加上一个星号,以标记这是一个可选节。问题是该命令
\optionalsection[TOC title][Header title]{Main title}
不起作用。可以修复此问题吗?
编辑:egreg 的代码运行完美,但边缘处显示 * 符号(见附图)
答案1
最后编辑推荐和有效的解决方案:
它基于我在这里的回答: 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}
(旧)第一个答案(不能接受\textlatin
标题内的内容):
解决方案是将命令的格式更改为:
\msection[<TOC Title><Sep><HEADER Title>]
其中 SEP 是分隔符,可以是逗号 (,) 或标题中不需要的任何符号。
\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}
如果使用可选参数,请小心每次都给出分隔符。
(旧)第二个答案(可以在标题内接受\textlatin
,但不能在可选参数/(简称)内接受):
新代码:
\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}
(旧)第三个答案(XeLaTeX 工作解决方案,但按顺序(上面)的第一个更好):
\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}
答案2
您可以使用来避免笨拙地重新定义内部结构。使用和类似命令xparse
不会出现任何问题。\textlatin
\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}
这样,星号在目录和章节标题中的宽度为零,但会在\ref
页眉中占据其空间。