TeX - LaTeX Asked by Eddy on January 16, 2021
I would like my reference to a set of subequations to automatically contain all the letters of the relecent subequations. See example code below. I am imagining that this will involve a macro labelsubeqn
to replace the labels in the example code, and perhaps take a second input argument indicating the number of subequations present.
documentclass{article}
usepackage{amsmath}
begin{document}
begin{subequations} label{eqn1}
begin{align}
a&=b \
c&=d
end{align}
end{subequations}
begin{subequations} label{eqn2}
begin{align}
a&=b \
c&=d \
e&=f
end{align}
end{subequations}
The reference `eqref{eqn1}' I would like to appear as `(1a,b)'
and the reference `eqref{eqn2}' I would like to appear as `(2a-c)'.
end{document}
I don't think it's good, but…
Why not? Because the purpose of the top level label
is to refer to the global number and the subequations are there for the reader to see.
documentclass{article}
usepackage{amsmath}
%usepackage{xparse} % not needed with LaTeX 2020-10-01 or later
ExplSyntaxOn
AddToHook{env/subequations/begin}
{
cs_set_eq:NN label __eddy_subequations_label:n
}
AddToHook{env/subequations/end}
{
bool_if:NT l__eddy_subequations_label_defer_bool
{
__eddy_subequations_label_do:V l__eddy_subequations_label_tl
}
}
bool_new:N l__eddy_subequations_label_defer_bool
tl_new:N l__eddy_subequations_label_tl
cs_new_protected:Nn __eddy_subequations_label:n
{
use:c { @bsphack }
bool_set_true:N l__eddy_subequations_label_defer_bool
tl_set:Nn l__eddy_subequations_label_tl { #1 }
cs_set_eq:Nc label { ltx@label }
use:c { @esphack }
}
cs_new_protected:Nn __eddy_subequations_label_do:n
{
int_case:nnF { value{equation} }
{
{1}{ __eddy_subequations_label_aux:nn { #1 } { a } } % just in case
{2}{ __eddy_subequations_label_aux:nn { #1 } { a,b } }
}
{ __eddy_subequations_label_aux:nn { #1 } { a-int_to_alph:n { value{equation} } } }
}
cs_generate_variant:Nn __eddy_subequations_label_do:n { V }
cs_new_protected:Nn __eddy_subequations_label_aux:nn
{
tl_set:cx { @currentlabel } { theparentequation #2 }
label{#1}
}
ExplSyntaxOff
begin{document}
begin{subequations} label{eqn1}
begin{align}
a&=b \
c&=d
end{align}
end{subequations}
begin{subequations} label{eqn2}
begin{align}
a&=b \
c&=d \
e&=f
end{align}
end{subequations}
The reference `eqref{eqn1}' I would like to appear as `(1a,b)'
and the reference `eqref{eqn2}' I would like to appear as `(2a-c)'.
end{document}
The idea is that the first label
command inside subequations
is for setting the global reference. However, we need to defer the setting when the full number of subequations is known. So label
is redefined to do the job and then it returns to its normal status.
You need this first label
if you plan to use label
also in environments that are not math displays inside the subequations
environments, such as enumerate
, for instance.
The deferred job suitably sets @currentlabel
and issues label
.
Note: this won't set the anchor for hyperref
at the beginning of the subequations
environment. That would cost much more.
Answered by egreg on January 16, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP