带有 xtemplate 的目录(expl3)

带有 xtemplate 的目录(expl3)

每次我排版一些书籍或文章时,我都会开始更新分段命令(\part、、\section一些\@-commands 等)、、\maketitle命令,\tableofcontents方法是将其复制粘贴到我的类中,然后进行更改以获得所需的视图。我认为这有点乏味,对于 LaTeX 新手来说,这项任务令人生畏,正如 中的许多帖子所证明的那样tex.stackexchange。最近我发现了xtemplate-package。现在我想\tableofcontents用它重新创建。模板应该看起来像(我需要考虑一下)。

\DeclareTemplateInterface{toc}{toc}{1}
{
  contentsnameformat: tokenlist ,
  chapterview : tokenlist ,
  sectionview : tokenlist ,

  %and so on
}

现在我的小挑战是使用 覆盖\addcontentsline\@starttoc函数expl3。但我不知道该怎么做。我需要一些初步帮助。

答案1

前言

为了回答这个问题,概述一种可能的解决方法,并展示一个概念证明,我不知何故忘乎所以......:$ 代码开发成一个小的包。

它似乎可以合理地用于标准类别,但我不提供任何保证 :) 此外 - 即使我忘乎所以 - 它距离功能完整和接近最初的目标还有很长的路要走:概述一条可能的前进道路。

使用示例

\documentclass{article}
% \documentclass{report}

\usepackage{xtoc}

\setxtoc{
  % top-entry-dotted = true ,
  main-entry-dotted = true ,
  sub-entry-formatting = \itshape ,
  preamble = Just a test.
}

\begin{document}

\tableofcontents

\listoffigures

\part{part: a somewhat longer text for demonstration purposes spanning at
  least two lines}
\section{section: a somewhat longer text for demonstration purposes spanning
  at least two lines}
\subsection{subsection: a somewhat longer text for demonstration purposes
  spanning at least two lines}
\subsubsection{subsubsection: a somewhat longer text for demonstration
  purposes spanning at least two lines}
\section{section: a somewhat longer text for demonstration purposes spanning
  at least two lines}
\subsection{subsection: a somewhat longer text for demonstration purposes
  spanning at least two lines}
\subsubsection{subsubsection: a somewhat longer text for demonstration
  purposes spanning at least two lines}
\begin{figure}
  \caption{bla bla bla}
\end{figure}

\end{document}

在此处输入图片描述

包裹代码 ( xtoc.sty)

代码尚未注释。无论如何,我希望它清晰易懂。如果不是,我会尝试添加一些解释。默认设置是标准类的设置。

% http://tex.stackexchange.com/q/246595/
\ProvidesPackage{xtoc}[2016/09/11 v0.1 formatting of the table of contents (CN)]
\RequirePackage{xtemplate,etoolbox}

\newbool{@xtoc@noredef}
\DeclareOption{noredef}{\booltrue{@xtoc@noredef}}
\ProcessOptions\relax

\newbool{@xtoc@topentrydotted}
\newbool{@xtoc@mainentrydotted}
\newbool{@xtoc@subentrydotted}

\newbool{@xtoc@toppagenumberright}
\newbool{@xtoc@mainpagenumberright}
\newbool{@xtoc@subpagenumberright}

\newrobustcmd*\@xtoc@fillwidthdots{%
  \leaders\hbox{%
    $\m@th\mkern\@xtoc@dotseparation mu
    \hbox{\@xtoc@dotsymbol}
    \mkern\@xtoc@dotseparation mu$%
  }%
}

\newrobustcmd*\@xtoc@entry[3]{%
  \leavevmode
  \csuse{@xtoc@#1entryformatting}%
  #2%
  \ifbool{@xtoc@#1pagenumberright}
    {\nobreak\ifbool{@xtoc@#1entrydotted}{\@xtoc@fillwidthdots}{}\hfil}{}%
  \makebox
    [\@xtoc@pagenumberwidth]%
    [\@xtoc@pagenumberalign]%
    {\@xtoc@pagenumberformatting#3}%
  \ifbool{@xtoc@#1pagenumberright}{}{\hfil\null}%
  \par
}

\newrobustcmd*\xtocsubentry[5]{%
  \ifnum #1>\@xtoc@leveldepth \else
    \vskip \@xtoc@subentryprespace
    \begingroup
      \leftskip#2\relax
      \rightskip\@xtoc@rightmargin
      \parfillskip -\rightskip
      \parindent #2\relax \@afterindenttrue
      \interlinepenalty\@M
      \@tempdima #3\relax
      \advance\leftskip \@tempdima
      \hskip -\leftskip
      \@xtoc@entry{sub}{#4}{#5}%
    \endgroup
  \fi
}

\newrobustcmd*\xtocmainentry[5]{%
  \ifnum \@xtoc@leveldepth >\z@
    \addpenalty\@secpenalty
    \addvspace{\@xtoc@mainentryprespace}%
    \setlength\@tempdima{#3}%
    \begingroup
      \parindent\z@
      \rightskip\@xtoc@pagenumberwidth
      \parfillskip -\@xtoc@pagenumberwidth
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      \@xtoc@entry{main}{#4}{#5}%
    \endgroup
  \fi
}

\newrobustcmd*\xtoctopentry[5]{%
  \ifnum \@xtoc@leveldepth >-2\relax
    \addpenalty\@secpenalty
    \addvspace{\@xtoc@mainentryprespace}%
    \setlength\@tempdima{#3}%
    \begingroup
      \parindent\z@
      \rightskip\@xtoc@pagenumberwidth
      \parfillskip -\@xtoc@pagenumberwidth
      \@xtoc@entry{top}{#4}{#5}%
      \nobreak
    \endgroup
  \fi
}%

\newrobustcmd*\@xtoc@makel@entry[1]{%
  \csdef{l@#1}{%
    \csuse{@xtoc@#1entry}
      {\csuse{@xtoc@#1level}}
      {\csuse{@xtoc@#1indent}}
      {\csuse{@xtoc@#1numwidth}}%
  }%
}

\DeclareObjectType{xtoc}{0}

\DeclareTemplateInterface{xtoc}{list-of}{0}
  {
    file-extension           : tokenlist   = toc ,
    heading-function         : function{1} = \section*{#1} ,
    heading                  : tokenlist   = \contentsname ,
    header                   : tokenlist   = \MakeUppercase\contentsname ,
    preamble                 : tokenlist ,
    preamble-post-space      : skip        = \baselineskip ,
    postamble                : tokenlist ,
    postamble-pre-space      : skip        = \baselineskip ,
    right-margin             : length      = 2.55em ,
    dot-separation           : tokenlist   = 4.5 ,
    dot-symbol               : tokenlist   = . ,
    page-number-width        : length      = 1.55em ,
    page-number-alignment    : choice{left,center,right} = right ,
    page-number-formatting   : tokenlist ,
    depth                    : integer     = 3 ,
    %
    top-entry-pre-space      : skip        = 2.25em \@plus\p@ ,
    top-entry-formatting     : tokenlist   = \large\bfseries ,
    top-entry-dotted         : choice{true,false} = false ,
    top-entry-page-number-right : choice{true,false} = true ,
    %
    main-entry-pre-space     : skip        = 1.0em \@plus\p@ ,
    main-entry-formatting    : tokenlist   = \bfseries ,
    main-entry-dotted        : choice{true,false} = false ,
    main-entry-page-number-right : choice{true,false} = true ,
    %
    sub-entry-pre-space      : skip        = \z@ \@plus.2\p@ ,
    sub-entry-formatting     : tokenlist ,
    sub-entry-dotted         : choice{true,false} = true ,
    sub-entry-page-number-right : choice{true,false} = true ,
    %
    part-entry               : function{5} = \xtoctopentry{#1}{#2}{#3}{#4}{#5} ,
    chapter-entry            : function{5} = \xtocmainentry{#1}{#2}{#3}{#4}{#5} ,
    section-entry            : function{5} = \xtocmainentry{#1}{#2}{#3}{#4}{#5} ,
    subsection-entry         : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    subsubsection-entry      : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    paragraph-entry          : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    subparagraph-entry       : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    figure-entry             : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    table-entry              : function{5} = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
    %
    part-level               : integer     = 0 ,
    chapter-level            : integer     = 1 ,
    section-level            : integer     = 1 ,
    subsection-level         : integer     = 2 ,
    subsubsection-level      : integer     = 3 ,
    paragraph-level          : integer     = 4 ,
    subparagraph-level       : integer     = 5 ,
    figure-level             : integer     = 1 ,
    table-level              : integer     = 1 ,
    %
    part-indent              : length      = 0pt ,
    chapter-indent           : length      = 0pt ,
    section-indent           : length      = 0pt ,
    subsection-indent        : length      = 1.5em ,
    subsubsection-indent     : length      = 3.8em ,
    paragraph-indent         : length      = 7em ,
    subparagraph-indent      : length      = 10em ,
    figure-indent            : length      = 1.5em ,
    table-indent             : length      = 1.5em ,
    %
    part-num-width           : length      = 0pt ,
    chapter-num-width        : length      = 1.5em ,
    section-num-width        : length      = 1.5em ,
    subsection-num-width     : length      = 2.3em ,
    subsubsection-num-width  : length      = 3.2em ,
    paragraph-num-width      : length      = 4.1em ,
    subparagraph-num-width   : length      = 5em ,
    figure-num-width         : length      = 2.3em ,
    table-num-width          : length      = 2.3em
  }

\DeclareTemplateCode{xtoc}{list-of}{0}
  {
    file-extension           = \@xtoc@fileextension ,
    heading-function         = \@xtoc@headingfunction ,
    heading                  = \@xtoc@heading ,
    header                   = \@xtoc@header ,
    preamble                 = \@xtoc@preamble ,
    preamble-post-space      = \@xtoc@preamblepostspace ,
    postamble                = \@xtoc@postamble ,
    postamble-pre-space      = \@xtoc@postambleprespace ,
    right-margin             = \@xtoc@rightmargin ,
    dot-separation           = \@xtoc@dotseparation ,
    dot-symbol               = \@xtoc@dotsymbol ,
    page-number-width        = \@xtoc@pagenumberwidth ,
    page-number-alignment    =
      {
        left   = \def\@xtoc@pagenumberalign{l} ,
        center = \def\@xtoc@pagenumberalign{c} ,
        right  = \def\@xtoc@pagenumberalign{r}
      } ,
    page-number-formatting   = \@xtoc@pagenumberformatting ,
    depth                    = \@xtoc@leveldepth ,
    %
    top-entry-pre-space      = \@xtoc@topentryprespace ,
    top-entry-formatting     = \@xtoc@topentryformatting ,
    top-entry-dotted         =
      {
        true = \@xtoc@topentrydottedtrue ,
        false = \@xtoc@topentrydottedfalse
      } ,
    top-entry-page-number-right =
      {
        true = \@xtoc@toppagenumberrighttrue ,
        false = \@xtoc@toppagenumberrightfalse
      } ,
    %
    main-entry-pre-space     = \@xtoc@mainentryprespace ,
    main-entry-formatting    = \@xtoc@mainentryformatting ,
    main-entry-dotted        =
      {
        true = \@xtoc@mainentrydottedtrue ,
        false = \@xtoc@mainentrydottedfalse
      } ,
    main-entry-page-number-right =
      {
        true = \@xtoc@mainpagenumberrighttrue ,
        false = \@xtoc@mainpagenumberrightfalse
      } ,
    %
    sub-entry-pre-space      = \@xtoc@subentryprespace ,
    sub-entry-formatting     = \@xtoc@subentryformatting ,
    sub-entry-dotted         =
      {
        true = \@xtoc@subentrydottedtrue ,
        false = \@xtoc@subentrydottedfalse
      } ,
    sub-entry-page-number-right =
      {
        true = \@xtoc@subpagenumberrighttrue ,
        false = \@xtoc@subpagenumberrightfalse
      } ,
    %
    part-entry               = \@xtoc@partentry ,
    chapter-entry            = \@xtoc@chapterentry ,
    section-entry            = \@xtoc@sectionentry ,
    subsection-entry         = \@xtoc@subsectionentry ,
    subsubsection-entry      = \@xtoc@subsubsectionentry ,
    paragraph-entry          = \@xtoc@paragraphentry ,
    subparagraph-entry       = \@xtoc@subparagraphentry ,
    figure-entry             = \@xtoc@figureentry ,
    table-entry              = \@xtoc@tableentry ,
    %
    part-level               = \@xtoc@partlevel ,
    chapter-level            = \@xtoc@chapterlevel ,
    section-level            = \@xtoc@sectionlevel ,
    subsection-level         = \@xtoc@subsectionlevel ,
    subsubsection-level      = \@xtoc@subsubsectionlevel ,
    paragraph-level          = \@xtoc@paragraphlevel ,
    subparagraph-level       = \@xtoc@subparagraphlevel ,
    figure-level             = \@xtoc@figurelevel ,
    table-level              = \@xtoc@tablelevel ,
    %
    part-indent              = \@xtoc@partindent ,
    chapter-indent           = \@xtoc@chapterindent ,
    section-indent           = \@xtoc@sectionindent ,
    subsection-indent        = \@xtoc@subsectionindent ,
    subsubsection-indent     = \@xtoc@subsubsectionindent ,
    paragraph-indent         = \@xtoc@paragraphindent ,
    subparagraph-indent      = \@xtoc@subparagraphindent ,
    figure-indent            = \@xtoc@figureindent ,
    table-indent             = \@xtoc@tableindent ,
    %
    part-num-width           = \@xtoc@partnumwidth ,
    chapter-num-width        = \@xtoc@chapternumwidth ,
    section-num-width        = \@xtoc@sectionnumwidth ,
    subsection-num-width     = \@xtoc@subsectionnumwidth ,
    subsubsection-num-width  = \@xtoc@subsubsectionnumwidth ,
    paragraph-num-width      = \@xtoc@paragraphnumwidth ,
    subparagraph-num-width   = \@xtoc@subparagraphnumwidth ,
    figure-num-width         = \@xtoc@figurenumwidth ,
    table-num-width          = \@xtoc@tablenumwidth
  }
  {%
    \AssignTemplateKeys
    \if@twocolumn
      \@restonecoltrue\onecolumn
    \else
      \@restonecolfalse
    \fi
    \@xtoc@headingfunction{\@xtoc@heading\@mkboth{\@xtoc@header}{\@xtoc@header}}%
    \@xtoc@makel@entry{part}%
    \@xtoc@makel@entry{chapter}%
    \@xtoc@makel@entry{section}%
    \@xtoc@makel@entry{subsection}%
    \@xtoc@makel@entry{subsubsection}%
    \@xtoc@makel@entry{paragraph}%
    \@xtoc@makel@entry{subparagraph}%
    \@xtoc@makel@entry{figure}%
    \ifdefvoid\@xtoc@preamble{}
      {\@xtoc@preamble\par\addvspace{\@xtoc@preamblepostspace}}%
    \@starttoc{\@xtoc@fileextension}%
    \ifdefvoid\@xtoc@postamble{}
      {\par\addvspace{\@xtoc@postambleprespace}\noindent\@xtoc@postamble}%
    \if@restonecol\twocolumn\fi
  }


\DeclareInstance{xtoc}{article-toc}{list-of}{}
\DeclareInstance{xtoc}{article-lof}{list-of}{
  file-extension = lof ,
  heading = \listfigurename ,
  header  = \MakeUppercase\listfigurename
}
\DeclareInstance{xtoc}{article-lot}{list-of}{
  file-extension = lot ,
  heading = \listtablename ,
  header  = \MakeUppercase\listtablename
}

\DeclareInstance{xtoc}{book-toc}{list-of}{
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}
\DeclareInstance{xtoc}{book-lof}{list-of}{
  file-extension = lof ,
  heading = \listfigurename ,
  header  = \MakeUppercase\listfigurename ,
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}
\DeclareInstance{xtoc}{book-lot}{list-of}{
  file-extension = lot ,
  heading = \listtablename ,
  header  = \MakeUppercase\listtablename ,
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}

\DeclareInstance{xtoc}{report-toc}{list-of}{
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}
\DeclareInstance{xtoc}{report-lof}{list-of}{
  file-extension = lof ,
  heading = \listfigurename ,
  header  = \MakeUppercase\listfigurename ,
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}
\DeclareInstance{xtoc}{report-lot}{list-of}{
  file-extension = lot ,
  heading = \listtablename ,
  header  = \MakeUppercase\listtablename ,
  heading-function        = \chapter*{#1} ,
  depth                   = 2 ,
  part-level              = -1 ,
  section-entry           = \xtocsubentry{#1}{#2}{#3}{#4}{#5} ,
  section-indent          = 1.5em ,
  section-num-width       = 2.3em ,
  subsection-indent       = 3.8em ,
  subsection-num-width    = 3.2em ,
  subsubsection-indent    = 7.0em ,
  subsubsection-num-width = 4.1em ,
  paragraph-indent        = 10em ,
  paragraph-num-width     = 5em ,
  subparagraph-indent     = 12em ,
  subparagraph-num-width  = 6em
}

\newrobustcmd*\setxtoc[1]{\EditTemplateDefaults{xtoc}{list-of}{#1}}

\ifbool{@xtoc@noredef}{}{%
  \IfInstanceExistTF{xtoc}{\@currname}{%
    \renewrobustcmd*\tableofcontents{\UseInstance{xtoc}{\@currname-toc}}%
    \renewrobustcmd*\listoffigures  {\UseInstance{xtoc}{\@currname-lof}}%
    \renewrobustcmd*\listoftables   {\UseInstance{xtoc}{\@currname-lot}}%
  }{%
    \renewrobustcmd*\tableofcontents[1][]{\UseTemplate{xtoc}{list-of}{#1}}%
    \renewrobustcmd*\listoffigures[1][]{\UseTemplate{xtoc}{list-of}{
      file-extension = lof ,
      heading = \listfigurename ,
      header  = \MakeUppercase\listfigurename ,
      #1
    }}%
    \renewrobustcmd*\listoftables[1][]{\UseTemplate{xtoc}{list-of}{
      file-extension = lot ,
      heading = \listtablename ,
      header  = \MakeUppercase\listtablename ,
      #1
    }}%
  }%
}

\endinput

评论

除了在这个答案中,我无意发布这个包,也无意进一步开发和维护它。如果有人想利用它并将其变成一个真正的包:请这样做:)

相关内容