
這個問題對我來說似乎很困惑,所以我不確定你是否能夠理解它。
我知道如何解決這個問題(現在)。我想知道的是我應該如何診斷並解決它。解決這個問題花了我大約 24 小時(誠然,這個問題不是我唯一的工作)。考慮到我相對容易地隔離問題並且解決方案的記錄相對充分,這似乎太長了。花時間弄清楚什麼在哪裡——或者,也許更準確地說,什麼時候把它說出來。
我的實際程式碼相當複雜,但是,更大的複雜性並沒有真正使發現問題變得更加困難。
基本問題是需要產生一個或多個術語表,但無法glossaries
在頂層建立標題、目錄條目和分頁符號。但我確實想glossaries
為子詞彙表建立標題,其中兩組或多組詞彙表條目被分組在一個主標題下。
我很快就發現:列印沒有標題的詞彙表和列印沒有標題的詞彙表。 [考慮到我使用了 SE 的搜索,速度非常快。
所以我花了接下來的 23 個小時試圖找出在哪裡/何時進行幹預。
回想起來,事情變得複雜的是我試圖從課堂上而不是文件本身進行幹預。看起來使用哪個鉤子是一個微妙的問題,即使你可以\begin{document}
在文件中直接進行更改之前或之後進行更改。
所做的改變begindocument
似乎有效;國產的則begindocument/after
不然。先前\printglossary
在課堂上所做的更改確實有效,但僅限於本地。嘗試從類別更改部分命令似乎在begindocument
除了非常非常接近之後失敗\printglossary
。但也cmd/printglossary/before
不起作用。
因此,在介入方面,很少有地方能發揮作用。我應該如何找到這些特定的地方?特別是,我如何確定我需要做出改變早些時候或僅很久以後而不是像我預期的那樣稍後?
\begin{filecontents}[overwrite]{\jobname-g1.tex}
\ProvidesFile{\jobname-g1.tex}
\newglossaryentry{a}{name=a,description=A,type=ga}
\newglossaryentry{b}{name=b,description=B,type=gb}
\newglossaryentry{c}{name=c,description=C,type=gc}
\newglossaryentry{m}{name=m,description=M}
\end{filecontents}
\documentclass{book}
\ExplSyntaxOn
% the next line doesn't work
\hook_gput_code:nnn { begindocument / after } { . / my glossaries }
% the next line does work (begindocument/before works, too)
% \hook_gput_code:nnn { begindocument } { . / glossaries }
% the following line also works
% \hook_gput_code:nnn { package/glossaries/after } { . }
{
\setglossarysection {subsection}
\renewcommand* \glsclearpage {}
}
\ExplSyntaxOff
\NewDocumentCommand \mything {+m}
{%
\cleardoublepage
{\parindent=0pt
\hrule\par
{\Huge #1\par}%
\hrule\par
}%
}
\NewDocumentCommand \mywords {ommo}
{% intervening up here somewhere doesn't work
\mything{#3}%
\addcontentsline{toc}{section}{#3}%
\IfValueTF{#4}{%
% intervening here works, but { cmd/printglossary/before } doesn't
\printglossary[title={#2},type=#2]%
\printglossary[title={#4},type=#4]%
}{%
\printglossary[title={},type=#2]%
}
}
\usepackage[nonumberlist,subentrycounter,toc,xindy]{glossaries}
\newglossary[ga-glg]{ga}{ga-gls}{ga-glo}{As}
\newglossary[gb-glg]{gb}{gb-gls}{gb-glo}{Bs}
\newglossary[gc-glg]{gc}{gc-gls}{gc-glo}{Cs}
\setglossarystyle{altlist}
\loadglsentries{\jobname-g1}
\makeglossaries
% Uncommenting the following line works
% \setglossarysection{subsection}
\begin{document}
% uncommenting this line works
% \setglossarysection{subsection}
\tableofcontents
\mything{T}
\gls{a} \gls{b} \gls{c} \gls{m}
\mywords[b]{ga}{A-B-Words}[gb]
\mywords{gc}{C-Words}
\mywords{main}{M-Words}
\end{document}
例如,我可以查看begindocument
鉤子的內容:
-> The hook 'begindocument/after':
> The hook is not declared.
> Code chunks:
> top-level/myglossaries -> \setglossarysection {subsection}\renewcommand *
\glsclearpage {}
> Document-level (top-level) code:
> ---
> Extra code for next invocation:
> ---
> Rules:
> ---
> Execution order:
> Not set because the hook is undeclared.
<recently read> }
l.895 \hook_show:n { begindocument/after }
?
-> The hook 'begindocument':
> Code chunks:
> amsmath -> \reset@strutbox@ \MakeRobust \dddot \MakeRobust \ddddot \mathc
hardef \std@minus \mathcode `\-\relax \mathchardef \std@equal \mathcode `\=\rel
ax \expandafter \let \csname overleftarrow \endcsname \@undefined \expandafter
\let \csname overrightarrow \endcsname \@undefined \MakeRobust \overrightarrow
\MakeRobust \overleftarrow \MakeRobust \overleftrightarrow \MakeRobust \underri
ghtarrow \MakeRobust \underleftarrow \MakeRobust \underleftrightarrow
> datatool-base -> \@ifundefined {texteuro}{}{\DTLnewcurrencysymbol {\texte
uro }}\@ifundefined {textdollar}{}{\DTLnewcurrencysymbol {\textdollar }}\@ifund
efined {textstirling}{}{\DTLnewcurrencysymbol {\textstirling }}\@ifundefined {t
extyen}{}{\DTLnewcurrencysymbol {\textyen }}\@ifundefined {textwon}{}{\DTLnewcu
rrencysymbol {\textwon }}\@ifundefined {textcurrency}{}{\DTLnewcurrencysymbol {
\textcurrency }}\@ifundefined {euro}{}{\DTLnewcurrencysymbol {\euro }}\@ifundef
ined {yen}{}{\DTLnewcurrencysymbol {\yen }}
> glossaries -> \ifglsacronym \providecommand *{\printacronyms }[1][]{\prin
tglossary [type=\acronymtype ,##1]}\fi \ifx \gls@codepage \@empty \@ifpackagelo
aded {fontspec}{\def \gls@codepage {utf8}}{}\fi \gls@begindocdefs \@ifpackagelo
aded {amsmath}{\let \gls@if@measuring \@gls@if@measuring }{}\let \gls@org@refst
epcounter \refstepcounter \let \gls@org@label \label \renewcommand *{\@gls@adju
stmode }{\ifvmode \mbox {}\fi }
> Document-level (top-level) code (executed last):
> ---
> Extra code for next invocation:
> ---
> Rules:
> ---
> Execution order:
> amsmath, datatool-base, glossaries.
<recently read> }
l.896 \hook_show:n { begindocument }
?
-> The hook 'begindocument/before':
> The hook is empty.
<recently read> }
l.897 \hook_show:n { begindocument/before }
?
但這似乎證實了我的程式碼begindocument/after
在完成任何預設設定後正在運行。這表明它應該等同於使用 after 文件中的程式碼\begin{document}
,但後者有效,而前者失敗。
那麼,當鉤子不包含其他可能幹擾的程式碼時,在begindocument/after
鉤子中運行的程式碼和之後運行的程式碼有什麼不同呢?\begin{document}
為什麼代碼需要放入早些時候鉤子,即使插入文件本身時間並不重要?