1

I am adapting to LaTeX3 the code given there to provide for an environment the content of which is not displayed but the labels of which are taken into account.

The idea is to typeset the content into a vbox to be discarded, but before that, to change the meaning of \label to a new \qlabel command that stores label information into a clist, and thereafter to process the clist with \dolabels to output relevant label information into .aux.

This seems to work… until amsmath is loaded, whether before or after the code. What happens is the reference to the non-typeset labels are undefined. I have reason to believe that amsmath somehow makes it difficult to change the meaning of \label (the clist remains empty), but I am at a complete loss as to how to eschew this.

Here is my code. Compiling (with LuaLaTeX) as is correctly gives references (2) (1) (4) at the end. Uncommenting one of the \usepackage{amsmath} gives (??) (1) (4) instead.

\documentclass{article}

%\usepackage{amsmath} \ExplSyntaxOn \makeatletter

\clist_new:N \g__chato_exo_labels_cl \box_new:N \l__chato_exo_dummy_box

\cs_new:Nn\cs__chato_exo_dolabels:nnn{@bsphack \protected@write @auxout {}{\string \newlabel {#1}{{#2}{#3}}}@esphack} \cs_new:Nn\cs__chato_exo_qlabel:n{\clist_gput_right:Nx \g__chato_exo_labels_cl { {{#1}{@currentlabel}{\thepage}} } }

\NewDocumentEnvironment{question}{O{}} {\begingroup\let\ltx@label\cs__chato_exo_qlabel:n\vbox_set:Nw \l__chato_exo_dummy_box} {\vbox_set_end:\endgroup\clist_map_inline:Nn \g__chato_exo_labels_cl {\cs__chato_exo_dolabels:nnn ##1}}

\makeatother \ExplSyntaxOff

%\usepackage{amsmath}

\begin{document}

\begin{equation} i,i,i, \label{eq2} \end{equation}

\begin{question}[truc] choucroute \begin{equation} i,i,i, \label{eq1} \end{equation}

\begin{equation} oon \label{eq} \end{equation} \end{question}

\begin{equation} i,i,i, \label{eq3} \end{equation}

(\ref{eq1}) (\ref{eq2}) (\ref{eq3}) \end{document}

ysalmon
  • 1,160
  • I don't get a correct result with your code, \ltx@label is not defined and so never executed. With amsmath the code work ok but will probably break if you load hyperref. – Ulrike Fischer Aug 29 '22 at 10:12
  • Oh, maybe I need to use \label when amsmath is not loaded and \ltx@label when it is ? – ysalmon Aug 29 '22 at 12:46

1 Answers1

3

Thanks to Ulrike Fischer for pointing out the \ltx@label thing. It is the original \label when amsmath is loaded -- I had been fumbling so much with all of this that I had lost track of that.

Thanks for indicating a problem with hyperref. I got it to compile by taking the meaning it gives to \label and storing the relevant information. I did not check whether the links actually work -- I of course do not expect links to non-typeset parts to.

EDIT : amsmath messes with \label not globally (upon package loading), but locally when a math environment is started. So if we only change the meaning of \ltx@label, we get equations to work, but not captions of figures (because they use the original \label).

So we need to change both :

\ExplSyntaxOn
\makeatletter

\clist_new:N \g__chato_exo_labels_cl \box_new:N \l__chato_exo_dummy_box

@ifpackageloaded{hyperref} { \cs_new:Npn\cs__chato_exo_dolabels#1#2#3#4#5{@bsphack \begingroup \def \label@name {#1}\label@hook \protected@write @auxout {}{\string \newlabel {#1}{{#2}{#3}{#4}{#5}{}}}\endgroup @esphack} \cs_new:Nn\cs__chato_exo_qlabel:n{\clist_gput_right:Nx \g__chato_exo_labels_cl { {{#1}{@currentlabel}{\thepage}{@currentlabelname}{@currentHref}} } } } { \cs_new:Npn\cs__chato_exo_dolabels#1#2#3{@bsphack \protected@write @auxout {}{\string \newlabel {#1}{{#2}{#3}}}@esphack} \cs_new:Nn\cs__chato_exo_qlabel:n{\clist_gput_right:Nx \g__chato_exo_labels_cl { {{#1}{@currentlabel}{\thepage}} } } }

\NewDocumentEnvironment{question}{O{}} {\group_begin:\cs_set_eq:NN\label\cs__chato_exo_qlabel:n @ifpackageloaded{amsmath}{\cs_set_eq:NN\ltx@label\cs__chato_exo_qlabel:n}{}\vbox_set:Nw \l__chato_exo_dummy_box} {\vbox_set_end:\group_end:\clist_map_inline:Nn \g__chato_exo_labels_cl {\cs__chato_exo_dolabels ##1}}

\makeatother \ExplSyntaxOff

ysalmon
  • 1,160