Git Product home page Git Product logo

Comments (5)

xkwxdyy avatar xkwxdyy commented on August 29, 2024

尝试

我试了一下,如果这个 hook 和 \RequirePackage{hyperref} 在同一个文件中是可以的:

\documentclass{ctexart}
\usepackage{etoolbox}
\AddToHook{package/hyperref/after}{\hypersetup{hidelinks}}
\AtEndPreamble
  {
    \RequirePackage{hyperref}
  }

\begin{document}

\tableofcontents

\section{test}
test

\end{document}

但是我在项目中处理的时候发现失效了。主要是我的 hook 是在一个库里面设置的,然后通过 cus 加载库,而 \RequirePackage{hyperref} cls 文件中

from cus.

xkwxdyy avatar xkwxdyy commented on August 29, 2024
\AtEndPreamble
  {
    \AddToHook { package / hyperref / after }
      { \hypersetup { allcolors = black } }
    \RequirePackage { hyperref }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
  }

行不通,而

\AtEndPreamble
  {
    \RequirePackage { hyperref }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
    \AddToHook { package / hyperref / after }
      { \hypersetup { allcolors = black } }
  }

可以。应该是因为前者相当于

\AtEndPreamble
  {
    \RequirePackage { hyperref }
    \hypersetup { allcolors = black }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
  }

导致前面的 black 被覆盖了。这么看的话,即使能把 \hypersetup { allcolors = black } 延迟到 hyperref 后面加载,好像我本身的问题也没解决。

为了防止出现 X-Y 问题,我说一下我最初的需求:

\AtEndPreamble
  {
    \RequirePackage { hyperref }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
  }

是默认的配置,然后在文档是 for-printfor-library 的时候,需要把颜色设置为 black。然后这个设置是放在了另一个库里。
这么看的话好像只能把 \RequirePackage { hyperref } 和设置放在库里处理了。

from cus.

xkwxdyy avatar xkwxdyy commented on August 29, 2024

目前我想到的唯一办法,就是把 hyperref 的加载放在库里面进行

% -------- %
% 超链接设置 %
% -------- %
\AtEndPreamble
  {
    \RequirePackage { hyperref }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
  }



% ----------------- %
% 不同文档类型下的设置 %
% ----------------- %
\cs_set:Nn \__whu_type_for_print_additional_setting: 
  {
    \AtEndPreamble { \hypersetup { allcolors = black } }
  }
\cs_set:Nn \__whu_type_for_library_additional_setting: 
  {
    \AtEndPreamble { \hypersetup { allcolors = black } }
  }

然后由于 cls

\seq_put_right:Nx \l_file_search_path_seq { ./module }
\seq_put_right:Nx \l_file_search_path_seq { ./library }
\seq_put_right:Nx \l_file_search_path_seq { ./library-cus }
\RequirePackage { whu }

\PassOptionsToClass{ \g__whu_to_book_clist }{ book }
\LoadClass { book }

\str_case:Vn \g__whu_type_str
  {
    { electronic } { \__whu_type_electronic_setting: }
    { for-print } { \__whu_type_for_print_setting: }
    { for-library } { \__whu_type_for_library_setting: }
  }

\str_case:Vn \g__whu_type_str
  {
    { electronic } { \__whu_type_electronic_setting: }
    { for-print } { \__whu_type_for_print_setting: }
    { for-library } { \__whu_type_for_library_setting: }
  }

是在宏包加载之后的,也就会在库调用之后,所以

\cs_set:Nn \__whu_type_for_print_additional_setting: 
  {
    \AtEndPreamble { \hypersetup { allcolors = black } }
  }
\cs_set:Nn \__whu_type_for_library_additional_setting: 
  {
    \AtEndPreamble { \hypersetup { allcolors = black } }
  }

部分的 \AtEndPreamble 会在

\AtEndPreamble
  {
    \RequirePackage { hyperref }
    \hypersetup
      { 
        pdfencoding = auto,
        citecolor   = magenta,
        linkcolor   = blue,
        hidelinks
      }
  }

的后面,从而生效。

from cus.

Sophanatprime avatar Sophanatprime commented on August 29, 2024

\begin{document} 除了会执行通用的环境钩子:env/document/before env/document/begin 还会执行 begindocument/before begindocument begindocument/end 这三个钩子。执行顺序就是这里列出的顺序。
\AtEndPreamble 使用的是 begindocument/before\AtBeginDocument 用的是 begindocument
可以用的钩子有很多,感觉 cus 可做的不多了。

比如,这个问题就可以用 \AtBeginDocument 解决。

from cus.

xkwxdyy avatar xkwxdyy commented on August 29, 2024

好的,感谢!用 \AtBeginDocument 解决了

from cus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.