Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

\AddToHook unexpected side effect #1591

Open
jlaurens opened this issue Dec 22, 2024 · 4 comments
Open

\AddToHook unexpected side effect #1591

jlaurens opened this issue Dec 22, 2024 · 4 comments
Assignees
Labels
bug category base (latex) fixed in dev Fixed in development branch, not in stable release

Comments

@jlaurens
Copy link
Contributor

Brief outline of the bug

Assume \FOO is not defined.
After \AddToHook{cmd/FOO/before}{} \FOO is \relax and no longer breaks when used.

This should not be considered a feature.

Minimal example showing the bug

\RequirePackage{latexbug}       % <--should be always the first line (see CONTRIBUTING)!
\documentclass{article}
\AddToHook{cmd/FOO/before}{}
\begin{document}
\show\FOO
\end{document}

Log file (required) and possibly PDF file

This is pdfTeX, Version 3.141592653-2.6-1.40.26 (TeX Live 2024) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./trashme.tex
LaTeX2e <2024-06-01> patch level 1
L3 programming layer <2024-05-27>
(/usr/local/texlive/2024/texmf-dist/tex/latex/latexbug/latexbug.sty)
(/usr/local/texlive/2024/texmf-dist/tex/latex/base/article.cls
Document Class: article 2024/02/08 v1.4n Standard LaTeX document class
(/usr/local/texlive/2024/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2024/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
(./trashme.aux)
> \FOO=\relax.
l.7 \show\FOO
             
? 
@muzimuzhi
Copy link
Contributor

muzimuzhi commented Dec 22, 2024

The \relax assignment happens at the c-type expansion of \exp_args:Nc \__hook_patch_cmd_or_delay:Nnn inside \__hook_try_put_cmd_hook:w:

latex2e/base/ltcmdhooks.dtx

Lines 584 to 589 in 7d4ec54

\cs_new_protected:Npn \@@_try_put_cmd_hook:w
#1 / #2 / #3 / #4 \s_@@_mark #5
{
\@@_debug:n { \iow_term:n { ->~Adding~cmd~hook~to~'#2'~(#3): } }
\exp_args:Nc \@@_patch_cmd_or_delay:Nnn {#2} {#2} {#3}
}

Luckyly, as \__hook_patch_cmd_or_delay:Nnn only do global assignment(s), wrapping it in an extra group does the trick:

\RequirePackage{latexbug}       % <--should be always the first line (see CONTRIBUTING)!
\documentclass{article}

\ExplSyntaxOn
\cs_undefine:N \__hook_try_put_cmd_hook:w
\cs_new_protected:Npn \__hook_try_put_cmd_hook:w
    #1 / #2 / #3 / #4 \s__hook_mark #5
  {
    \__hook_debug:n { \iow_term:n { ->~Adding~cmd~hook~to~'#2'~(#3): } }
    \group_begin: % <<< added
    \exp_args:Nc \__hook_patch_cmd_or_delay:Nnn {#2} {#2} {#3}
    \group_end: % <<< added
  }
\ExplSyntaxOff

\AddToHook{cmd/FOO/before}{}

\begin{document}
\show\FOO
\end{document}

@FrankMittelbach
Copy link
Member

@muzimuzhi I was thinking along the same lines, but I don't like to introduce a dependency of relying on \__hook_patch_cmd_or_delay:Nnn using only gloal assignments, so I do it slightly differently. The alternative is to fully take out the first argument and generate it later (but that requires more surgery.

@Rimole
Copy link

Rimole commented Dec 22, 2024

@FrankMittelbach

ef35caf#diff-a37b1fd2a3d174eb379905a10f50d81a694fa77b3f8252390a53076ff6bb7933

base/doc/ltnews41.tex line 378:

it no longer raised and \enquote{Undefined csname} error but silently

Replace and by an , please.

@FrankMittelbach FrankMittelbach moved this from Pool (unscheduled issues) to Done in dev in upcoming LaTeX2e releases Dec 22, 2024
@FrankMittelbach
Copy link
Member

already done a few hours ago 76ecf9d

josephwright pushed a commit that referenced this issue Dec 24, 2024
* fix for #1591

* fix typo

* Apply suggestions from code review

Co-authored-by: Yukai Chou <[email protected]>

---------

Co-authored-by: Yukai Chou <[email protected]>
@FrankMittelbach FrankMittelbach added fixed in branch fixed in dev Fixed in development branch, not in stable release and removed fixed in branch labels Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category base (latex) fixed in dev Fixed in development branch, not in stable release
Projects
Status: Done in dev
Development

No branches or pull requests

4 participants