使用部分目录时附录前缀不显示在目录中

使用部分目录时附录前缀不显示在目录中

我尝试使用此解决方案在我的文档中创建部分目录,但当我添加附录时,它改变了附录包的行为。尽管添加了\usepackage[page,toc,titletoc,title]{appendix},但“附录”和“附录 A”并未显示在目录中。

建议的解决方案没有提供所需的输出,因为我希望附录显示在部分目录中,而不是一般目录中。

我认为问题与\tocbasic@addxcontentsline或有关\listoftoc{\ext@parttoc}。 .aux 文件确实区分了 .toc 和 .tcpI,但它区分了 和\contentsline {chapter}{Appendices}{9}{}\protected@file@percent\contentsline {chapter}{Appendix \numberline {A}Chapter appendix}{11}{}\protected@file@percent导致第一个在 .tcpI 中没有条目,并且它不会显示\numberline第二个中之前的内容。

有什么办法可以改变这种行为吗?TIA。

平均能量损失

\documentclass{scrbook}
\usepackage[page,toc,titletoc,title]{appendix}
\usepackage{xpatch}

\usepackage{blindtext}

% Partial TOC
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part
\makeatother

\setcounter{tocdepth}{\partnumdepth}% depth of TOC


\begin{document}
    \tableofcontents
    \part{PART 1}
    \parttoc
    \chapter{Something}
    \blindtext
    \begin{appendices}
        \chapter{Chapter appendix}
        \blindtext
    \end{appendices}
    \chapter{Non appendix chapter}
\end{document}

经过一番研究,我发现另一个解决方案它从不同的角度处理同一个问题。尽管我试图理解代码,但我未能做出适当的改变,使它作为每个的部分目录工作\part。这是另一个平均能量损失

\documentclass{scrbook}
\usepackage[page,toc,titletoc,title]{appendix}
\usepackage{etoolbox}
\usepackage{catchfile}
\usepackage{blindtext}

% Works with or without hyperref.
\usepackage{hyperref}

\makeatletter
  % Macros for resetting and appending to the variables that store the part tocs.
  \def\@parttoc@toc@reset{%
    \expandafter\gdef\csname @parttoc@toc@\@parttoc@part@num\endcsname{}%
  }
  \def\@parttoc@toc@append{%
    \expandafter\gappto\csname @parttoc@toc@\@parttoc@part@num\endcsname
  }
  % The parsing macro, that goes through the toc file and creates the part toc macros.
  \def\@parttoc@parse@tocfile{%
    % We only need to do this once.
    \let\@part@parse@tocfile\relax
    \begingroup
      \def\@parttoc@part@num{0}%
      \@parttoc@toc@reset
      \makeatletter
      \IfFileExists{\jobname.toc}{%
        \CatchFileDef\@tmpa{\jobname.toc}{}%
        \expandafter\@parttoc@parse@head
        \@tmpa
        \contentsline\@parttoc@parse@stop%
      }{}%
    \endgroup
  }
  \def\@parttoc@part@string{part}
  \def\@parttoc@parse@stop{\@parttoc@parse@stop}%
  \long\def\@parttoc@parse@head#1\contentsline#2{%
    \@parttoc@toc@append{#1}%
    \ifx\@parttoc@parse@stop#2%
      \expandafter\@gobble
    \else
      \begingroup
      \def\@tmpa{#2}%
      \ifx\@tmpa\@parttoc@part@string
        \endgroup
        \expandafter\expandafter
        \expandafter\@parttoc@parse@cline@part
      \else
        \endgroup
        \expandafter\expandafter
        \expandafter\@parttoc@parse@cline@other
      \fi
    \fi
    {#2}%
  }
  \def\@parttoc@parse@cline@other{%
    \@parttoc@toc@append{\contentsline}%
    \@parttoc@parse@head
  }
  \def\@parttoc@parse@cline@part#1#2#3{%
    \begingroup
      \def\numberline##1##2\@nil{%
        \endgroup
        \def\@parttoc@part@num{##1}%
      }%
    #2\@nil
    \@parttoc@toc@reset
    \expandafter\@parttoc@parse@head\@parttoc@parse@gobble@hyperrefargs
  }
  % A macro for printing the current part toc.
  \def\@parttoc@toc@print{%
    \begingroup
      \setcounter{tocdepth}{\partocdepth}%
      \parindent 0pt%
      \chapter*{\contentsname}%
      \csname @parttoc@toc@\arabic{part}\endcsname
      \@parttoc@toc@afterhook
    \endgroup
  }
  \newcommand*\partocdepth{3}
  \def\@parttoc@toc@afterhook{}
  \newcommand\parttocafter[1]{%
    \def\@parttoc@toc@afterhook{#1}%
  }
  % Set everything up.
  \AtBeginDocument{%
    % When hyperref is loaded, \contentsline has one more argument.
    \@ifpackageloaded{hyperref}{%
      \protected@write\@auxout{}{\let\protect\@parttoc@parse@gobble@hyperrefargs\protect\@gobble}%
    }{%
      \protected@write\@auxout{}{\let\protect\@parttoc@parse@gobble@hyperrefargs\protect\@empty}%
    }%
    % When the aux file is read in the first time, the toc file will be parsed.
    \protected@write\@auxout{}{%
        \protect\@parttoc@parse@tocfile
    }%
    % Automatically print the part toc after each part title.
    \apptocmd\scr@@startpart{\@parttoc@toc@print}{}{}%
  }
\makeatother

\begin{document}
\tableofcontents
\part{PART 1}
\chapter{Something}
\blindtext
\begin{appendices}
  \chapter{Chapter appendix}
  \blindtext
\end{appendices}
\chapter{Non appendix chapter}
\end{document}

答案1

重新定义\addappheadtotoc(由包提供appendix)以获取部分目录中的条目»附录«:

\renewcommand{\addappheadtotoc}{% original definition see package appendix
  \phantomsection
  \addchaptertocentry{}{\appendixtocname}%
}

对于目录中的前缀 »Appendix«,在环境\addchaptertocentry内重新定义 KOMA-Script 命令appendices

\xapptocmd{\appendices}{\useappendixprefixintoc}{}{}
\newcommand{\useappendixprefixintoc}{
  \let\originaladdchaptertocentry\addchaptertocentry
  \renewcommand\addchaptertocentry[2]{%
    \IfArgIsEmpty{##1}
      {\originaladdchaptertocentry{##1}{##2}}
      {\originaladdchaptertocentry{}{\appendixname~##1\protect\autodot\ ##2}}
  }%
}

例子:

\documentclass{scrbook}
\usepackage[page,toc]{appendix}
\usepackage{xpatch}

\usepackage{blindtext}

% Partial TOC
\makeatletter
\newif\ifuseparttoc
\newcommand*{\parttoc}[1][\thepart]{% new command to generate and show a chapter toc
\useparttoctrue% switch on part-toc-entries
\edef\ext@parttoc{tcp#1}% extension of the part-toc-file, e.g., tcpI
\DeclareNewTOC[
    listname=My Content,
    %unset=onecolumn% if the part toc should use twocolumn
]{\ext@parttoc}% declare a new toc file
\begingroup
    \value{tocdepth}=\chaptertocdepth% we want entries down to chapter
    \listoftoc{\ext@parttoc}% show the toc with header
\endgroup
}
\xapptocmd\addtocentrydefault{% patch the KOMA-Script's generic toc entry generator
\ifuseparttoc% if part toc entries should be generated
    \expandafter\tocbasic@addxcontentsline\expandafter{\ext@parttoc}{#1}{#2}{#3}% do it
\fi
}{}{}
\xpretocmd\part{\useparttocfalse}{}{}% automatically switch of part toc entries at start of every \part
\makeatother

\setcounter{tocdepth}{\partnumdepth}% depth of TOC


\renewcommand{\addappheadtotoc}{% original definition see package appendix
  \phantomsection
  \addchaptertocentry{}{\appendixtocname}%
}
\xapptocmd{\appendices}{\useappendixprefixintoc}{}{}
\newcommand{\useappendixprefixintoc}{
  \let\originaladdchaptertocentry\addchaptertocentry
  \renewcommand\addchaptertocentry[2]{%
    \IfArgIsEmpty{##1}
      {\originaladdchaptertocentry{##1}{##2}}
      {\originaladdchaptertocentry{}{\appendixname~##1\protect\autodot\ ##2}}
  }%
}

\begin{document}
\tableofcontents
\part{PART 1}
\parttoc
\chapter{Something}
\blindtext
\begin{appendices}
  \chapter{Chapter appendix}
  \blindtext
\end{appendices}
\chapter{Non appendix chapter}
\end{document}

在此处输入图片描述

相关内容