Open pwintz opened 1 year ago
The code below is out of date. I am just keeping it here because the new version is evolved from it, and some of the previous explanations still apply.
The difficulty here is that \label
might be redefined inside displayed equations. For example, amsmath
redefines it to be \label@in@display
, which only records the label text and then passes it to the original label command at the end of the equation. But I am not sure how ntheorem
handles this.
That said, if you use amsmath
, you may use the following modified version of cleveref-usedon.sty
(at the end). With this, you may set the UsedBy
text and style as:
% \usepackage{todonotes,marginnote}
\SetUsedByText
{%
Referenced by:#1.%
}
\SetUsedByMessage
{%
\todo[%
inline, % Display in-line instead of in the margin.
size=\scriptsize,
color=blue!40,
backgroundcolor=white,
textcolor=darkgray,
bordercolor=darkgray,
prepend]{%
\textup{\UsedByText{#1}}%
}%
}
\SetUsedByMessageMath
{%
\marginnote{\UsedByText{#1}}%
}
And similarly for UsedOn
and UsedByAndOn
. Then things should work as you wished.
However, since \label@in@display
is specific to amsmath
, without further test for other cases, I cannot yet risk to merge this version of code into the main branch for the sake of compatibility.
Below is the modified cleveref-usedon.sty
.
(updated, please see the next comment)
The code below is out of date. I am just keeping it here because the new version is evolved from it, and some of the previous explanations still apply.
Hopefully the following version also works for ntheorem
. The code is a bit redundant though.
Without \SetUsedByMessageMath
(or should I name it as \SetUsedByMathMessage
?), nothing would happen for equations, as usual. The actual configuration should definitely be left to the user.
Another note is that if instead print the info on the margin one wishes to print it immediately after an equation, the following code might help:
\ExplSyntaxOn
\SetUsedByMessageMath
{
\tl_gset:Ne \g_tmpa_tl {\UsedByText{#1}}
}
\AddToHook{env/equation/before}{\tl_gclear:N \g_tmpa_tl}
\AddToHook{env/equation/after}
{
\todo[%
inline, % Display in-line instead of in the margin.
size=\scriptsize,
color=blue!40,
backgroundcolor=white,
textcolor=blue!50!cyan,
bordercolor=blue!50!cyan,
prepend]{%
\textup{\tl_use:N \g_tmpa_tl}%
}%
}
\ExplSyntaxOff
Note that for some reason one has to use fully expanded version \tl_gset:Ne
otherwise there would be many errors like
Argument of \@cref@stack@top has an extra }.
<inserted text>
Paragraph ended before \@cref@stack@top was complete.
<to be read again>
...
Thus I am not really sure if it could always work... it works in my testing document though...
Also, the above code only patches equation
, the same thing needs to be done for other type of displayed environments.
Below is my testing document:
\documentclass{article}
\usepackage{ntheorem}
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\usepackage[usedby]{cleveref-usedon}
\usepackage{todonotes,marginnote}
\SetUsedByText
{%
Referenced by: #1.%
}
\SetUsedByMessage
{%
\todo[%
inline, % Display in-line instead of in the margin.
size=\scriptsize,
color=blue!40,
backgroundcolor=white,
textcolor=darkgray,
bordercolor=darkgray,
prepend]{%
\textup{\UsedByText{#1}}%
}%
}
% \SetUsedByMessageMath
% {%
% \marginnote{\UsedByText{#1}}%
% }
\ExplSyntaxOn
\SetUsedByMessageMath
{
\tl_gset:Ne \g_tmpa_tl {\UsedByText{#1}}
}
\AddToHook{env/equation/before}{\tl_gclear:N \g_tmpa_tl}
\AddToHook{env/equation/after}
{
\todo[%
inline, % Display in-line instead of in the margin.
size=\scriptsize,
color=blue!40,
backgroundcolor=white,
textcolor=blue!50!cyan,
bordercolor=blue!50!cyan,
prepend]{%
\textup{\tl_use:N \g_tmpa_tl}%
}%
}
\ExplSyntaxOff
\begin{document}
\begin{theorem}\label{thm}
\cref{eq}
\end{theorem}
\begin{lemma}
\cref{thm}, \cref{eq}
\end{lemma}
\begin{equation}
\label{eq}
\int x
% \meaning\label
\end{equation}
% \meaning\label
\end{document}
The code in https://github.com/SvenPistre/cleveref-usedon/issues/10#issuecomment-1770748342 gives a version that works for many types of displayed equations, with a new syntax for text and style configuration supported. With that version, it is possible to either print the Message in the margin as you originally suggested (via \SetUsedByMessageInsideMath
), or print the Message as usual (via \SetUsedByMessageOutsideMath
).
Currently, the UsedBy/UsedOn/etc messages are not displayed for equations. I would like to see where equations are referenced. It is tricky, however, because we probably don't want to use the same message format when displaying it within an equation, so it would be nice to have a way to specify an alternative. Otherwise, we'd have to do something like this: