防止 \addcontentsline 中自动 Hy@writebookmark

防止 \addcontentsline 中自动 Hy@writebookmark

在处理我的问题和解决方案(尚未发布;-)包的过程中,我遇到了一些问题,这些问题与文档的分段有关,比如,一本包含章节/节/小节等的书。

因此,在我寻找解决方案的过程中,有一个克隆将带有标题等的分段命令保存到外部文件并在编译运行结束时通过 读取它\input

也就是说,\chapter命令必须重新定义为这样,它将首先生成普通章节,然后通过\immediate\write\LaTeXStandardChapter(请参阅 MWE 了解定义)生成克隆章节。只要延迟的章节积累了章节编号,就没有问题。使用包\addcontentsline时,将它们很好地添加到目录中并生成书签。hyperref

好吧,我不想有累积的章节编号,我宁愿重新开始,例如

问题部分:第一章 ----> 第一节第一部分

克隆部分:克隆章节第一 --> 克隆节第一的第一

等等,即在读取克隆的内容文件之前必须重置章节号。

现在该\LaTeXStandardChapter{...}命令将添加内容行 --> 这样就很好了。

但是书签是错误的,因为它们是从章节号 1 等创建的,出现了两次 --> 单击书签将转到错误的位置!

显然,我必须禁用自动生成书签的功能\addcontentsline,即将其放入\Hy@writebookmark一个组中并“手动”添加书签。

所有这些都有效,但有没有更好的方法呢?

这是我的代码:(更大框架的一部分,仅显示或缩短相关部分以便快速使用)

\documentclass[12pt,paper=a4]{book}


\RequirePackage{etoolbox}%
\RequirePackage{xcolor}%
\RequirePackage{morewrites}
\RequirePackage[bookmarksopen=true,bookmarksopenlevel=2]{hyperref}%
\RequirePackage{bookmark}%


\newwrite\CloneChapter%

\let\LaTeXStandardChapter\chapter%
\makeatletter
\newrobustcmd{\unstarredchapter@noopt}[1]{%
\unstarredchapter[#1]{#1}%  Call the version with the optional first arg%
}% End of \unstarredchapter@noopt



\newrobustcmd{\unstarredchapter@opt}[2][]{%
\LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone%
\immediate\write\CloneChapter{%
\string\LaTeXStandardChapter[]{\unexpanded{Explanation -- #2}}%  Prevent an entry to the toc from the cloned chapter
\string\phantomsection%
\string\begingroup
\string\renewcommand{\unexpanded{\Hy@writebookmark}}[5]{}%   Drop this "wrong" bookmark
\string\addcontentsline{toc}{chapter}{\unexpanded{\protect\numberline{\thechapter}}{\unexpanded{Explanation -- #1}}}%
\string\endgroup
\string\pdfbookmark[0,color=red]{\unexpanded{Explanation #1}}{cloned::chapter::bookmark::\thechapter}% Manually write the bookmark
}% End of \immediate\write
}% End of \unstarredchapter@opt


% 'Rewrite' the starred chapter, to cool down `\tableofcontents` ;-)
\newrobustcmd{\starredchapter}[1]{%
\LaTeXStandardChapter*{#1}%
}%

\newrobustcmd{\unstarredchapter}{%
\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}
}%


\renewcommand{\chapter}{%
\@ifstar{\starredchapter}{\unstarredchapter}%
}%



\makeatother

\AtBeginDocument{%
\immediate\openout\CloneChapter=\jobname.clone%
\immediate\write\CloneChapter{%
  \string\makeatletter%
  \string\setcounter{chapter}{0}%
}%
}%

\AtEndDocument{%
\immediate\closeout\CloneChapter%
}%

\newrobustcmd{\CollectClonedChapter}{%
\part{Explanations}%
\setcounter{chapter}{0}%
\IfFileExists{\jobname.clone}{\input{\jobname.clone}}{%
}%
}%


\newcounter{somecounter}[chapter]

\newrobustcmd{\MyCommand}[2]{%
\refstepcounter{somecounter}%
\textbf{Example \thesomecounter:}

\noindent#1
\hrule%
\immediate\write\CloneChapter{%
\expandafter\string\unexpanded{\textbf{Explanation to Example \number\value{chapter}.\number\value{somecounter}}}

}%
\immediate\write\CloneChapter{%
\noindent
}%
\immediate\write\CloneChapter{%
\expandafter\string\unexpanded{#2}%
}%
}


\begin{document}
\tableofcontents

\part{Questions}%

\chapter{Original Chapter Number One}

\MyCommand{Foo}{Well, this was foo}%


\chapter[Short Original 2]{Original Chapter Number Two}

\MyCommand{And now for something completely different}{Who used that line very often?}

\CollectClonedChapter%

\end{document}

在此处输入图片描述

编辑

重复写\renewcommand{\Hy@writebookmark}有点过分了——在我原来的框架中,我包裹了\addcontentsline另一个执行重新定义的命令。为了简单起见,我在这里省略了它。

显然这个问题没有答案:-(

答案1

这是一种完全不同的方法,但仍然采用代码的一般结构。

一句话:我不知道包裹更多写作并决定不使用它。事实上,它改变了我对打开和关闭文件流的工作方式的期望TeX。它似乎允许在尚未关闭的文件上使用读取流。

由于彩色书签并非在所有 pdf 查看器中都呈现,因此我例外地使用了Acrobat Reader此答案。

\documentclass[12pt,paper=a4]{scrbook}

\RequirePackage{etoolbox}
\RequirePackage{xcolor}
%% \RequirePackage{morewrites}%% modifies our habits with respect to opened
%% and closed file streams. Too unsettling for a (in the process of becoming
%% old) guy.
\RequirePackage[bookmarksopen=true,bookmarksopenlevel=2]{hyperref}
\RequirePackage{bookmark}


\newwrite\CloneChapter

\let\LaTeXStandardChapter\chapter

\makeatletter

\renewcommand{\chapter}{\@ifstar{\starredchapter}{\unstarredchapter}}%

\newrobustcmd{\unstarredchapter}
             {\@ifnextchar[{\unstarredchapter@opt}{\unstarredchapter@noopt}}

\newrobustcmd{\unstarredchapter@noopt}[1]{%
     \unstarredchapter[#1]{#1}%  Call the version with the optional first arg%
}

\newrobustcmd{\unstarredchapter@opt}[2][]{%
    \LaTeXStandardChapter[#1]{#2}% Normal Chapter, the source of the clone
    \immediate\write\CloneChapter 
      {\string\LaTeXStandardChapter[Explanation -- #1]{Explanation -- #2}}%
}

\newrobustcmd{\starredchapter}[1]{\LaTeXStandardChapter*{#1}}%

\makeatother

\AtBeginDocument{\immediate\openout\CloneChapter=\jobname.clone }

% without morewrites, we can not wait EndDocument to close the file
% and then reopen it
% \AtEndDocument {\immediate\closeout\CloneChapter }%

\newrobustcmd{\CollectClonedChapter}{%
    \immediate\closeout\CloneChapter 
    \part{Explanations}
    \edef \ChapterCount {\arabic{chapter}}%
    \renewcommand \thechapter {\the\numexpr\value{chapter}-\ChapterCount\relax}%
    \bookmarksetup{color=red}%
    \makeatletter
    \IfFileExists{\jobname.clone}{\input{\jobname.clone}}
                                 {CLONE FILE WAS SWALLOWED SOMEWHERE!}%
    \makeatother
}%


\newcounter{somecounter}[chapter]

\newrobustcmd{\MyCommand}[2]{%
    \refstepcounter{somecounter}%
    \textbf{Example \thesomecounter:}

    \noindent #1
    \hrule
    \immediate\write\CloneChapter {%
        \string\textbf{Explanation to Example 
                        \arabic{chapter}.\arabic{somecounter}}

        \noindent #2
    }% end of \write
}


\begin{document}
\bookmark[named=FirstPage]{Title page}

\tableofcontents

\part{Questions}

\chapter[Short 1]{Original Chapter Number One}

\MyCommand{Foo}{Well, this was foo}

\chapter[Short 2]{Original Chapter Number Two}

\MyCommand{And now for something completely different}
          {Who used that line very often?} 


\CollectClonedChapter

\end{document}

胡普弗克隆

相关内容