目录对齐

目录对齐

我翻译的书的目录包括必读章节(学习所需),例如 1.1、1.2、1.3,可选章节(在章节编号前用 * 表示),例如 *1.4 以及没有编号的章节(问题、难题等)。目录按附图所示对齐章节编号。主要有以下五个情况:(以第 16 章为例)

*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)

是否有可能实现这样的对齐?我使用回忆录类

在此处输入图片描述

我提供了一个最小的工作示例来展示当前的情况,我想修改代码以给出上面描述的结果..非常感谢您的支持(如果无法准确地得到这个结果,我希望我们至少可以右对齐章节编号和编号章节下的未编号章节)

\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
\chapter [chapToc1][chapHeader1]{chapTitle1}
\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]
\addcontentsline{toc}{section}{Unnumbered Section 1}
\section * {Unnumbered Section 1}
\lipsum[1-2]
\addcontentsline{toc}{section}{Unnumbered Section 2}
\section * {Unnumbered Section 2}
\lipsum[1-2]
\end{document} 

\optionalsection 命令是应我的 coleygr 的要求而实现的,可以在这里找到

修改的部分命令

答案1

这是一个使用修改的部分命令作为基础:

在此处输入图片描述

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

这个想法是向目录写入一个与主文档中显示的不同的计数器值。\phantom{0}如果节号小于 10,则此不同值会插入一个,从而有效地将其打印为在左侧填充空格的两位数。

答案2

使用我之前的问题中的解决方案和一个白色星号的解决方案:

\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[\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}

在此处输入图片描述

相关内容